VC关于如何获取鼠标点击时的屏幕坐标
我在VC下利用MFC创建了一个对话框程序,想把它作为一个截图软件,但是怎么去获得鼠标点击在屏幕上的坐标啊?只有获取坐标才能构建出一个矩形框啊。有哪位大侠教教我,救命啊!...
我在VC下利用MFC创建了一个对话框程序,想把它作为一个截图软件,但是怎么去获得鼠标点击在屏幕上的坐标啊?只有获取坐标才能构建出一个矩形框啊。有哪位大侠教教我,救命啊!
展开
3个回答
展开全部
这是我用 wind32做的 直接把代码发给你好了 你自己去研究下
// eg11.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <stdio.h>
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;
}
// eg11.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <stdio.h>
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;
}
展开全部
有几种方案。
1.如ls说的,建立一个全屏的透明窗口(先隐藏当前的控制对话框),然后在透明窗口上拖动鼠标获取一个RECT ,当然透明窗口也是一个对话框。
2.隐藏当前的控制对话框,执行 SetCapture(),在屏幕上拖动鼠标获取一个RECT,WM_LBUTTONUP的时候执行 ReleaseCapture().这样也可以获取一个有效的RECT,
1.如ls说的,建立一个全屏的透明窗口(先隐藏当前的控制对话框),然后在透明窗口上拖动鼠标获取一个RECT ,当然透明窗口也是一个对话框。
2.隐藏当前的控制对话框,执行 SetCapture(),在屏幕上拖动鼠标获取一个RECT,WM_LBUTTONUP的时候执行 ReleaseCapture().这样也可以获取一个有效的RECT,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建一个没有边的透明对话框,最大化。
让桌面上的所有窗口重画一边(参考链接)。
然后用CDC等函数。
别忘了CImage
让桌面上的所有窗口重画一边(参考链接)。
然后用CDC等函数。
别忘了CImage
参考资料: http://www.softist.com/programming/enum-win/enum-win.htm
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询