应用程序框架的模型

本文详细介绍了基于WinMain.cpp的应用程序框架模型,包括窗口句柄、窗体类名称的定义,以及初始化、运行和关闭应用程序的主要流程。通过具体代码示例展示了如何创建窗口类、注册窗口、显示窗口并进行消息循环。

/**************************************************
WinMain.cpp
应用程序框架的模型
**************************************************/

// Include files
#include <windows.h>
#include <stdio.h>

// Window handles, class and caption text 声明WINDOWS句柄 和窗体类的标题类名等
HWND          g_hWnd;
HINSTANCE     g_hInst;
static char   g_szClass[]   = "ShellClass";
static char   g_szCaption[] = "Shell Application";

// Function prototypes
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev,          /
                   LPSTR szCmdLine, int nCmdShow);
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg,              /
                           WPARAM wParam, LPARAM lParam);

BOOL DoInit(); //声明初始化函数
BOOL DoShutdown();//声明退出函数
BOOL DoFrame();//声明主框架函数

int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev,          /
                   LPSTR szCmdLine, int nCmdShow)
{
  WNDCLASSEX wcex;
  MSG        Msg;

  g_hInst = hInst;

  // Create the window class here and register it
  //设置窗体与注册窗体
  wcex.cbSize        = sizeof(wcex);
  wcex.style         = CS_CLASSDC;
  wcex.lpfnWndProc   = WindowProc;
  wcex.cbClsExtra    = 0;
  wcex.cbWndExtra    = 0;
  wcex.hInstance     = hInst;
  wcex.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wcex.hbrBackground = NULL;
  wcex.lpszMenuName  = NULL;
  wcex.lpszClassName = g_szClass;
  wcex.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  if(!RegisterClassEx(&wcex))
    return FALSE;
//建立窗体
  // Create the Main Window
  g_hWnd = CreateWindow(g_szClass, g_szCaption,
        WS_CAPTION | WS_SYSMENU,
        0, 0, 400, 400,
        NULL, NULL,
        hInst, NULL );
  if(!g_hWnd)
    return FALSE;
  //显示与更新窗体
  ShowWindow(g_hWnd, SW_NORMAL);
  UpdateWindow(g_hWnd);

  // Run init function and return on error
  if(DoInit() == FALSE)
    return FALSE;

  // Start message pump, waiting for signal to quit
  ZeroMemory(&Msg, sizeof(MSG));   //ZeroMemory 宏为填充一内存块为零
  //&Msg :starting address of the block of memory to fill with zeros  sizeof(MSG):长度
//消息循环
  while(Msg.message != WM_QUIT) {
    if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
    }
 //调用框架函数
    if(DoFrame() == FALSE)
      break;
  }

  // Run shutdown function
  DoShutdown();
 
  //The UnregisterClass function unregisters a window class, freeing the memory required for the class.
  //窗体类的类名, 窗口句柄
  UnregisterClass(g_szClass, hInst);

  return Msg.wParam;
}

//回调函数
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg,              /
                           WPARAM wParam, LPARAM lParam)
{
  switch(uMsg) {
    case WM_DESTROY:
      PostQuitMessage(0); //退出窗口消息
      return 0;

  }

  //返回默认消息处理
  return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

BOOL DoInit()
{
  return TRUE;
}

BOOL DoShutdown()
{
  return TRUE;
}

BOOL DoFrame()
{
  return TRUE;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值