1.WM_LBUTTONDBLCLK 双击鼠标左键;
2.WM_LBUTTONDOWN 单击鼠标左键;
3.WM_LBUTTONUP 松开鼠标左键;
4.鼠标中键及右键分别将上述L替换为M、R;
5.WM_MOUSEMOVE 鼠标移动消息;
6.WM_MOLSEWHEEL 鼠标滚轮消息;
1.lParam:其参数值分为高位字节与低位字节,低位字节存储鼠标光标的X坐标值,高位字节存储Y坐标值;
WORD LOWORD(lParam 参数);
WORD HIWORD(lParam 参数);
2.wParam:记录鼠标按键及Ctrl、Shift键,通过wParam与测试标志的与操作判断按键是否按下;
测试标志:MK_L/M/RBUTTON(左中右)、MK_SHIFT、MK_CONTROL
3.当处理滚轮消息时,1不变,wParam低位字节存储按键的状态信息,高位字节是120或-120,表示向前或向后滚动;
1.HWND SetCapture(HWND hwnd);
功能:获取窗口外的鼠标消息;
2.BOOL SetCursorPos(int x轴坐标,int y轴坐标);
功能:设定鼠标光标位置,这里的位置是相对于屏幕左上角的坐标;
3.BOOL ClientToScreen(HWND hwnd,LPPOINT 窗口点坐标);
功能:窗口点坐标转换为屏幕坐标,然后再使用SetCursorPos函数;
4.BOOL ScreenToClient(HWND hwnd,LPPOINT 屏幕点坐标);
功能:把屏幕坐标转换为窗口坐标;
5. int ShowCursor(BOOL true或false);
功能:隐藏及显示鼠标光标;
6.BOOL ClipCursor(CONST RECT 移动区域矩形);
功能:限制鼠标光标的移动区域,解除限制则参数设为NULL;
7.BOOL GetWindowRect(HWND hwnd,LPRECT 矩形结构);
功能:取得窗口外部区域矩形;
8.BOOL GetClientRect(HWND hwnd,LPRECT 矩形结构);
功能:取得窗口内部区域矩形;
1.一开始未按鼠标左键,则进行背景图的贴图并实现背景循环;
2.移动鼠标触发消息处理函数,获取鼠标光标的位置后,在贴图函数中,根据鼠标光标位置,确定飞机的贴图坐标,为了产生移
动效果,让飞机的坐标缓缓接近鼠标光标;
2.单击鼠标左键,消息处理函数处理该消息,设置第一颗子弹的贴图坐标(因为声明的子弹结构体是全局变量,所以它的成员变量
exist默认被初始化为0),然后回到贴图函数,先贴背景,再贴飞机,最后贴第一颗子弹,现在若不进行任何操作,则在主函数
内继续循环,该子弹的贴图坐标每次循环横坐标都要减10,直到小于0;
3.在第一颗子弹未消失之前,继续单击鼠标左键,此时实现的是两颗子弹的贴图,一直循环一直贴,直到消失;
#include "stdafx.h" #include <stdio.h> struct BULLET//定义子弹结构体 { int x,y; bool exist; }; HINSTANCE hInst; HBITMAP bg,ship,bullet; HDC hdc,mdc,bufdc; HWND hWnd; DWORD tPre,tNow; int x,y,nowX,nowY;//光标坐标,飞机贴图坐标 int w=0,bcount;//w为滚动背景所要裁剪的宽度;记录飞机现有的子弹数目 BULLET b[30];//存储飞机发出的子弹 ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void MyPaint(HDC hdc); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; MyRegisterClass(hInstance); if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { tNow = GetTickCount(); if(tNow-tPre >= 40) MyPaint(hdc); } } return msg.wParam; } ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = NULL; wcex.hCursor = NULL; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = "canvas"; wcex.hIconSm = NULL; return RegisterClassEx(&wcex); } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HBITMAP bmp; POINT pt,lt,rb; RECT rect; hInst = hInstance; hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } MoveWindow(hWnd,100,100,640,480,true); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); hdc = GetDC(hWnd); mdc = CreateCompatibleDC(hdc); bufdc = CreateCompatibleDC(hdc); bmp = CreateCompatibleBitmap(hdc,640,480); SelectObject(mdc,bmp); bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,648,480,LR_LOADFROMFILE); ship = (HBITMAP)LoadImage(NULL,"ship.bmp",IMAGE_BITMAP,100,148,LR_LOADFROMFILE); bullet = (HBITMAP)LoadImage(NULL,"bullet.bmp",IMAGE_BITMAP,10,20,LR_LOADFROMFILE); x = 300; y = 300; nowX = 300; nowY = 300; ////设定鼠标光标位置 pt.x = 300; pt.y = 300; ClientToScreen(hWnd,&pt);//把窗口坐标转化为屏幕坐标,因为SetCursorPos只认屏幕坐标 SetCursorPos(pt.x,pt.y); ShowCursor(false); //隐藏鼠标光标 //限制鼠标光标移动区域 GetClientRect(hWnd,&rect);//获取客户区窗口的大小 lt.x = rect.left; lt.y = rect.top; rb.x = rect.right; rb.y = rect.bottom; ClientToScreen(hWnd,<); ClientToScreen(hWnd,&rb); rect.left = lt.x; rect.top = lt.y; rect.right = rb.x; rect.bottom = rb.y; ClipCursor(&rect);//限制鼠标在矩形区域内 MyPaint(hdc); return TRUE; } void MyPaint(HDC hdc) { char str[20] = ""; int i; SelectObject(bufdc,bg); BitBlt(mdc,0,0,w,480,bufdc,640-w,0,SRCCOPY); BitBlt(mdc,w,0,640-w,480,bufdc,0,0,SRCCOPY); //飞机贴图缓慢向鼠标移动 if(nowX < x) { nowX += 10; if(nowX > x) nowX = x; } else { nowX -=10; if(nowX < x) nowX = x; } if(nowY < y) { nowY += 10; if(nowY > y) nowY = y; } else { nowY -= 10; if(nowY < y) nowY = y; } SelectObject(bufdc,ship); BitBlt(mdc,nowX,nowY,100,74,bufdc,0,74,SRCAND); BitBlt(mdc,nowX,nowY,100,74,bufdc,0,0,SRCPAINT); SelectObject(bufdc,bullet); if(bcount!=0) for(i=0;i<30;i++) if(b[i].exist) { BitBlt(mdc,b[i].x,b[i].y,10,10,bufdc,0,10,SRCAND); BitBlt(mdc,b[i].x,b[i].y,10,10,bufdc,0,0,SRCPAINT); b[i].x -= 10; if(b[i].x < 0) { bcount--; b[i].exist = false; } } sprintf(str,"X坐标%d ",x); TextOut(mdc,0,0,str,strlen(str)); sprintf(str,"Y坐标%d ",y); TextOut(mdc,0,20,str,strlen(str)); BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY); tPre = GetTickCount(); w += 10; if(w==640) w = 0; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int i; switch (message) { case WM_KEYDOWN: if(wParam==VK_ESCAPE) //只要按下一次鼠标左键,都会从(nowX,nowY+30)出现一颗子弹 PostQuitMessage(0); break; case WM_LBUTTONDOWN: for(i=0;i<30;i++) { if(!b[i].exist) { b[i].x = nowX; b[i].y = nowY + 30; b[i].exist = true; bcount++; break; } } case WM_MOUSEMOVE: x = LOWORD(lParam); if(x > 530) x = 530; else if(x < 0) x = 0; y = HIWORD(lParam); if(y > 380) y = 380; else if(y < 0) y = 0; break; case WM_DESTROY: ClipCursor(NULL); DeleteDC(mdc); DeleteDC(bufdc); DeleteObject(bg); DeleteObject(bullet); DeleteObject(ship); ReleaseDC(hWnd,hdc); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }