先把這兩個定義給弄清除 INT_PTR CALLBACK
typedef __int64 INT_PTR, *PINT_PTR;
#define CALLBACK __stdcall
FillRect 填充矩形區域
WINUSERAPI
int
WINAPI
FillRect(
__in HDC hDC,
__in CONST RECT *lprc, //要填充的矩形區域
__in HBRUSH hbr //畫筆,這裡的畫筆可以不用選入DC直接填充,
);
SetRect設定矩形區域的範圍,可以直接用左上和右下四個點設定矩形區域的範圍
WINUSERAPI
BOOL
WINAPI
SetRect(
__out LPRECT lprc, //要設定的對象
__in int xLeft,
__in int yTop,
__in int xRight,
__in int yBottom);
FrameRect 指定的畫刷為指定的矩形畫邊框。邊框的寬和高總是一個邏輯單元。
WINUSERAPI
int
WINAPI
FrameRect(
__in HDC hDC,
__in CONST RECT *lprc,
__in HBRUSH hbr);
InvertRect 對矩形區域内的顔色進行取反操作 (函數通過對矩形内部的像素點進行邏輯NOT操作而将視窗中的矩形反轉。)
WINUSERAPI
BOOL
WINAPI
InvertRect(
__in HDC hDC,
__in CONST RECT *lprc);
OffsetRect 對矩形區域進行偏移,偏移distancex和distancey長度
WINUSERAPI
BOOL
WINAPI
OffsetRect(
__inout LPRECT lprc,
__in int dx,
__in int dy);
SetRectEmpty 将矩形lpRect設為一個空矩形
IsRectEmpty 矩形區域是否為空
WINUSERAPI
BOOL
WINAPI
SetRectEmpty(
__out LPRECT lprc);
WINUSERAPI
BOOL
WINAPI
SetRectEmpty(
__out LPRECT lprc);
InflateRect函數增大或減小指定矩形的寬和高。InflateRect函數在矩形的左和右增加dx,在矩形的上下增加dy。 dx和dy參數是有符号值。正數增加寬和高,負數減小。
WINUSERAPI
BOOL
WINAPI
InflateRect(
__inout LPRECT lprc,
__in int dx,
__in int dy);
GetClientRect、GetWindowRect 獲得窗體的矩形區域
長為rect.right,高為rect.bottom
WINUSERAPI
BOOL
WINAPI
GetClientRect(
__in HWND hWnd,
__out LPRECT lpRect);
WINUSERAPI
BOOL
WINAPI
GetWindowRect(
__in HWND hWnd,
__out LPRECT lpRect);
IntersectRect獲得兩個矩形的相交區域
UnionRect獲得兩個矩形合并區域
//相交兩個矩形區域
WINUSERAPI
BOOL
WINAPI
IntersectRect(
__out LPRECT lprcDst,
__in CONST RECT *lprcSrc1,
__in CONST RECT *lprcSrc2);
//合并兩個矩形區域
WINUSERAPI
BOOL
WINAPI
UnionRect(
__out LPRECT lprcDst,
__in CONST RECT *lprcSrc1,
__in CONST RECT *lprcSrc2);
PtInRect點是否在矩形區域内
WINUSERAPI
BOOL
WINAPI
PtInRect(
__in CONST RECT *lprc,
__in POINT pt);
在這裡程式設計的時候,學習到一個小技巧
DlgMainProc消息處理函數的最後一個參數,LPARAM lParam是傳遞了滑鼠的指針
POINT pt;
pt.x = LOWORD(lParam);獲得滑鼠的x點
pt.y = HIWORD(lParam);獲得滑鼠的y點
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPn1kMVR1TyEkeNBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL4ITM5ATO0EDMzIjMwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
下面是我寫的代碼
#include <Windows.h>
#include "resource.h"
INT_PTR CALLBACK DlgMainProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
BOOL bRet = true;
HDC hdc;
PAINTSTRUCT ps;
static int ix = 10;
static int iy = 10;
HBRUSH hBrush;
TCHAR szBuffer[256];
DWORD isEmpty;
RECT rect;
RECT rect_dest1,rect_dest2;
RECT rect_src1,rect_src2;
int wmId,b;
POINT pt;
switch(uMsg)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
b = HIWORD(wParam);
switch(wmId)
{
case IDC_BUTTON1:
isEmpty = IsRectEmpty(&rect);
wsprintf(szBuffer,TEXT("SetRectEmpty:\nx = %d \ny = %d \nIsRectEmpty:%d\n"),rect.left,rect.top,isEmpty);
MessageBox(hWnd,szBuffer,TEXT("test"),MB_OK);
break;
case IDC_BUTTON2:
EndDialog(hWnd,0);
break;
default:
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
break;
case WM_CLOSE:
EndDialog(hWnd,0);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd,&ps);
SetRect(&rect,ix,iy,ix+100,iy+100);
FillRect(hdc,&rect,(HBRUSH)GetStockObject(GRAY_BRUSH ));
SetRect(&rect,ix*2+100,iy,ix*2+200,iy+100);
FrameRect(hdc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));
SetRect(&rect,ix*3+200,iy,ix*3+300,iy+100);
hBrush = CreateSolidBrush(RGB(0,255,255));
SelectObject(hdc,(HBRUSH)hBrush);
Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
InvertRect(hdc,&rect);
OffsetRect(&rect,50,50);
//SetRect(&rect,ix*3+250,iy,ix*3+350,iy+100);
InvertRect(hdc,&rect);
OffsetRect(&rect,100,100);
OffsetRect(&rect,-320,10);
InflateRect(&rect,50,50);
FrameRect(hdc,&rect,(HBRUSH)GetStockObject(LTGRAY_BRUSH));
//SetRectEmpty(&rect);
isEmpty = IsRectEmpty(&rect);
wsprintf(szBuffer,TEXT("SetRectEmpty:\nx = %d \ny = %d \nIsRectEmpty:%d\n"),rect.left,rect.top,isEmpty);
//MessageBox(NULL,szBuffer,TEXT("test"),MB_OK);
GetClientRect(hWnd,&rect);
SetRect(&rect_src1,rect.right-200,rect.bottom-200,rect.right-10,rect.bottom-10);
SetRect(&rect_src2,rect.right-300,rect.bottom-300,rect.right-110,rect.bottom-110);
IntersectRect(&rect_dest1,&rect_src1,&rect_src2);
UnionRect(&rect_dest2,&rect_src1,&rect_src2);
FillRect(hdc,&rect_src1,(HBRUSH)GetStockObject(BLACK_BRUSH));
FillRect(hdc,&rect_src2,(HBRUSH)GetStockObject(WHITE_BRUSH));
FillRect(hdc,&rect_dest1,(HBRUSH)GetStockObject(DKGRAY_BRUSH));
FrameRect(hdc,&rect_dest2,(HBRUSH)GetStockObject(DKGRAY_BRUSH));
EndPaint(hWnd,&ps);
break;
case WM_LBUTTONDOWN:
break;
case WM_MOUSEMOVE:
GetClientRect(hWnd,&rect);
rect.left = rect.right-200;
rect.top = rect.bottom-200;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
if(PtInRect(&rect,pt))
{
MessageBox(hWnd,TEXT("滑鼠移動到選擇區域了"),TEXT("測試"),MB_OK);
}
break;
default:
bRet = false;
break;
}
return bRet;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR szCmdLine,
int nCmdLine)
{
DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DlgMainProc);
return 0;
}