BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
// 这里可以添加你的处理逻辑
// 例如,将句柄添加到列表中或者其他操作
// 这里我们仅仅输出到调试窗口
OutputDebugString(L"枚举窗口句柄: ");
char windowHandle[10];
sprintf(windowHandle, "%x", hwnd);
OutputDebugStringA(windowHandle);
OutputDebugStringA("\n");
WideChar windowText[256];
// 获取窗口标题
GetWindowText(hwnd, windowText, sizeof(windowText));
if(String(windowText).Length()>0)
Form1->Memo1->Lines->Append(windowText);
// 继续枚举其他窗口
return TRUE;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
EnumWindows(EnumWindowsProc, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
HWND hWnd = FindWindow(L"123456", NULL); //GetForegroundWindow();
if (hWnd != NULL)
{
WideChar windowText[256];
// 获取窗口标题
GetWindowText(hWnd, windowText, sizeof(windowText));
// 显示窗口标题
ShowMessage(windowText);
//const int messageID =0x0400+0; // 替换为正确的消息ID
//SendMessage(hWnd, WM_COMMAND, messageID, 0);
}
else
{
ShowMessage("无活动窗口");
}
}