第四讲鼠标.docx
《第四讲鼠标.docx》由会员分享,可在线阅读,更多相关《第四讲鼠标.docx(35页珍藏版)》请在冰豆网上搜索。
![第四讲鼠标.docx](https://file1.bdocx.com/fileroot1/2023-2/3/67e14c55-ada4-44b2-b55e-2932f5af070d/67e14c55-ada4-44b2-b55e-2932f5af070d1.gif)
第四讲鼠标
第四讲:
鼠标
●只要鼠标跨越窗口,或者在某窗口中按下鼠标按钮,那么窗口过程函数就会收到鼠标消息,而不管该窗口是否为活动窗口和是否拥有输入焦点窗口。
●鼠标消息:
1.lParam是相对于客户区左上角的坐标,其中
X坐标:
LOWORD(lParam)
Y坐标:
HIWORD(lParam)
2.wParam是Shift键和Ctrl键或鼠标按钮的状态,若
wParam&MK_SHIFT>0
wParam&MK_CONTROL>0
wParam&MK_LBUTTON>0
wParam&MK_MBUTTON>0
wParam&MK_RBUTTON>0
表示在产生相应的鼠标消息时,也按下了Shift键和Ctrl键或鼠标按钮。
WM_LBUTTONDOWN
WM_MBUTTONDOWN
WM_RBUTTONDOWN
WM_LBUTTONUP
WM_RBUTTONUP
WM_MBUTTONUP
WM_LBUTTONDBLCLK
WM_MBUTTONDBLCLK
WM_RBUTTONDBLCLK
WM_MOUSEMOVE
●鼠标双击消息
如果希望窗口过程函数能接受鼠标双击消息,那么在注册窗口类时,窗口风格应为:
wndclass.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
●重点:
鼠标消息:
WM_LBUTTONDOWN
WM_MBUTTONDOWN
WM_RBUTTONDOWN
WM_LBUTTONUP
WM_RBUTTONUP
WM_MBUTTONUP
WM_LBUTTONDBLCLK
WM_MBUTTONDBLCLK
WM_RBUTTONDBLCLK
WM_MOUSEMOVE
子窗口风格:
WS_CHILDWINDOW|WS_VISIBLE
取程序实例句柄:
(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);
函数MoveWindow(hwndChild[x][y],x*cxBlock,y*cyBlock,cxBlock,cyBlock,TRUE);移动窗口和改变窗口大小尺寸,产生WM_SIZE消息。
存取预留在窗口额外字节的函数:
SetWindowLong(hwnd,0,0);
GetWindowLong(hwnd,0,0);
设置窗口捕获鼠标函数:
SetCapture(hwnd);一旦窗口hwnd被设置了捕获鼠标,不管鼠标光标是否在窗口hwnd的边界之内,窗口hwnd都接受鼠标输入。
释放窗口捕获鼠标函数:
ReleaseCapture();
WM_MOUSEMOVE消息:
每当鼠标移动时,窗口接收WM_MOUSEMOVE消息,系统此时自动把鼠标光标形状切换到在窗口类中定义的鼠标光标形状,如
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
/*--------------------------------------------------
CONNECT.C--Connect-the-DotsMouseDemoProgram
(c)CharlesPetzold,1998
--------------------------------------------------*/
#include
#defineMAXPOINTS1000
LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);
intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,
PSTRszCmdLine,intiCmdShow)
{
staticTCHARszAppName[]=TEXT("Connect");
HWNDhwnd;
MSGmsg;
WNDCLASSwndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
if(!
RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("ProgramrequiresWindowsNT!
"),
szAppName,MB_ICONERROR);
return0;
}
hwnd=CreateWindow(szAppName,TEXT("Connect-the-PointsMouseDemo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
returnmsg.wParam;
}
LRESULTCALLBACKWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
{
staticPOINTpt[MAXPOINTS];
staticintiCount;
HDChdc;
inti,j;
PAINTSTRUCTps;
switch(message)
{
caseWM_LBUTTONDOWN:
iCount=0;
InvalidateRect(hwnd,NULL,TRUE);
return0;
caseWM_MOUSEMOVE:
if(wParam&MK_LBUTTON&&iCount<1000)
{
pt[iCount].x=LOWORD(lParam);
pt[iCount++].y=HIWORD(lParam);
hdc=GetDC(hwnd);
//SetPixel(hdc,LOWORD(lParam),HIWORD(lParam),0);
SetPixel(hdc,LOWORD(lParam),HIWORD(lParam),RGB(255,0,0));
ReleaseDC(hwnd,hdc);
}
return0;
caseWM_LBUTTONUP:
InvalidateRect(hwnd,NULL,FALSE);
return0;
caseWM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SetCursor(LoadCursor(NULL,IDC_WAIT));
ShowCursor(TRUE);
for(i=0;ifor(j=i+1;j{
MoveToEx(hdc,pt[i].x,pt[i].y,NULL);
LineTo(hdc,pt[j].x,pt[j].y);
}
ShowCursor(FALSE);
SetCursor(LoadCursor(NULL,IDC_ARROW));
EndPaint(hwnd,&ps);
return0;
caseWM_DESTROY:
PostQuitMessage(0);
return0;
}
returnDefWindowProc(hwnd,message,wParam,lParam);
}
/*-------------------------------------------------
CHECKER1.C--MouseHit-TestDemoProgramNo.1
(c)CharlesPetzold,1998
-------------------------------------------------*/
#include
#defineDIVISIONS5
LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);
intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,
PSTRszCmdLine,intiCmdShow)
{
staticTCHARszAppName[]=TEXT("Checker1");
HWNDhwnd;
MSGmsg;
WNDCLASSwndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
if(!
RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("ProgramrequiresWindowsNT!
"),
szAppName,MB_ICONERROR);
return0;
}
hwnd=CreateWindow(szAppName,TEXT("Checker1MouseHit-TestDemo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
returnmsg.wParam;
}
LRESULTCALLBACKWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
{
staticBOOLfState[DIVISIONS][DIVISIONS];
staticintcxBlock,cyBlock;
HDChdc;
intx,y;
PAINTSTRUCTps;
RECTrect;
switch(message)
{
caseWM_SIZE:
cxBlock=LOWORD(lParam)/DIVISIONS;
cyBlock=HIWORD(lParam)/DIVISIONS;
return0;
caseWM_LBUTTONDOWN:
x=LOWORD(lParam)/cxBlock;
y=HIWORD(lParam)/cyBlock;
if(x{
fState[x][y]^=1;
rect.left=x*cxBlock;
rect.top=y*cyBlock;
rect.right=(x+1)*cxBlock;
rect.bottom=(y+1)*cyBlock;
InvalidateRect(hwnd,&rect,FALSE);
}
else
MessageBeep(0);
return0;
caseWM_PAINT:
hdc=BeginPaint(hwnd,&ps);
for(x=0;xfor(y=0;y{
Rectangle(hdc,x*cxBlock,y*cyBlock,
(x+1)*cxBlock,(y+1)*cyBlock);
if(fState[x][y])
{
MoveToEx(hdc,x*cxBlock,y*cyBlock,NULL);
LineTo(hdc,(x+1)*cxBlock,(y+1)*cyBlock);
MoveToEx(hdc,x*cxBlock,(y+1)*cyBlock,NULL);
LineTo(hdc,(x+1)*cxBlock,y*cyBlock);
}
}
EndPaint(hwnd,&ps);
return0;
caseWM_DESTROY:
PostQuitMessage(0);
return0;
}
returnDefWindowProc(hwnd,message,wParam,lParam);
}
/*-------------------------------------------------
CHECKER3.C--MouseHit-TestDemoProgramNo.3
(c)CharlesPetzold,1998
-------------------------------------------------*/
#include
#defineDIVISIONS5
LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);
LRESULTCALLBACKChildWndProc(HWND,UINT,WPARAM,LPARAM);
TCHARszChildClass[]=TEXT("Checker3_Child");
intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,
PSTRszCmdLine,intiCmdShow)
{
staticTCHARszAppName[]=TEXT("Checker3");
HWNDhwnd;
MSGmsg;
WNDCLASSwndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
if(!
RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("ProgramrequiresWindowsNT!
"),
szAppName,MB_ICONERROR);
return0;
}
wndclass.lpfnWndProc=ChildWndProc;
wndclass.cbWndExtra=sizeof(long);
wndclass.hIcon=NULL;
wndclass.lpszClassName=szChildClass;
RegisterClass(&wndclass);
hwnd=CreateWindow(szAppName,TEXT("Checker3MouseHit-TestDemo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
returnmsg.wParam;
}
LRESULTCALLBACKWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
{
staticHWNDhwndChild[DIVISIONS][DIVISIONS];
intcxBlock,cyBlock,x,y;
switch(message)
{
caseWM_CREATE:
for(x=0;xfor(y=0;yhwndChild[x][y]=CreateWindow(szChildClass,NULL,
WS_CHILDWINDOW|WS_VISIBLE,
0,0,0,0,
hwnd,(HMENU)(y<<8|x),
(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),
NULL);
return0;
caseWM_SIZE:
cxBlock=LOWORD(lParam)/DIVISIONS;
cyBlock=HIWORD(lParam)/DIVISIONS;
for(x=0;xfor(y=0;yMoveWindow(hwndChild[x][y],
x*cxBlock,y*cyBlock,
cxBlock,cyBlock,TRUE);
return0;
caseWM_LBUTTONDOWN:
MessageBeep(0);
return0;
caseWM_DESTROY:
PostQuitMessage(0);
return0;
}
returnDefWindowProc(hwnd,message,wParam,lParam);
}
LRESULTCALLBACKChildWndProc(HWNDhwnd,UINTmessage,
WPARAMwParam,LPARAMlParam)
{
HDChdc;
PAINTSTRUCTps;
RECTrect;
switch(message)
{
caseWM_CREATE:
SetWindowLong(hwnd,0,0);//on/offflag
return0;
caseWM_LBUTTONDOWN:
SetWindowLong(hwnd,0,1^GetWindowLong(hwnd,0));
InvalidateRect(hwnd,NULL,FALSE);
return0;
caseWM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
Rectangle(hdc,0,0,rect.right,rect.bottom);
if(GetWindowLong(hw