前幾天看一網友關於 按鍵精靈 的討論。 其實質是控制鼠標進行模仿動作。現給出主要實現代碼及工作結構。但僅適合Win32編程初學者,高手就不用進了。
LRESULT APIENTRY WndProc(HWND, UINT, WPARAM, LPARAM); //窗口函數
BOOL ckzc(HINSTANCE); //注冊窗口類函數
BOOL ckcj(HINSTANCE, int); //創建窗口函數
HINSTANCE hInst; //當前句柄
HWND hWndMain; //窗口句柄
上面首先是window窗口函數聲明。然後依照慣例注冊窗口類函數和創建窗口函數。通過關鍵字HINSTANCE、HWND分別獲得當前句柄、窗口句柄。
BOOL ckzc(HINSTANCE hInstance)
{
WNDCLASSEX wcexMouse;
wcexMouse.cbSize = sizeof(WNDCLASSEX);
wcexMouse.style = CS_DBLCLKS; //支持鼠標雙擊
wcexMouse.lpfnWndProc = (WNDPROC)MainWndProc;
wcexMouse.cbClsExtra = 0;
wcexMouse.cbWndExtra = 0;
wcexMouse.hInstance = hInstance;
wcexMouse.hIcon = LoadIcon(NULL,IDI_WINLOGO);
wcexMouse.hCursor = LoadCursor(NULL,IDC_ARROW);
wcexMouse.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wcexMouse.lpszMenuName = NULL;
wcexMouse.lpszClassName = "MouseWClass";
wcexMouse.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
//為了保證程序WIN98/NT的兼容性和可移植性,用以下對窗口進行注冊
if (!RegisterClassEx (&wcexMouse))
{
if (!RegisterClass((LPWNDCLASS)&wcexMouse.style))
MessageBox (NULL, TEXT ("注冊窗口類失敗!"), "wcexMouse", MB_ICONERROR);
return (FALSE);
}
return (TRUE);
}
進行window類注冊。
BOOL ckcj(HINSTANCE hInstance,int nCmdShow)
{
hInst = hInstance;
hWndMain = CreateWindow("MouseWClass",
"我的鼠標---cnscom",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
if(!hWndMain)
return (FALSE);
ShowWindow(hWndMain,nCmdShow); //顯示窗口
UpdateWindow(hWndMain); //刷新窗口
return (TRUE);
}
窗口創建工作。
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if(!ckzc(hInstance)) //注冊窗口類
return (FALSE);
if(!ckcj(hInstance,nCmdShow)) //創建窗口
return (FALSE);
InvalidateRect(hWndMain, NULL, TRUE); //按指定區域更新區域
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
一個窗口程序最重要的環節了。WinMain對以上我們建立的函數進行了總匯。
LRESULT APIENTRY WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDc, hMemDc;
PAINTSTRUCT ps;
DWord fwKeys;
POINT ptMouse;
char szTitle[100];
static RECT rect1, rect2;
static HCURSOR hCursorPen;
switch(message)
{
case WM_CREATE:
rect1.left = 90;
rect1.right = 170;
rect1.top = 8;
rect1.bottom = 68;
rect2.left = 90;
rect2.right = 170;
rect2.top = 78;
rect2.bottom = 138;
hCursorPen = LoadCursor(hInst, "RCCURSOR");
break;
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MOUSEMOVE:
ptMouse.x = LOWord(lParam); //取lParam低字節為橫坐標x
ptMouse.y = HIWord(lParam); //取lParam高字節為橫坐標y
fwKeys = wParam; //將鼠標按鈕和鍵盤組合賦給32位無符號整數fwKeys
//很喜歡各個游戲中很有特色的鼠標吧。下面的代碼就可以幫助你實現
//判斷是否位於窗口之中否則,采用普通的箭頭鼠標。
if (PtInRect(&rect1, ptMouse) || PtInRect(&rect2, ptMouse)) //判斷指定點是否在指定窗口內
SetCursor(hCursorPen);
//在標題欄上顯示鼠標的當前位置和按鍵和按鍵信息
//把輸出格式和輸出變量送到szTitle數組
wsprintf(szTitle, "鼠標測試程序 ---- 鼠標位置:[%d,%d]", ptMouse.x, ptMouse.y);
if (fwKeys!=0)
{
//把指定的內容輸出到szTitle數組中
lstrcat(szTitle, " ---- 您按下了");
if(fwKeys & MK_CONTROL)
lstrcat(szTitle, "Ctrl ");
if(fwKeys & MK_SHIFT)
lstrcat(szTitle, "Shift ");
if(fwKeys & MK_LBUTTON)
lstrcat(szTitle, "鼠標左按鈕");
if(fwKeys & MK_RBUTTON)
lstrcat(szTitle, "鼠標右按鈕");
lstrcat(szTitle, "鍵");
}
SetWindowText(hWnd, szTitle);
break;
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
//如果鼠標雙擊了窗口客戶區,那麼就顯示坐標信息
hDc = GetDC(hWnd);
//設置輸出字體顏色
SetTextColor(hDc, (message == WM_LBUTTONDBLCLK) ? RGB(255, 0, 0) : RGB(0, 0, 255));
wsprintf(szTitle, "(%d,%d)", LOWORD(lParam), HIWord(lParam));
TextOut(hDc, LOWORD(lParam), HIWord(lParam), szTitle, lstrlen(szTitle)); //在指定位置輸出文本
ReleaseDC(hWnd, hDc);
break;
case WM_PAINT:
hDc = BeginPaint(hWnd, &ps);
hMemDc = CreateCompatibleDC(hDc);
SelectObject(hMemDc, hSun);
BitBlt(hDc, 90, 8, 80, 60, hMemDc, 0, 0, SRCCOPY);
DeleteDC(hMemDc);
hMemDc = CreateCompatibleDC(hDc);
SelectObject(hMemDc, hMoon);
BitBlt(hDc, 90, 78, 80, 60, hMemDc, 0, 0, SRCCOPY);
DeleteDC(hMemDc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
DeleteObject(hSun);
DeleteObject(hMoon);
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (0);
}
以上是我們全文之重點。。主要的對鼠標的操作。注釋很完全,不多羅嗦了。