#include"stdio.h"
#include"stdlib.h"
#include"windows.h"
int main()
{
SetConsoleTitle("11");
POINT *ptrpos;
RECT* rect;
HWND hwnd=FindWindow(NULL,"11");
ptrpos=(POINT*)malloc(sizeof(POINT));
rect=(RECT*)malloc(sizeof(RECT*));
while(1)
{
Sleep(500);
printf("%d\n",GetWindowRect(hwnd,rect));
GetCursorPos(ptrpos);
printf("Rrect->left=%lld rect->top=%lld %d %d\n",rect->left,rect->top,ptrpos->x,ptrpos->y);
}
}
在运行以上代码时,rect->left总不在预期中,经查资料发现,GetWindowRect函数调用失败返回0,该程序确实返回了0,就大佬帮忙下,为什么会失败?
这样写这样写 HWND hwnd = FindWindowEx(NULL,NULL,L"ConsoleWindowClass",0);
return 0一般是正常的 但是接口说是不正常,因为你的句柄找的是NULL
FindWindow(NULL,"11");
这个句柄(人)程序都不知道在哪里,程序不可能满大街给你找..你要获取句柄啊。。
兄弟 我这个打出来了。。。你用的可能是假的Visual
#include"stdio.h"
#include"stdlib.h"
#include"windows.h"
using namespace std;
int main()
{
SetConsoleTitle(L"11");
POINT* ptrpos;
RECT* rect;
HWND hwnd = FindWindow(NULL,L"11");
ptrpos = (POINT*)malloc(sizeof(POINT));
rect = (RECT*)malloc(sizeof(RECT));
while (1)
{
Sleep(500);
printf("%d\n", GetWindowRect(hwnd, rect));
GetCursorPos(ptrpos);
printf("Rrect->left=%lld rect->top=%lld\n", rect->left, rect->top);
}
}
我在Visual stdudio 2019 运行,还是返回0