a c#winforms应用程序在表单上具有列表框。ListBox窗口句柄传递给使用SendMessage(HWND,LB_ADDSTRING ...)的Legacy Win32 DLL将项目添加到列表框中。这些字符串出现在运行时的列表框中,但是listbox.items.count是0,并且无法使用listBox.items [x] .toString()
访问单个项目。您需要在C#应用中做什么才能使它意识到这些字符串在其列表中,因此应反映在项目中。Div>
创建ListBox
的子类,覆盖WndProc
,收听LB_ADDSTRING
消息(值= 0x180),防止这些消息正常处理,而是将其包含的数据添加到Items
集合中。尚未测试此代码,但它应该足够接近您的需求:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class LegacyListBox : ListBox
{
private const int LB_ADDSTRING = 0x180;
public LegacyListBox() { }
protected override void WndProc(ref Message m)
{
if (m.Msg == LB_ADDSTRING)
{
Items.Add(Marshal.PtrToStringUni(m.LParam));
// prevent base class from handling this message
return;
}
base.WndProc(ref m);
}
}