鼠标的按钮操作,主要以下三种
1单击:然下鼠标,然后松开
2双击:连续两次快速按下鼠标按钮,然后松开
3保持按下按钮,并移动鼠标
下面看看按钮事件:
|
按钮 |
按下 |
释放 |
第二次按下按钮 |
|
左键 |
WM_LBUTTONDOWN |
WM_LBUTTONUP |
WM_LBUTTONDBLCLK |
|
中键 |
WM_MBUTTONDOWN |
WM_MBUTTONUP |
WM_MBUTTONDBLCLK |
|
右键 |
WM_RBUTTONDOWN |
WM_RBUTTONUP |
WM_RBUTTONDBLCLK |
下面是一个简单的鼠标处理事例:
#include<windows.h>
#include<windowsx.h>//GET_X_LPARAM (lParam)此类函数在这个头文件里
#define MAXPOINTS 1000
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("leidemingzi");
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_ERROR);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WindowProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("the program require window NT"),szAppName,MB_ICONERROR);
return 0;
}
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;
static int iCount;
static POINT pt[MAXPOINTS];
int i,j;
switch(uMsg)
{
case WM_LBUTTONDOWN:
iCount=0;
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_MOUSEMOVE:
if(wParam & MK_LBUTTON & (iCount<1000))//很重要喔
{
pt[iCount ].x = GET_X_LPARAM (lParam) ;
pt[iCount++].y = GET_Y_LPARAM (lParam) ;
hdc = GetDC (hwnd) ;
SetPixel (hdc, GET_X_LPARAM (lParam), GET_Y_LPARAM (lParam), 0) ;
ReleaseDC (hwnd, hdc) ;
}
return 0;
case WM_LBUTTONUP:
InvalidateRect(hwnd,NULL,FALSE);//使矩形区域无效,触动WM_PAINT事件,并且WM_PAINT事件的时候,不删除原来的区域内容
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SetCursor(LoadCursor(NULL,IDC_WAIT));//把鼠标设置为忙碌状态
ShowCursor(TRUE);//显示鼠标
for (i = 0 ; i < iCount - 1 ; i++)
for (j = i + 1 ; j < iCount ; 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);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
其中的一个效果图如下:


被折叠的 条评论
为什么被折叠?



