下面这个例子是获取屏幕的坐标:
#include<windows.h> #include<windowsx.h> LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ) { static TCHAR szAppName[]=TEXT("AppName"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hInstance=hInstance; wndclass.lpfnWndProc=WindowProc; wndclass.lpszClassName=szAppName; wndclass.lpszMenuName=NULL; wndclass.style=CS_HREDRAW|CS_VREDRAW; if(!RegisterClass(&wndclass)){ MessageBox(NULL,TEXT("This program requires Windows NT"),szAppName,MB_ICONERROR); } hwnd=CreateWindow( szAppName, // registered class name TEXT("This is title"), // window name WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // horizontal position of window CW_USEDEFAULT, // vertical position of window CW_USEDEFAULT, // window width CW_USEDEFAULT, // window height NULL, // handle to parent or owner window NULL, // menu handle or child identifier hInstance, // handle to application instance NULL // window-creation data ); ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0)){ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { HDC hdc; PAINTSTRUCT ps; TCHAR szBuffer[30]; POINT point; RECT rect; static RECT rr; static int cxClient; static int cyClient; switch(uMsg){ case WM_SIZE: cxClient=GET_X_LPARAM(lParam); cyClient=GET_Y_LPARAM(lParam); GetClientRect(hwnd,&rr); //wsprintf(szBuffer,TEXT("cyClient %d"),cyClient); //MessageBox(NULL,szBuffer,TEXT("tip"),MB_ICONERROR); return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); GetCursorPos(&point); TextOut(hdc,200,200,szBuffer,wsprintf(szBuffer,TEXT("the point is %d,%d"),point.x,point.y)); EndPaint(hwnd,&ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_MOUSEMOVE: //SendMessage(hwnd,WM_PAINT,0,0); rect.left=rr.left; rect.top=rr.top; rect.right=rr.right; rect.bottom=rr.bottom; InvalidateRect(hwnd,&rect,TRUE); return 0; } return DefWindowProc(hwnd,uMsg,wParam,lParam); }
可以把屏幕坐标转化为客户区坐标:
#include<windows.h> #include<windowsx.h> LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ) { static TCHAR szAppName[]=TEXT("AppName"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hInstance=hInstance; wndclass.lpfnWndProc=WindowProc; wndclass.lpszClassName=szAppName; wndclass.lpszMenuName=NULL; wndclass.style=CS_HREDRAW|CS_VREDRAW; if(!RegisterClass(&wndclass)){ MessageBox(NULL,TEXT("This program requires Windows NT"),szAppName,MB_ICONERROR); } hwnd=CreateWindow( szAppName, // registered class name TEXT("This is title"), // window name WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // horizontal position of window CW_USEDEFAULT, // vertical position of window CW_USEDEFAULT, // window width CW_USEDEFAULT, // window height NULL, // handle to parent or owner window NULL, // menu handle or child identifier hInstance, // handle to application instance NULL // window-creation data ); ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0)){ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { HDC hdc; PAINTSTRUCT ps; TCHAR szBuffer[30]; POINT point; RECT rect; static RECT rr; static int cxClient; static int cyClient; switch(uMsg){ case WM_SIZE: cxClient=GET_X_LPARAM(lParam); cyClient=GET_Y_LPARAM(lParam); GetClientRect(hwnd,&rr); //wsprintf(szBuffer,TEXT("cyClient %d"),cyClient); //MessageBox(NULL,szBuffer,TEXT("tip"),MB_ICONERROR); return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); GetCursorPos(&point); ScreenToClient(hwnd,&point);//将屏幕坐标转化为客户坐标 TextOut(hdc,200,200,szBuffer,wsprintf(szBuffer,TEXT("the point is %d,%d"),point.x,point.y)); EndPaint(hwnd,&ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_MOUSEMOVE: //SendMessage(hwnd,WM_PAINT,0,0); rect.left=rr.left; rect.top=rr.top; rect.right=rr.right; rect.bottom=rr.bottom; InvalidateRect(hwnd,&rect,TRUE); return 0; } return DefWindowProc(hwnd,uMsg,wParam,lParam); }