CB_SELECTSTRING 不适用于日文项目

CB_SELECTSTRING is not working for Japanese item

 [DllImport("user32.dll")]
 public static extern int SendMessage(IntPtr hWnd, int msg, string wParam, string lParam);

 Private void GetSlecteITemIdex()
 {
       int _ComBoxHandle = System.Windows.Automation.AutomationElement.Current.NativeWindowHandle;
       int l_GETSelectedItem1 = SendMessage((IntPtr)l_ComBoxHandle, CB_SELECTSTRING, null, "英語");
         if (l_GETSelectedItem1 == -1)
                throw new Exception("Item not found.");
 }

组合图片:

我想从组合框中获取日语项目“英语”的索引,但它总是使用上面的代码给出索引“-1”,对于英语项目,它给我正确的索引,另外需要做的是正确获取日语项目索引。

根据 docsCB_SELECTSTRING 消息的参数是:

  • wParam: The zero-based index of the item preceding the first item to be searched... If wParam is -1, the entire list is searched from the beginning.

  • lParam: A pointer to the null-terminated string that contains the characters for which to search. The search is not case sensitive, so this string can contain any combination of uppercase and lowercase letters.

因此,您应该从 https://www.pinvoke.net/default.aspx/user32.sendmessage 中为 SendMessage 选择以下声明:

[DllImport("user32.dll", CharSet = CharSet.Auto)] 
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam)

为第一个参数传递 -1(而不是 null)以从头开始搜索。

(请注意,CharSet = CharSet.Unicode may be chosen also. As explained in Charsets and marshalingAutoUnicode 都会导致字符串在 Windowschar16_t (UTF-16) 在 .NET Core 2.2 和更早版本的 Unix 上;它们仅在 .NET Core 3.0 及更高版本和 Unix 上的 Mono,其中 Auto 将字符串编组为 char (UTF-8).)