当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>Visual C++教程

VC中对鼠标的确定及简单控制

 前几天看一网友关于 按键精灵 的讨论。 其实质是控制鼠标进行模仿动作。现给出主要实现代码及工作结构。但仅适合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);
}

  以上是我们全文之重点。。主要的对鼠标的操作。
相关内容
赞助商链接