WM_SETTEXT写中文字母

时间:2018-07-23 13:48:50

标签: c# winforms treeview windows-messages editcontrol

我要设置“编辑控件”的文本。当我这样做时,新内容是中文。

例如,此:

[DllImport("user32.dll")]
public static extern int SendMessageW([InAttribute] System.IntPtr hWnd, int Msg, int wParam, string lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr GetFocus();

IntPtr c = GetFocus();
SendMessageW(c, 12, 0, "Test"); //Notice that 12 = WM_SETTEXT

将我的编辑控件设置为此:敔瑳

1 个答案:

答案 0 :(得分:0)

根据pinvoke.net,您需要在字符串参数上使用MarshalAs属性:

[DllImport("user32.dll")]
public static extern int SendMessageW(
    [InAttribute] System.IntPtr hWnd, 
    int Msg, 
    int wParam, 
    [MarshalAs(UnmanagedType.LPWStr)] string lParam);