`
java-mans
  • 浏览: 11434639 次
文章分类
社区版块
存档分类
最新评论

获取鼠标坐标GetCursorPos

 
阅读更多

下面这个例子是获取屏幕的坐标:

#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);  
} 


分享到:
评论

相关推荐

    【VS2010 C# 代码】获取鼠标所在位置的窗口句柄名称和文字及位置

    [DllImport("user32.dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标 public static extern int GetCursorPos( ref POINTAPI lpPoint ); [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定...

    VB获取鼠标指针坐标

    摘要:VB源码,API专区,获取坐标 VB获取鼠标指针坐标,在窗口中适时显示... GetCursorPos point '获取鼠标位置  Label1.Caption = "当前鼠标坐标:" & point.x & " , " & point.y  End Sub 运行环境:Windows/VB6

    鼠标实时位置跟踪

    C++实现鼠标跟踪实时显示图像坐标,获取鼠标位置(在屏幕的位置) CPoint m_mouse; GetCursorPos(&m_mouse); 2、 屏幕转化为客户端(控件的相对位置)& 客户端位置转化为屏幕位置 ClientToScreen(this-&gt;m_...

    VB取屏幕颜色

    获得坐标点颜色,跟踪鼠标坐标的颜色,GetDC,GetPixel,GetCursorPos

    恶搞qq窗口

    //鼠标位置 POINT pos = { 0 }; //qq登录窗口的整体矩形坐标 ... //获取鼠标位置 GetCursorPos(&pos;); //获得qq登录窗口的整体矩形坐标 GetWindowRect(hwndQQ, &wndRect;); 一系列操作恶搞qq窗口

    VC API常用函数简单例子大全89个

    第三个:GetCursorPos获取鼠标当前位置 第四个:WindowFromPoint根据坐标点获得对应的窗口句柄 第五个MoveWindow根据窗口句柄移动窗口,改变窗口大小 第六个ShowWindow设置窗口显示状态,如隐藏,最大化,最小化 第...

    鼠标模拟键盘.frm

    'Command1.Caption = GetPixel(a, p.x, p.y) 'h获取颜色值 If p.x = mx Or p.y = my Then GoTo 50 If p.x &gt; mx Then GoTo 10 If p.x GoTo 50 10 SendKeys "右" GoTo 50 20 SendKeys "左" GoTo 50 30 ...

    VC API常用函数简单例子大全.doc

    第三个:GetCursorPos获取鼠标当前位置(屏幕) 6 第四个:WindowFromPoint根据坐标点获得对应的窗口句柄 6 第五个MoveWindow根据窗口句柄移动窗口,改变窗口大小 6 第六个ShowWindow设置窗口显示状态,如隐藏,...

    API之网络函数---整理网络函数及功能

    GetMessagePos 取得消息队列中上一条消息处理完毕时的鼠标指针屏幕位置 GetMessageTime 取得消息队列中上一条消息处理完毕时的时间 PostMessage 将一条消息投递到指定窗口的消息队列 PostThreadMessage 将一条...

    C++MFC教程

    例如有两个窗口共用一个窗口过程代码,你在窗口一上按下鼠标时消息就会通过窗口一的句柄被发送到窗口一而不是窗口二。 5、示例:下面有一段伪代码演示如何在窗口过程中处理消息 LONG yourWndProc(HWND hWnd,UINT ...

Global site tag (gtag.js) - Google Analytics