Minigui学习---鼠标获取和坐标在不同区域转换

(1)设置鼠标的所在的Wnd:

函数定义(window.h文件中定义的):

MG_EXPORT HWND GUIAPI GetCapture(void); //Retrives the handle to the window (if any) that has captured the mouse.return The handle to the window that has captured the mouse, 0 for no window captures the mouse.

Only one window at a time can capture the mouse; this window receives mouse input whether or not the cursor is within its borders.

MG_EXPORT HWND GUIAPI SetCapture(HWND hWnd);//Sets the mouse capture to the specified window. param hWnd The handle to the window.return The old capture window. Once a window has captured the mouse, all mouse input is directed to that window, regardless of whether the cursor is within the borders of that window. Only one window at a time can capture the mouse.

MG_EXPORT void GUIAPI ReleaseCapture(void); //Releases the mouse capture from a window and restores normal mouse  input processing

例如:

  printf("hjhe--hwnd=%x\n",hWnd);
  printf("hjhe--getCapture=%x\n",GetCapture());
  SetCapture(hWnd);
  printf("hjhe--1 getCapture=%x\n",GetCapture());

返回值为:

hjhe--hwnd=8c04cc0
hjhe--getCapture=0
hjhe--1 getCapture=8c04cc0


(2)屏幕和窗口坐标转换

函数定义(window.h文件中定义的):

/* 

* \param hWnd The handle to the window.
 * \param x The pointer to the x coordinate.
 * \param y The pointer to the y coordinate.

*/

MG_EXPORT void GUIAPI ClientToScreen (HWND hWnd, int* x, int* y);//converts the client coordinates of the specified point (*x,*y) in the specified window hWnd to screen coordinates.

 

/**
 * \param hWnd The handle to the window.
 * \param x The pointer to the x coordinate.
 * \param y The pointer to the y coordinate.
 */
MG_EXPORT void GUIAPI ScreenToClient (HWND hWnd, int* x, int* y);//converts the screen coordinates of the specified point   (*x,*y) to client coordinates of the specified window   hWnd

注意:Screen coordinates是包括标题栏、边框等在内的所有部分;而client coordinates仅是除去标题栏、边框的客户绘制区域;所以当我们在创建窗体的时候将窗口的style设置为none(即 CreateInfo.dwStyle = WS_VISIBLE | WS_NONE ),那么窗口将没有 标题栏和边框,所以这两个坐标是一致的;但是如果设置了标题栏和边框(即:CreateInfo.dwStyle = WS_VISIBLE | WS_BORDER |WS_CAPTION;),那么这两个坐标就是不一致的,他们之间的差值是:标题栏的高度和边框的宽度

注意:当处理过程接收到MSG_LBUTTONUPMSG_MOUSEMOVEMSG_LBUTTONDOWNMSG_RBUTTONDOWN等消息时,通过int x = LOWORD (lParam); int y = HIWORD (lParam);解析到的坐标为client coordinates坐标值。

 

注意:通过GetWindowRect获取到的是client 区域的大小,而不是Screen区域大小;可以通过ClientToScreen函数将其转换为Screen区域大小。

例如:

RECT rcx;

GetWindowRect(hMainWnd, &rcx);
    printf("hjhe--hMainWnd rcx.left=%d,rcx.top=%d,rcx.right=%d,rcx.bottom=%d\n",rcx.left,rcx.top,rcx.right,rcx.bottom);
GetWindowRect(hWnd, &rcx);
  printf("hjhe--hWnd rcx.left=%d,rcx.top=%d,rcx.right=%d,rcx.bottom=%d\n",rcx.left,rcx.top,rcx.right,rcx.bottom);
ClientToScreen(hMainWnd, &rcx.left, &rcx.top);
ClientToScreen(hMainWnd, &rcx.right, &rcx.bottom);
   printf("hjhe--rcx.left=%d,rcx.top=%d,rcx.right=%d,rcx.bottom=%d\n",rcx.left,rcx.top,rcx.right,rcx.bottom);

返回值为:

hjhe--hMainWnd rcx.left=0,rcx.top=0,rcx.right=240,rcx.bottom=320
hjhe--hWnd rcx.left=0,rcx.top=0,rcx.right=240,rcx.bottom=320
hjhe--rcx.left=2,rcx.top=23,rcx.right=242,rcx.bottom=343

从返回值可以看出,Screen区域比Client区域高13,宽2,即可知道标题栏目高为13,边框宽度为2

例如:

int x = LOWORD (lParam);
int y = HIWORD (lParam);

  printf("hjhe--2 x=%d,y=%d\n",x,y);

ClientToScreen (hWnd, &x, &y);
   printf("hjhe--1 x=%d,y=%d\n",x,y);
ScreenToClient(hWnd, &x, &y);
   printf("hjhe--x=%d,y=%d\n",x,y);

返回值为:

hjhe--2 x=176,y=58

hjhe--1 x=178,y=81
hjhe--x=176,y=58

从返回值中可以验证:Screen区域比Client区域高13

(3)window坐标和屏幕坐标转换

函数定义(window.h文件中定义的):

/**
 *
 * \param hWnd The handle to the window.
 * \param x The pointer to the x coordinate.
 * \param y The pointer to the y coordinate.
  */
MG_EXPORT void GUIAPI WindowToScreen (HWND hWnd, int* x, int* y);//converts the window coordinates of the specified point (*x,*y) in the specified window \a hWnd to the screen coordinates

/**
 *
 * \param hWnd The handle to the window.
 * \param x The pointer to the x coordinate.
 * \param y The pointer to the y coordinate.
  */
MG_EXPORT void GUIAPI ScreenToWindow (HWND hWnd, int* x, int* y);//converts the screen coordinates of the specified point  (*x,*y) to the window coordinates of the specfied window \a hWnd

你可能感兴趣的:(minigui学习)