Windows の extern デスクトップ アプリケーションから情報を取得しようとしています。
Textboxes (クラス "Edit") からテキストを抽出する方法は知っていますが、クラス名が "ThunderRT6ListBox" および "ThunderRT6ComboBox" のコントロールから値を抽出する方法はわかりません。どうやってやるの?
テキストボックスからテキストを抽出する次のコードがあります。
public static class ModApi
{
[DllImport("user32.dll", EntryPoint = "FindWindowA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "SendMessageTimeout", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint SendMessageTimeoutText(IntPtr hWnd, int Msg, int countOfChars, StringBuilder text, uint flags, uint uTImeoutj, uint result);
[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static internal extern bool EnumChildWindows(IntPtr hWndParent, funcCallBackChild funcCallBack, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, StringBuilder lParam);
const int LB_GETCOUNT = 0x018B;
const int LB_GETTEXT = 0x0189;
public static string GetText(IntPtr hwnd)
{
var text = new StringBuilder(1024);
if (SendMessageTimeoutText(hwnd, 0xd, 1024, text, 0x2, 1000, 0) != 0)
{
return text.ToString();
}
return "";
}
}
public foo()
{
IntPtr value = new IntPtr(0x019C086A); //ID locate using Spy++
String caption = ModApi.GetText(value);
}
更新 1:
ListBox から読み取る方法:
public static List<string> GetListBoxContents(IntPtr listBoxHwnd)
{
int cnt = (int)SendMessage(listBoxHwnd, LB_GETCOUNT, IntPtr.Zero, null);
List<string> listBoxContent = new List<string>();
for (int i = 0; i < cnt; i++)
{
StringBuilder sb = new StringBuilder(256);
IntPtr getText = SendMessage(listBoxHwnd, LB_GETTEXT, (IntPtr)i, sb);
listBoxContent.Add(sb.ToString());
}
return listBoxContent;
}
更新 2:
ComboBox から読み取る方法:
public static List<string> GetComboBoxContents(IntPtr cbBoxHwnd)
{
int cnt = (int)SendMessage(cbBoxHwnd, CB_GETCOUNT, IntPtr.Zero, null);
List<string> listBoxContent = new List<string>();
for (int i = 0; i < cnt; i++)
{
//int txtLength = SendMessage(cbBoxHwnd, CB_GETLBTEXTLEN, i, 0);
StringBuilder sb = new StringBuilder(256);
IntPtr getText = SendMessage(cbBoxHwnd, CB_GETLBTEXT, (IntPtr)i, sb);
listBoxContent.Add(sb.ToString());
}
return listBoxContent;
}