Beispiel #1
0
        // 复制或者剪切ListView中选定的事项到Clipboard
        // parameters:
        //      bCut    是否为剪切
        public static void CopyLinesToClipboard(
            Form form,
            string strFormat,
            ListView list,
            bool bCut)
        {
            Cursor oldCursor = form.Cursor;

            form.Cursor = Cursors.WaitCursor;

            MemLineCollection mem_lines = new MemLineCollection();

            mem_lines.Format = strFormat;

            List <int> indices  = new List <int>();
            string     strTotal = "";

            for (int i = 0; i < list.SelectedIndices.Count; i++)
            {
                int index = list.SelectedIndices[i];

                ListViewItem item    = list.Items[index];
                string       strLine = Global.BuildLine(item);
                strTotal += strLine + "\r\n";

                MemLine mem_line = new MemLine();
                mem_line.Item = item;
                mem_line.Tag  = item.Tag;
                mem_lines.Add(mem_line);

                if (bCut == true)
                {
                    indices.Add(index);
                }
            }

            // Clipboard.SetDataObject(strTotal, true);

            DataObject obj = new DataObject();

            obj.SetData(typeof(MemLineCollection), mem_lines);
            obj.SetData(strTotal);
            Clipboard.SetDataObject(obj, true);

            if (bCut == true)
            {
                for (int i = indices.Count - 1; i >= 0; i--)
                {
                    int index = indices[i];
                    list.Items.RemoveAt(index);
                }
            }

            form.Cursor = oldCursor;
        }
Beispiel #2
0
        // 从剪贴板中Paste行插入到ListView中当前选定的位置
        // parameters:
        //      bInsertBefore   是否前插? 如果==true前插,否则后插
        public static void PasteLinesFromClipboard(Form form,
                                                   string strFormatList,
                                                   ListView list,
                                                   bool bInsertBefore)
        {
            IDataObject ido = Clipboard.GetDataObject();

            if (ido.GetDataPresent(DataFormats.UnicodeText) == false &&
                ido.GetDataPresent(typeof(MemLineCollection)) == false)
            {
                MessageBox.Show(form, "剪贴板中没有内容");
                return;
            }

            // 优先使用专用格式
            if (ido.GetDataPresent(typeof(MemLineCollection)) == true)
            {
                MemLineCollection mem_lines = (MemLineCollection)ido.GetData(typeof(MemLineCollection));

                if (StringUtil.IsInList(mem_lines.Format, strFormatList) == false)
                {
                    MessageBox.Show(form, "剪贴板中的内容格式不符合当前窗口要求,无法粘贴");
                    return;
                }

                int index = -1;

                if (list.SelectedIndices.Count > 0)
                {
                    index = list.SelectedIndices[0];
                }

                Cursor oldCursor = form.Cursor;
                form.Cursor = Cursors.WaitCursor;

                list.SelectedItems.Clear();

                foreach (MemLine line in mem_lines)
                {
                    /*
                     * ListViewItem item = Global.BuildListViewItem(
                     *  list,
                     *  line.Line);
                     * item.Tag = line.Tag;
                     * */
                    line.Item.Tag = line.Tag;

                    if (index == -1)
                    {
                        list.Items.Add(line.Item);
                    }
                    else
                    {
                        if (bInsertBefore == true)
                        {
                            list.Items.Insert(index, line.Item);
                        }
                        else
                        {
                            list.Items.Insert(index + 1, line.Item);
                        }

                        index++;
                    }

                    line.Item.Selected = true;
                }

                form.Cursor = oldCursor;
                return;
            }

            if (ido.GetDataPresent(DataFormats.UnicodeText) == true)
            {
                string strWhole = (string)ido.GetData(DataFormats.UnicodeText);

                int index = -1;

                if (list.SelectedIndices.Count > 0)
                {
                    index = list.SelectedIndices[0];
                }

                Cursor oldCursor = form.Cursor;
                form.Cursor = Cursors.WaitCursor;

                list.SelectedItems.Clear();

                string[] lines = strWhole.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < lines.Length; i++)
                {
                    ListViewItem item = Global.BuildListViewItem(
                        list,
                        lines[i]);

                    if (index == -1)
                    {
                        list.Items.Add(item);
                    }
                    else
                    {
                        if (bInsertBefore == true)
                        {
                            list.Items.Insert(index, item);
                        }
                        else
                        {
                            list.Items.Insert(index + 1, item);
                        }

                        index++;
                    }

                    item.Selected = true;
                }

                form.Cursor = oldCursor;
            }
        }