设计一鼠标应用程序,当按下Ctrl键的同时按下鼠标左键,在窗口中拖动鼠标,可以画一个圆;在按下shift键的同时按下鼠标左键,在窗口中拖动鼠标,可以画一个矩形.
#include<windows.h> #include<stdlib.h> #include<string.h> long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG IParam); BOOL InitWindowsClass(HINSTANCE hInstance); BOOL InitWindows(HINSTANCE hInstance, int nCmdShow); HWND hWndMain; RECT rectl; int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { MSG Message; if(! InitWindowsClass(hInstance)) return FALSE; if(! InitWindows(hInstance,nCmdShow)) return FALSE; while(GetMessage(&Message,0,0,0)) { TranslateMessage(&Message); DispatchMessage(&Message); } return Message.wParam; } long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG IParam) { HDC hDC; WORD x,y; HCURSOR hCusor; static BOOL bCircle=FALSE,bRect=FALSE; PAINTSTRUCT ps; x=LOWORD(IParam); y=HIWORD(IParam); switch(iMessage) { case WM_LBUTTONDOWN: if(wParam&MK_CONTROL) { bCircle=TRUE; bRect=FALSE; rectl.left=x; rectl.top=y; } else if(wParam&MK_SHIFT) { bRect=TRUE; bCircle=FALSE; rectl.left =x; rectl.top =y; } else { bRect= FALSE; bCircle=FALSE; } break; case WM_LBUTTONUP: bRect=FALSE; bCircle=FALSE; break; case WM_MOUSEMOVE: rectl.right =x; rectl.bottom=y; if(bRect==TRUE||bCircle==TRUE) { InvalidateRect(hWnd,NULL,TRUE); } break; case WM_PAINT: hDC=BeginPaint(hWnd,&ps); if(bCircle==TRUE) { Ellipse(hDC,rectl.left,rectl.top,rectl.right,rectl.bottom); } if(bRect ==TRUE) { Rectangle(hDC,rectl.left,rectl.top,rectl.right,rectl.bottom); } EndPaint(hWnd,&ps); break; case WM_DESTROY: PostQuitMessage(0); return 0; default : return(DefWindowProc(hWnd,iMessage,wParam,IParam)); } return 0; } BOOL InitWindows(HINSTANCE hInstance,int nCmdShow) { HWND hWnd; hWnd=CreateWindow("6-5zz", "a mouse application programe!", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if(! hWnd) return FALSE; hWndMain=hWnd; ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); return TRUE; } BOOL InitWindowsClass(HINSTANCE hInstance) { 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_APPLICATION); WndClass.hInstance=hInstance; WndClass.lpfnWndProc =WndProc; WndClass.lpszClassName ="6-4"; WndClass.lpszMenuName =NULL; WndClass.style =0; return RegisterClass(&WndClass); }
|