gpt4 book ai didi

c++ - 如何按位置获取窗口控制(win32 API)?

转载 作者:行者123 更新时间:2023-12-02 10:10:20 24 4
gpt4 key购买 nike

有没有办法使用控件的坐标位置来获取控制 handle ?换句话说,如果我们知道它的坐标位置,有没有办法在窗口区域找到控件?

最佳答案

您可以使用 WindowFromPoint :
这是示例:

#include <Windows.h>
#include <iostream>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR szCmdLine, _In_ int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}

hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
100,
100,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
POINT pt;
char bf[100];
HWND h;
HDC hdc;
RECT rc;
case WM_LBUTTONDOWN:
pt.x = 10;
pt.y = 10;
hdc = GetDC(hwnd);
h = WindowFromPoint(pt);
GetClientRect(hwnd, &rc);
sprintf_s(bf, 100, "%p\n", h);
TextOut(hdc,rc.right/2,rc.bottom/2,bf,strlen(bf));
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
当我按下左键时,用于设置坐标的 handle 显示在屏幕中间。
enter image description here

关于c++ - 如何按位置获取窗口控制(win32 API)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63873958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com