VC怎样获取屏幕取点的坐标

2024-11-23 01:38:07
推荐回答(3个)
回答1:

你自己去研究下
// eg11.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include

HWND MainHwnd;
HWND TrackHwnd;

UINT g_nXCoor;
UINT g_nYCoor;
//
LRESULT CALLBACK WndProcMain (HWND hWnd, UINT message, WPARAM wParam ,LPARAM lParam);
LRESULT CALLBACK WndProcTrack (HWND hWnd, UINT message, WPARAM wParam ,LPARAM lParam);

int _stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrev,
char * lpCmdLine, int nShowCmd )
{

WNDCLASSEX WndClassEx;

WndClassEx.cbSize = sizeof( WNDCLASSEX );
WndClassEx.style = NULL;
WndClassEx.lpfnWndProc = WndProcMain;

WndClassEx.cbClsExtra = 0;
WndClassEx.cbWndExtra = 0;
WndClassEx.hInstance = hInstance;
WndClassEx.hIcon = LoadIcon( NULL, IDI_APPLICATION );
WndClassEx.hCursor = LoadCursor( NULL, IDC_ARROW );
WndClassEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

WndClassEx.lpszMenuName = NULL;
WndClassEx.lpszClassName = "MyWndClassMain";
WndClassEx.hIconSm = LoadIcon( NULL, IDI_APPLICATION );

::RegisterClassEx( &WndClassEx );

MainHwnd = CreateWindow ( "MyWndClassMain", "My First Windows", WS_OVERLAPPEDWINDOW,
100, 100, 600, 600, NULL, NULL, hInstance, NULL);

WndClassEx.lpfnWndProc = WndProcTrack;
WndClassEx.lpszClassName = "MyWndClassTrack";
::RegisterClassEx( &WndClassEx );

TrackHwnd = CreateWindow ("MyWndClassTrack", "TrackWindows", WS_OVERLAPPEDWINDOW | WS_CHILD,
0, 0, 200, 50, MainHwnd, NULL, hInstance, NULL);

ShowWindow (MainHwnd, nShowCmd );
ShowWindow (TrackHwnd, nShowCmd );

MSG msg;
while( GetMessage( &msg,NULL,NULL,NULL ) )
{
::DispatchMessage( &msg );
}
return 0;
}

/***************************************************************
* Name:

* Description:

* Input:

* Return:

* History:

* Warning:

****************************************************************/
LRESULT CALLBACK WndProcMain (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch( message )
{
case WM_MOUSEMOVE:

g_nXCoor = LOWORD( lParam );
g_nYCoor = HIWORD( lParam );

InvalidateRect( hWnd, NULL,TRUE );

MoveWindow (TrackHwnd, g_nXCoor + 15, g_nYCoor + 15, 200, 50, TRUE );
break;

case WM_PAINT:
{
PAINTSTRUCT PS;
HDC hDC;
RECT rectClient;

hDC = BeginPaint (hWnd, &PS);

GetClientRect ( hWnd, &rectClient);

MoveToEx (hDC, g_nXCoor, rectClient.top, NULL );
LineTo (hDC, g_nXCoor, rectClient.bottom);

MoveToEx (hDC, rectClient.left, g_nYCoor, NULL );
LineTo (hDC, rectClient.right, g_nYCoor);

EndPaint (hWnd, &PS);
}
break;

case WM_DESTROY:
::PostQuitMessage ( 0 );
break;

default:
return ::DefWindowProc(hWnd, message, wParam, lParam );
}

return TRUE;
}

/***************************************************************
* Name:

* Description:

* Input:

* Return:

* History:

* Warning:

****************************************************************/
LRESULT CALLBACK WndProcTrack (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch( message )
{
case WM_PAINT:
{
PAINTSTRUCT PS;
HDC hDC;
RECT rectClient;
CHAR strMousePosition[100];

hDC = BeginPaint (hWnd, &PS);

GetClientRect (hWnd, &rectClient);

sprintf (strMousePosition, "Mouse Pt: x= %4d y = %4d", g_nXCoor, g_nYCoor);

TextOut (hDC, 0, 0, strMousePosition, strlen ( strMousePosition));

EndPaint (hWnd, &PS);
}
break;

case WM_DESTROY:
::PostQuitMessage( 0 );
break;

default:

return ::DefWindowProc(hWnd, message, wParam, lParam );
}

return TRUE;
}
另外,虚机团上产品团购,超级便宜

回答2:

用windows的消息机制,也能知道你当前鼠标的像素坐标。
LOWORD( lParam ), HIWORD( lParam ) 对应的是X,Y坐标

回答3:

api函数GetCursorPos
或者你用mfc