2

I'm developing a dictionary. I'm using this code, to get text from the clipboard.

    [DllImport("User32.dll")]
    protected static extern int SetClipboardViewer(int hWndNewViewer);
    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
    IntPtr nextClipboardViewer;

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        // defined in winuser.h
        const int WM_DRAWCLIPBOARD = 0x308;
        const int WM_CHANGECBCHAIN = 0x030D;

        switch (m.Msg)
        {
            case WM_DRAWCLIPBOARD:
                DisplayClipboardData();
                SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
                break;
            case WM_CHANGECBCHAIN:
                if (m.WParam == nextClipboardViewer)
                    nextClipboardViewer = m.LParam;
                else
                    SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }
    internal void DisplayClipboardData()
    {
            bool isDataText = false;
            IDataObject iData = new DataObject();
            iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Text))
            {
                textBox1.Text = (string)iData.GetData(DataFormats.Text); 
            }
    }

First - I can set my keyboard language to EN (english) or BG (bulgarian). The problem occurs when I set my keyboard language to EN and try to copy non-latin chars, then I get ????? instead the correct word. If I set to BG, I get the correct word. I tried to copy non-latin text from UTF-8 and ANSII encoded documents, it's the same, no difference, I get only ?????? (question marks)

vinsa
  • 1,132
  • 12
  • 25

1 Answers1

3

Try to use Clipboard.GetText(); instead of (string)iData.GetData(DataFormats.Text);, which should get the text in the correct format (Text or UnicodeText).

Torben Kohlmeier
  • 6,713
  • 1
  • 15
  • 15
  • 1
    I tied `textBox1.Text = Clipboard.GetText(TextDataFormat.UnicodeText);` and it's working – vinsa Jun 02 '13 at 21:04
  • 1
    Depending on the OS, you have to get the text in UnicodeText or Text format. `Clipboard.GetText();` will choose the correct format for you, so you should use this method. – Torben Kohlmeier Jun 02 '13 at 21:09