SetCapture

来源:互联网 发布:名片设计软件 编辑:程序博客网 时间:2024/05/14 03:00
 一般,只有鼠标在CWnd的客户区内,你才能接受到鼠标消息。调用SetCapture后,即使鼠标移动出客户区,你也可以接受到鼠标消息。不过系统中只能有一个程序调用SetCapture,所以你需要在不使用时调用ReleaseCapture释放。而GetCapture可以知道当前哪个窗口调用了SetCapture。
  1. BLOKOUT2.C   
  2.   /*----------------------------------------------------------------------------   
  3.         BLOKOUT2.C   --   Mouse   Button   &   Capture   Demo   Program   
  4.   (c)   Charles   Petzold,   1998   
  5.   ----------------------------------------------------------------------------*/   
  6.     
  7.   #include   <windows.h>   
  8.   LRESULT   CALLBACK   WndProc   (HWND,   UINT,   WPARAM,   LPARAM)   ;   
  9.   int   WINAPI   WinMain   (HINSTANCE   hInstance,   HINSTANCE   hPrevInstance,   
  10.   PSTR   szCmdLine,   int   iCmdShow)   
  11.   {   
  12.             static   TCHAR   szAppName[]   =   TEXT   ("BlokOut2")   ;   
  13.             HWND                   hwnd   ;   
  14.             MSG                     msg   ;   
  15.             WNDCLASS           wndclass   ;   
  16.     
  17.           wndclass.style     =   CS_HREDRAW   |   CS_VREDRAW   ;   
  18.             wndclass.lpfnWndProc       =   WndProc   ;   
  19.             wndclass.cbClsExtra         =   0   ;   
  20.             wndclass.cbWndExtra         =   0   ;   
  21.             wndclass.hInstance           =   hInstance   ;   
  22.             wndclass.hIcon                   =   LoadIcon   (NULL,   IDI_APPLICATION)   ;   
  23.     wndclass.hCursor               =   LoadCursor   (NULL,   IDC_ARROW)   ;   
  24.             wndclass.hbrBackground   =   (HBRUSH)   GetStockObject   (WHITE_BRUSH)   ;   
  25.             wndclass.lpszMenuName     =   NULL   ;   
  26.             wndclass.lpszClassName   =   szAppName   ;   
  27.               
  28.   if   (!RegisterClass   (&wndclass))   
  29.             {   
  30.       MessageBox   ( NULL,   TEXT   ("Program   requires   Windows   NT!"),     
  31.   szAppName,   MB_ICONERROR)   ;   
  32.   return   0   ;   
  33.     }   
  34.     
  35.             hwnd   =   CreateWindow   ( szAppName,   TEXT   ("Mouse   Button   &   Capture   Demo"),   
  36.   WS_OVERLAPPEDWINDOW,   
  37.                                           CW_USEDEFAULT,   CW_USEDEFAULT,   
  38.                                           CW_USEDEFAULT,   CW_USEDEFAULT,   
  39.                                           NULL,   NULL,   hInstance,   NULL)   ;   
  40.               
  41.             ShowWindow   (hwnd,   iCmdShow)   ;   
  42.             UpdateWindow   (hwnd)   ;   
  43.               
  44.             while   (GetMessage   (&msg,   NULL,   0,   0))   
  45.             {   
  46.                       TranslateMessage   (&msg)   ;   
  47.                       DispatchMessage   (&msg)   ;   
  48.             }   
  49.             return   msg.wParam   ;   
  50.   }   
  51.     
  52.   void   DrawBoxOutline   (HWND   hwnd,   POINT   ptBeg,   POINT   ptEnd)   
  53.   {   
  54.             HDC   hdc   ;   
  55.             hdc   =   GetDC   (hwnd)   ;   
  56.             SetROP2   (hdc,   R2_NOT)   ;   
  57.             SelectObject   (hdc,   GetStockObject   (NULL_BRUSH))   ;   
  58.             Rectangle   (hdc,   ptBeg.x,   ptBeg.y,   ptEnd.x,   ptEnd.y)   ;   
  59.               
  60.             ReleaseDC   (hwnd,   hdc)   ;   
  61.   }   
  62.     
  63.   LRESULT   CALLBACK   WndProc   ( HWND   hwnd,   UINT   message,   WPARAM   wParam,LPARAM   lParam)   
  64.   {   
  65.             static   BOOL     fBlocking,   fValidBox   ;   
  66.             static   POINT   ptBeg,   ptEnd,   ptBoxBeg,   ptBoxEnd   ;   
  67.             HDC                     hdc   ;   
  68.             PAINTSTRUCT     ps   ;   
  69.               
  70.             switch   (message)   
  71.             {   
  72.             case   WM_LBUTTONDOWN   :   
  73.                       ptBeg.x   =   ptEnd.x   =   LOWORD   (lParam)   ;   
  74.                       ptBeg.y   =   ptEnd.y   =   HIWORD   (lParam)   ;   
  75.                         
  76.                       DrawBoxOutline   (hwnd,   ptBeg,   ptEnd)   ;   
  77.     
  78.                       SetCapture   (hwnd)   ;   
  79.                       SetCursor   (LoadCursor   (NULL,   IDC_CROSS))   ;   
  80.                         
  81.                       fBlocking   =   TRUE   ;   
  82.                       return   0   ;   
  83.                         
  84.             case   WM_MOUSEMOVE   :   
  85.                       if   (fBlocking)   
  86.                       {   
  87.                                 SetCursor   (LoadCursor   (NULL,   IDC_CROSS))   ;   
  88.                                   
  89.                                 DrawBoxOutline   (hwnd,   ptBeg,   ptEnd)   ;   
  90.                                   
  91.                                 ptEnd.x   =   LOWORD   (lParam)   ;   
  92.                                 ptEnd.y   =   HIWORD   (lParam)   ;   
  93.                                   
  94.                                 DrawBoxOutline   (hwnd,   ptBeg,   ptEnd)   ;   
  95.                       }   
  96.                   return   0   ;   
  97.                         
  98.             case   WM_LBUTTONUP   :   
  99.                       if   (fBlocking)   
  100.                       {   
  101.                                 DrawBoxOutline   (hwnd,   ptBeg,   ptEnd)   ;   
  102.                                   
  103.                                 ptBoxBeg       =   ptBeg   ;   
  104.                                 ptBoxEnd.x   =   LOWORD   (lParam)   ;   
  105.                                 ptBoxEnd.y   =   HIWORD   (lParam)   ;   
  106.                                   
  107.                                 ReleaseCapture   ()   ;   
  108.                                 SetCursor   (LoadCursor   (NULL,   IDC_ARROW))   ;   
  109.                                   
  110.                                 fBlocking   =   FALSE   ;   
  111.                                 fValidBox     =   TRUE   ;   
  112.                                   
  113.                                 InvalidateRect   (hwnd,   NULL,   TRUE)   ;   
  114.                       }   
  115.                       return   0   ;   
  116.                         
  117.             case   WM_CHAR   :   
  118.                       if   (fBlocking   &   wParam   ==   '/x1B')               //   i.e.,   Escape   
  119.                       {   
  120.                                 DrawBoxOutline   (hwnd,   ptBeg,   ptEnd)   ;   
  121.                                 ReleaseCapture   ()   ;   
  122.                                 SetCursor   (LoadCursor   (NULL,   IDC_ARROW))   ;   
  123.     
  124.                                 fBlocking   =   FALSE   ;   
  125.                       }   
  126.                       return   0   ;   
  127.                         
  128.             case   WM_PAINT   :   
  129.                       hdc   =   BeginPaint   (hwnd,   &ps)   ;   
  130.                         
  131.                       if   (fValidBox)   
  132.                       {   
  133.                                 SelectObject   (hdc,   GetStockObject   (BLACK_BRUSH))   ;   
  134.                                 Rectangle   (hdc,   ptBoxBeg.x,   ptBoxBeg.y,   
  135.                                           ptBoxEnd.x,   ptBoxEnd.y)   ;   
  136.                       }   
  137.                         
  138.                       if   (fBlocking)   
  139.                       {   
  140.                                 SetROP2   (hdc,   R2_NOT)   ;   
  141.                                 SelectObject   (hdc,   GetStockObject   (NULL_BRUSH))   ;   
  142.                                 Rectangle   (hdc,   ptBeg.x,   ptBeg.y,   ptEnd.x,   ptEnd.y)   ;   
  143.                       }   
  144.                         
  145.                       EndPaint   (hwnd,   &ps)   ;   
  146.                       return   0   ;   
  147.                         
  148.             case   WM_DESTROY   :   
  149.                       PostQuitMessage   (0)   ;   
  150.                       return   0   ;   
  151.             }   
  152.             return   DefWindowProc   (hwnd,   message,   wParam,   lParam)   ;   
  153.   }   
  154.   摘自《Windows   Programming》

原创粉丝点击