VC中对鼠标的确定及简单控制
2010-05-25 20:36:01 来源:WEB开发网一个窗口程序最重要的环节了。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);
}
以上是我们全文之重点。。主要的对鼠标的操作。
更多精彩
赞助商链接