MFC 消息循环模拟Win32程序

来源:互联网 发布:牛牛决战8.0怎么改数据 编辑:程序博客网 时间:2024/06/04 18:45

以下是我在vc6.0环境下运行通过的MFC消息循环模拟程序;欢迎大家给与宝贵意见

如果连接出现问题,请点击 [Project]--->[Settings]下选择 Link项最下方的 Project Optioins中将

/subsystem:console改为/subsystem:windows

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

#define dim(x)   (sizeof(x)/sizeof(x[0]))//消息队列的长度

/*以下为定义的消息处理函数*/
LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

/*以下为消息结构
struct MSGMAP_ENTRY
{
public:
 UINT umessage;
 LONG (*pfn)(HWND,UINT,WPARAM,LPARAM);//以函数指针指向相应的消息处理函数

};

/*对消息数组中出现的消息声明相应的消息处理函数*/

LONG OnChar(HWND,UINT,WPARAM,LPARAM);
LONG OnButtonDown(HWND,UINT,WPARAM,LPARAM);
LONG OnPaint(HWND,UINT,WPARAM,LPARAM);
LONG OnClose(HWND,UINT,WPARAM,LPARAM);
LONG OnDestroy(HWND,UINT,WPARAM,LPARAM);

//主函数
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;
 wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
 wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
 wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
 wndcls.hInstance=hInstance;
 wndcls.lpfnWndProc=WinSunProc;
 wndcls.lpszClassName="Weixin2003";
 wndcls.lpszMenuName=NULL;
 wndcls.style=CS_HREDRAW | CS_VREDRAW;
 RegisterClass(&wndcls);

 HWND hwnd;
 hwnd=CreateWindow("Weixin2003","VC++讨论中心",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;
}

//消息队列及函影射数组
struct MSGMAP_ENTRY _MessageEntries[] =
{
 WM_CHAR,         OnChar,
 WM_LBUTTONDOWN,  OnButtonDown,
 WM_PAINT,        OnPaint,
 WM_CLOSE,        OnClose,
 WM_DESTROY,      OnDestroy,
};
LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
 int i;
 for(i=0;i<dim(_MessageEntries);i++)
 {
  if(uMsg==_MessageEntries[i].umessage)
  {
   return ((*_MessageEntries[i].pfn)(hwnd,uMsg,wParam,lParam));
  }
 }
 return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

LONG OnChar(HWND hWnd,UINT,WPARAM,LPARAM)
{
    MessageBox(hWnd,"键盘按下","消息",0);
 return 0;
}
LONG OnButtonDown(HWND hWnd,UINT,WPARAM,LPARAM)
{

 MessageBox(hWnd,"左键按下","消息",0);
 return 0;
}
LONG OnPaint(HWND hWnd,UINT,WPARAM,LPARAM)
{
 HDC hdc;
 PAINTSTRUCT ps;
 hdc = BeginPaint(hWnd,&ps);
 TextOut(hdc,0,0,"
www.csdn.net",strlen(www.csdn.net));
 EndPaint(hWnd,&ps);
 return 0;
}
LONG OnClose(HWND hWnd,UINT,WPARAM,LPARAM)
{
 if(IDYES==MessageBox(hWnd,"是否结束程序?","消息",MB_YESNO))
 {
  DestroyWindow(hWnd);
 }
 return 0;
}
LONG OnDestroy(HWND,UINT,WPARAM,LPARAM)
{
 PostQuitMessage(0);
 return 0;

原创粉丝点击