黄维通源码--对鼠标与键盘的响应

#include<windows.h>
#include<stdio.h>

long WINAPI WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
HWND hWndMain;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
 MSG Message;
 if(!InitWindowsClass(hInstance))
     return FALSE;
 if(!InitWindows(hInstance,nCmdShow))
     return FALSE;

 while(GetMessage(&Message,NULL,0,0))
    {
      TranslateMessage(&Message);
      DispatchMessage(&Message);
    }
 
 return Message.wParam;
}


BOOL InitWindowsClass(HINSTANCE hInstance)
{
 WNDCLASS wndclass;
 char lpszClassName[]="windows";

 wndclass.style=0;
 wndclass.lpfnWndProc=WndProc;
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.hInstance=hInstance;
 wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
   wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
// wndclass.hbrBackground=NULL;
// wndclass.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE + 1);
 wndclass.lpszMenuName=NULL;
 wndclass.lpszClassName=lpszClassName;

 if(!RegisterClass(&wndclass))
   {
     MessageBeep(0);
     return FALSE;
   }

 return TRUE;
}
 
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
 char lpszClassName[]="windows";
 char lpszTitle[]="eattonton";
 hWndMain=CreateWindow
       (
 lpszClassName,
 lpszTitle,
 WS_OVERLAPPEDWINDOW,
 CW_USEDEFAULT,
 CW_USEDEFAULT,
 CW_USEDEFAULT,
 CW_USEDEFAULT,
 NULL,
 NULL,
 hInstance,
 NULL
       );

 ShowWindow(hWndMain,nCmdShow);
 UpdateWindow(hWndMain);
 
  return TRUE;
}

 

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
 HDC hDC;
 PAINTSTRUCT ps;
 HPEN hPen;
 HBRUSH hBrush;
 //TEXTMETRIC tm;
 HCURSOR hcursor;
  char str[15];
 
 static int x,y;
 
 static char cUp[]="You had hit the UP key";
 static char cCtrl[]="You had hit the Ctrl key";
 static char cShift[]="You had hit the SHIFT key";
 static char cCtrl_A[]="You had hit the CTRL A key";
 static char cShift_B[]="You had hit the SHIFT B key";
 
 static BOOL nUpKeyDown=FALSE,
             nShiftKeyDown=FALSE,
             nCtrlKeyDown=FALSE,
             nCtrlAKeyDown=FALSE,
             nShiftBKeyDown=FALSE;
 
 switch(message)
   {
     case WM_MOUSEMOVE:
          x=LOWORD(lParam);
          y=HIWORD(lParam);
          if(x>50 && x<150 && y>50 && y<150)
          {
            hcursor=LoadCursor(NULL,IDC_CROSS);
            SetCursor(hcursor);
          }
          if(x>150 && x<250 && y>50 && y<150)
          {
            hcursor=LoadCursor(NULL,IDC_SIZEALL);
            SetCursor(hcursor);          
          }
          if(x>250 && x<350 && y>50 && y<150)
          {
            hcursor=LoadCursor(NULL,IDC_SIZENESW);
            SetCursor(hcursor);          
          }
          if(x>50 && x<150 && y>150 && y<250)
          {
            hcursor=LoadCursor(NULL,IDC_IBEAM);
            SetCursor(hcursor);          
          }
          if(x>150 && x<250 && y>150 && y<250)
          {
            hcursor=LoadCursor(NULL,IDC_WAIT);
            SetCursor(hcursor);          
          }
          if(x>250 && x<350 && y>150 && y<250)
          {
            hcursor=LoadCursor(NULL,IDC_UPARROW);
            SetCursor(hcursor);          
          }
          if(x>50 && x<150 && y>250 && y<350)
          {
            hcursor=LoadCursor(NULL,IDC_SIZEWE);
            SetCursor(hcursor);          
          }
          if(x>150 && x<250 && y>250 && y<350)
          {
            hcursor=LoadCursor(NULL,IDC_SIZENWSE);
            SetCursor(hcursor);          
          }
          if(x>250 && x<350 && y>250 && y<350)
          {
            hcursor=LoadCursor(NULL,IDC_SIZNS);
            SetCursor(hcursor);          
          }
          hDC=GetDC(hwnd);
          sprintf(str,"%d,%d/0",x,y);
          TextOut(hDC,5,5,str,strlen(str));
     case WM_KEYDOWN:
          {
            switch(wParam)
              {
                case VK_UP:
                     nUpKeyDown=TRUE;
                     break;
                case VK_SHIFT:
                     nShiftKeyDown=TRUE;
                     break;
                case VK_CONTROL:
                     nCtrlKeyDown=TRUE;
                     break;
                default:
                     break;
              }
          }
     case WM_KEYUP:
          InvalidateRect(hwnd,NULL,FALSE);
          break;
     case WM_CHAR:
          if(wParam==(65 & VK_CONTROL))
            {
              if(nCtrlKeyDown==TRUE)
                {
                  nCtrlKeyDown=TRUE;
                  nCtrlKeyDown=FALSE;
  }
             }
          else if(wParam==98 || wParam==66)
             {
               if(nShiftKeyDown==TRUE)
                 {
      nShiftBKeyDown=TRUE;
                    nShiftKeyDown=FALSE;
                 }    
      }
          break;
     case WM_PAINT:
          hDC=BeginPaint(hwnd,&ps);
          hBrush=(HBRUSH)GetStockObject(WHITE_BRUSH);
          hPen=(HPEN)GetStockObject(WHITE_PEN);
          SelectObject(hDC,hPen);
          SelectObject(hDC,hBrush);
          SetTextColor(hDC,RGB(255,0,0));
          if(nUpKeyDown==TRUE)
           {
             Rectangle(hDC,0,0,300,200);
             TextOut(hDC,0,0,cUp,strlen(cUp));
             nUpKeyDown=FALSE;
           }
          else if(nCtrlAKeyDown==TRUE)
           {
             Rectangle(hDC,0,0,300,200);
             TextOut(hDC,0,100,cCtrl_A,strlen(cCtrl_A));
             nCtrlAKeyDown=FALSE;
             nCtrlKeyDown=FALSE;
           }
          else if(nCtrlKeyDown==TRUE && nCtrlAKeyDown==FALSE)
           {
             Rectangle(hDC,0,0,300,200);
             TextOut(hDC,0,60,cCtrl,strlen(cCtrl));
             nCtrlKeyDown=FALSE;
           }
          else if(nShiftBKeyDown==TRUE)
           {
             Rectangle(hDC,0,0,300,200);
             TextOut(hDC,0,0,cShift_B,strlen(cShift_B));
             nShiftBKeyDown=FALSE;
             nShiftKeyDown=FALSE;            
           }
          else if(nShiftBKeyDown==FALSE && nShiftKeyDown==TRUE)
           {
             Rectangle(hDC,0,0,300,200);
             TextOut(hDC,0,0,cShift,strlen(cShift));
             nShiftKeyDown=FALSE;               
           }
          
           DeleteObject(hPen);
           DeleteObject(hBrush);
           EndPaint(hwnd,&ps);
           break;
     case WM_DESTROY:
          PostQuitMessage(0);
     default:
          return DefWindowProc(hwnd,message,wParam,lParam);
   }

 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值