实现Windows应用程序对键盘与鼠标的响应

 

源文件代码:

//windows.h文件中包含应用程序中所需的数据类型和数据结构的定义

#include"windows.h"

#include"实验二.h"

HMENU hmenu,haddmenu;

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);//窗口函数说明

//---------以下初始化窗口类--------

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)

{

       HWND hwnd;

       MSG Msg;

       HACCEL hAccel;

       WNDCLASS wndclass;

       char lpszMenuName[]="Menu";

       char lpszClassName[]="菜单";      //窗口类名

       char lpszTitle[]="E07620203 苏郑";    //窗口标题名-

 

 

       //窗口类的定义

       wndclass.style=0;                               //窗口类型为默认类型

       wndclass.lpfnWndProc=WndProc;              //窗口处理函数为WndProc

       wndclass.cbClsExtra=0;                           //窗口类无扩展

       wndclass.cbWndExtra=0;                           //窗口实例无扩展

       wndclass.hInstance=hInstance;                    //当前实例句柄

       wndclass.hIcon=LoadIcon(hInstance,"Icon");  //窗口的最小化图标为默认图标

       wndclass.hCursor=LoadCursor(hInstance,"Cursor");  //窗口采用箭头光标

       wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);   

//窗口背景为白色

       wndclass.lpszMenuName=lpszMenuName;             //窗口中无菜单

       wndclass.lpszClassName=lpszClassName;      //窗口类名为“窗口示例”

 

 

 

 

       //-----------以下进行窗口类的注册---------------

       if(!RegisterClass(&wndclass))             //如果注册失败则发出警告声音

       {

              MessageBeep(0);

              return FALSE;

       }

 

       //创建窗口

       hwnd=CreateWindow(

                            lpszClassName,              //窗口类名

                                     lpszTitle,                  //窗口实例的标题名

                                     WS_OVERLAPPEDWINDOW,        //窗口风格

                                     CW_USEDEFAULT,

                                     CW_USEDEFAULT,        //窗口左上角坐标为默认值

                                     CW_USEDEFAULT,

                                     CW_USEDEFAULT,           //窗口的高和宽为默认值

                                     NULL,                       //此窗口无父窗口

                                     NULL,                       //次窗口无主菜单

                                     hInstance,            //创建此窗口应用程序的当前句柄

                                     NULL                        //不使用该值

                                     );

       ShowWindow(hwnd,nCmdShow);                   //显示窗口

       UpdateWindow(hwnd);                          //绘制用户区

    hAccel=LoadAccelerators(hInstance,lpszMenuName);//加载加速键资源

       while(GetMessage(&Msg,NULL,0,0))             //消息循环

       {

                     if(!TranslateAccelerator(hwnd,hAccel,&Msg))

              {

                     TranslateMessage(&Msg);

                     DispatchMessage(&Msg);

              }

       }

       return Msg.wParam;

}

 

 

 

//窗口函数

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)

{

       static bool draw = false;

       static bool pingjunzhiadd = true;

       static bool left = true;

       HDC hdc;

       PAINTSTRUCT ps;

       HPEN hp;

       HBRUSH hb;

       //RECT rect;

//     int i;

      

       switch(message)

       {

       case WM_COMMAND:

              switch(LOWORD(wParam))

              {

      

              case IDM_EXIT:

                     SendMessage(hwnd,WM_DESTROY,0,0);

                     break;

             

              case IDM_fangchamodify:

                    

                     ModifyMenu(hmenu,IDM_fangcha,MF_BYCOMMAND,IDM_nihe,"线性拟合");

                     DrawMenuBar(hwnd);

                     break;

 

              case IDM_pingjunzhiadd:

           if(pingjunzhiadd)

                 {

            InsertMenu(hmenu,IDM_junfanggen,MF_BYCOMMAND|MF_ENABLED,IDM_pingjunzhi,"平均值");

            DrawMenuBar(hwnd);

                 }

                     pingjunzhiadd = false;

                 break;

 

              case IDM_DRAW:

                     hmenu = GetMenu(hwnd);

                     draw = true;

                     break;

             

              }

              break;

      

       case WM_LBUTTONDOWN:

              if(left)

              {

             

           hmenu=GetMenu(hwnd);

              haddmenu=CreateMenu();

              AppendMenu(haddmenu,MF_GRAYED,IDM_zonghedelete,"删除计算总和");

              AppendMenu(haddmenu,MF_GRAYED,IDM_fangchamodify,"修改计算方差");

              AppendMenu(haddmenu,MF_GRAYED,IDM_pingjunzhiadd,"添加计算平均值");

              InsertMenu(hmenu,2,MF_POPUP|MF_BYPOSITION,(UINT)haddmenu,"增加(&C)");

        DrawMenuBar(hwnd);

             

              }

              left = false;

 

              break;

             

       case WM_RBUTTONDOWN:

 

              EnableMenuItem(haddmenu,IDM_zonghedelete,MF_ENABLED);

              EnableMenuItem(haddmenu,IDM_fangchamodify,MF_ENABLED);

              EnableMenuItem(haddmenu,IDM_pingjunzhiadd,MF_ENABLED);

              DrawMenuBar(hwnd);

              break;

 

       case WM_PAINT:

              if(draw)

              {

                     hdc = BeginPaint (hwnd, &ps);

                     SetMapMode(hdc,MM_TEXT);

                     Rectangle(hdc,130,60,270,200);

                     hb=CreateHatchBrush(HS_CROSS,RGB(255,0,0));

                     SelectObject(hdc,hb);

                     Ellipse(hdc,130,70,270,190);

                     hp=CreatePen(PS_DASHDOT,1,RGB(0,255,0));

                     SelectObject(hdc,hp);

                     MoveToEx(hdc,100,130,NULL);

                     LineTo(hdc,300,130);

                     MoveToEx(hdc,200,30,NULL);

                     LineTo(hdc,200,230);

                     EndPaint(hwnd,&ps);

                     DeleteObject(hp);

                     DeleteObject(hb);

              }

                           

              break;

                    

       case WM_DESTROY:

              PostQuitMessage(0);       //调用PostQuitMessage发出WM_QUIT消息

              break;

       default:                        //默认时采取系统消息默认处理函数

              return DefWindowProc(hwnd,message,wParam,lParam);   

       }

       return 0;

}

 

头文件代码:

#define IDM_ADDMENU              14

#define IDM_DELMENU               16

#define IDM_OPEN                    11

#define IDM_SAVE                    13

#define IDM_DRAW                   12

#define IDM_EXIT                       15

#define IDM_zonghe                     18

#define IDM_fangcha                   19

#define IDM_junfanggen                 21  

#define IDM_zonghehelp                22 

#define IDM_fangchahelp              23 

#define IDM_junfanggenhelp             24   

#define IDM_ABOUT                  20

#define IDM_zonghedelete               30

#define IDM_fangchamodify              31

#define IDM_pingjunzhiadd                32

#define IDM_nihe                       33

#define IDM_pingjunzhi                  34

                    

资源文件代码:

#include"windows.h"

#include"实验二.h"

Menu MENU DISCARDABLE

{

   POPUP"文件(&F)"

   {

      MENUITEM"打开(&O)/tCtrl+O",   IDM_OPEN

        

         MENUITEM"保存(&S)/tCtrl+S",   IDM_SAVE

        

         MENUITEM"画图(&D)",           IDM_DRAW

        

         MENUITEM"退出(&X)",           IDM_EXIT

   }

   POPUP"计算(&C)"

   {

      MENUITEM"总和",      IDM_zonghe

      

         MENUITEM"方差",       IDM_fangcha

        

         MENUITEM"均方根",     IDM_junfanggen

        

        

   }

   POPUP"帮助(&H)"

   {

      MENUITEM"计算总和的帮助",      IDM_zonghehelp

      

         MENUITEM"计算方差的帮助",       IDM_fangchahelp

        

         MENUITEM"计算均方根的帮助",     IDM_junfanggenhelp

         MENUITEM SEPARATOR

         MENUITEM"关于(&A)",             IDM_ABOUT

   }

}

 

Cursor CURSOR G:/2009年学习/vc++/实验二/Cursor.cur

Icon ICON G:/2009年学习/vc++/实验二/Icon.ico

 

你可能感兴趣的:(实现Windows应用程序对键盘与鼠标的响应)