コード例 #1
19
        private string CopySelectedText()
        {
            if (null == scriptState)
                return string.Empty;

            string textToCopy = scriptState.textBuffer.GetText(scriptState.selectionStart, scriptState.selectionEnd);

            textToCopy = textToCopy.Replace("\n", "\r\n");

            try
            {
                RichTextFormatter formatter = new RichTextFormatter(
                    scriptState.textBuffer, codeFragmentManager);

                string formattedContent = formatter.Format(
                    scriptState.selectionStart, scriptState.selectionEnd);

                DataObject clipboardData = new DataObject();
                clipboardData.SetData(DataFormats.Text, textToCopy);
                clipboardData.SetData(DataFormats.Rtf, formattedContent);
                Clipboard.SetDataObject(clipboardData);
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                System.Threading.Thread.Sleep(0);
                try
                {
                    Clipboard.SetData(DataFormats.Text, textToCopy);
                }
                catch (System.Runtime.InteropServices.COMException)
                {
                    MessageBox.Show("Can't Access Clipboard");
                }
            }

            return textToCopy;
        }
コード例 #2
0
        private string CopySelectedText()
        {
            if (null == scriptState)
            {
                return(string.Empty);
            }

            string textToCopy = scriptState.textBuffer.GetText(scriptState.selectionStart, scriptState.selectionEnd);

            textToCopy = textToCopy.Replace("\n", "\r\n");

            try
            {
                RichTextFormatter formatter = new RichTextFormatter(
                    scriptState.textBuffer, codeFragmentManager);

                string formattedContent = formatter.Format(
                    scriptState.selectionStart, scriptState.selectionEnd);

                DataObject clipboardData = new DataObject();
                clipboardData.SetData(DataFormats.Text, textToCopy);
                clipboardData.SetData(DataFormats.Rtf, formattedContent);
                Clipboard.SetDataObject(clipboardData);
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                System.Threading.Thread.Sleep(0);
                try
                {
                    Clipboard.SetData(DataFormats.Text, textToCopy);
                }
                catch (System.Runtime.InteropServices.COMException)
                {
                    MessageBox.Show("Can't Access Clipboard");
                }
            }

            return(textToCopy);
        }