MFC(一)(win32函数演示windows程序的运行过程)

Windows操作系统的所封装的消息格式:

typedef struct tagMSG {

  HWND   hwnd;

  UINT   message;

  WPARAM wParam;

  LPARAM lParam;

  DWORD  time;

  POINT  pt;

} MSG, *PMSG;

 

注释:

hwnd

Handle to the window whose window procedure receives the message.

message

Specifies the message identifier. Applications can only use the low word; the high word is reserved by the system.

wParam

Specifies additional information about the message. The exact meaning depends on the value of the message member.

lParam

Specifies additional information about the message. The exact meaning depends on the value of the message member.

time

Specifies the time at which the message was posted.

pt

Specifies the cursor position, in screen coordinates, when the message was posted.

 

句柄(HANDLE),资源的标识

操作系统要管理和操作这些资源,都是通过句柄来找到对应的资源。按资源的类型,又可将句柄细分成图标句柄(HICON),光标句柄(HCURSOR),窗口句柄(HWND),应用程序实例句柄(HINSTANCE)等等各种类型的句柄。操作系统给每一个窗口指定的一个唯一的标识号即窗口句柄。

 

 

入口函数winmain

int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance// handle to previous instance
  LPSTR lpCmdLine,          // command line命令行参数、类似于C语言main函数的两个参数,要设定该值的话可以在VC 6.0的Project/settings/debug/programme arguments下面进行设置
  int nCmdShow              // show state显示状态
);//该值通常由操作系统自动赋值
 
设计窗口类

typedef struct _WNDCLASS {

   UINT      style;   //特征可以用& | ~进行组合

   WNDPROC  lpfnWndProc;  //每一种不同类型的窗口都有自己专用的回调函数,该函数就是通过lpfnWndProc成员指定的

   int       cbClsExtra;//分配的额外内存空间

   int       cbWndExtra;//同上

   HANDLE   hInstance;//当前实例的实例号,通常用winmain函数的形参实例号赋值

   HICON     hIcon;//图标实例号

   HCURSOR   hCursor;//

   HBRUSH    hbrBackground;

   LPCTSTR   lpszMenuName;

   LPCTSTR   lpszClassName; 

} WNDCLASS;

 
 
我的第一个MFC程序代码实例:
 

#include <windows.h>

#include <stdio.h>

 

 

//回调函数,注意CALLBACK关键字不能掉,表明的是一种操作系统从消息队列中调用消息的一种方式。该函数的名字可以对边起,其其实就是一种对MSG消息前几个变量

LRESULT CALLBACK WinTotem(

  HWND hwnd,      // handle to window

  UINT uMsg,      // message identifier

  WPARAM wParam,  // first message parameter

  LPARAM lParam   // second message parameter

);

 

 

//应用程序入口,其参数由操作系统自动赋值

int WINAPI WinMain(

  HINSTANCE hInstance,      // handle to current instance

  HINSTANCE hPrevInstance,  // handle to previous instance

  LPSTR lpCmdLine,          // command line

  int nCmdShow              // show state

)

{

 

         //设计窗口类

         WNDCLASS wndcls;

         //额外分配的内存

         wndcls.cbClsExtra=0;

         wndcls.cbWndExtra=0;

         //设置背景色,HBRUSH画刷

         wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);

         //设置光标的类型.采用标准的类型时候第一个参数设置为NULL

         wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);

         //设置图标的类型,采用标准类型的时候第一个参数为NULL

         wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);

         //当前实例标识

         wndcls.hInstance=hInstance;

         //指定回调函数

         wndcls.lpfnWndProc=WinTotem;

         //指定类型名称,就如手机的型号

         wndcls.lpszClassName="Totem";

         //指定菜单的名称,没有菜单时设置为NULL

         wndcls.lpszMenuName=NULL;

         //设置窗口的类型由|&~连接各种属性

         wndcls.style=CS_HREDRAW | CS_VREDRAW;

        

 

 

         //注册窗口类,手机设计好了必须到工商局注册一下才能生产啊

         RegisterClass(&wndcls);

 

 

         //产生窗口

         HWND hwnd;

         hwnd=CreateWindow("Totem","我的第一个MFC程序",WS_OVERLAPPEDWINDOW,

                   0,0,600,400,NULL,NULL,hInstance,NULL);

 

         //显示窗口

         ShowWindow(hwnd,SW_SHOWNORMAL);

         //刷新窗口

         UpdateWindow(hwnd);

 

         MSG msg;

         //每次从系统的消息队列中取出一条消息

         while(GetMessage(&msg,NULL,0,0))

         {

                   //将消息交给操作系统

                   TranslateMessage(&msg);

                   //操作系统将消息传给回调函数处理

                   DispatchMessage(&msg);

         }

         return 0;

}

 

//回调函数的实现

LRESULT CALLBACK WinTotem(

  HWND hwnd,      // handle to window

  UINT uMsg,      // message identifier

  WPARAM wParam,  // first message parameter

  LPARAM lParam   // second message parameter

)

{

         //对不同的消息类型进行处理

         switch(uMsg)

         {

                   //键盘消息

         case WM_CHAR:

                   char szChar[20];

                   //根据参数对键盘进行判断,将字符串标准格式化输入到内存

                   sprintf(szChar,"char is %d",wParam);

                   MessageBox(hwnd,szChar,"weixin",0);

                   break;

                   //鼠标左键按下

         case WM_LBUTTONDOWN:

                   MessageBox(hwnd,"mouse clicked","世界之窗",0);

                   //设备上下文环境

                   HDC hdc;

                   hdc=GetDC(hwnd);

                   TextOut(hdc,0,50,"世界你好",strlen("世界你好"));

                   //用完后记得要释放DC

                   ReleaseDC(hwnd,hdc);

                   break;

                   //窗口重绘,这里的DC获取和释放和前面不一样哦亲,而且还不能替换呢

         case WM_PAINT:

                   HDC hDC;

                   PAINTSTRUCT ps;

                   hDC=BeginPaint(hwnd,&ps);

                   TextOut(hDC,0,0,"世界之窗",strlen("世界之窗"));

                   EndPaint(hwnd,&ps);

                   break;

                   //关闭窗口

         case WM_CLOSE:

                   if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))

                   {

                            DestroyWindow(hwnd);

                   }

                   break;

                   //这里有点小技巧哦,由DestroyWindow()发出的消息引发下列函数的处理,如果没有下列函数的处理可能就会产生窗口不显示但是进程还在后台运行的现象。

         case WM_DESTROY:

                   PostQuitMessage(0);

                   break;

                   //将其它情况用默认的方式理

         default:

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

         }

         return 0;

}

 

转载于:https://www.cnblogs.com/totem1990/archive/2012/03/22/2412644.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值