W32Template.h

 
最近重新看下Windows程序设计,温故知新,感觉写那个WinMain函数实在是麻烦,定义一个宏来替代,模板如下:
在写的过程中还出了一点小问题,在写一个宏要管一片代码时,代码间不能空行。

#pragma once
#include <Windows.h>
#ifdef _T
#undef _T
#define _T TEXT
#else
#define _T TEXT
#endif
#define TWOPI (2*3.14159)
#define NUMLINES 1000
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wp,LPARAM lp);
HWND hwndWnd;
HWND GetWndHandle()
{
 return hwndWnd;
}
#define DECLARE_W32_APP(name,style)\
 int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )\
 {\
  static TCHAR szAppName[] = #name;\
  MSG msg;\
  HWND hwnd;\
  WNDCLASS wndclass = {\
   CS_VREDRAW | CS_HREDRAW,\
   WndProc,\
   0,\
   0,\
   hInstance,\
   LoadIcon(NULL,IDI_APPLICATION),\
   LoadCursor(NULL,IDC_ARROW),\
   (HBRUSH)GetStockObject(WHITE_BRUSH),\
   NULL,\
   #name\
  };\
  if (!RegisterClass(&wndclass))\
  {\
   MessageBox( NULL, _T("Program requires Windows NT!\n"), #name, MB_ICONERROR );\
    return 0;\
  }\
  hwnd = CreateWindow( #name, #name, style, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL, NULL, hInstance, NULL );\
  ShowWindow(hwnd,nShowCmd);\
  UpdateWindow(hwnd);\
  hwndWnd = hwnd;\
  while (GetMessage(&msg,NULL,0,0))\
  {\
   TranslateMessage(&msg);\
   DispatchMessage(&msg);\
  }\
  return msg.wParam;\
 }
void Ondle();
#define DECLARE_W32_APP_EX(name,style)\
 int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd ){\
  static TCHAR szAppName[] = #name;\
  HWND hwnd;\
  MSG msg;\
  WNDCLASS wndclass = {\
   CS_VREDRAW | CS_HREDRAW,\
   WndProc,\
   0,\
   0,\
   hInstance,\
   LoadIcon(NULL,IDI_APPLICATION),\
   LoadCursor(NULL,IDC_ARROW),\
   (HBRUSH)GetStockObject(WHITE_BRUSH),\
   NULL,\
   #name\
  };\
  if (!RegisterClass(&wndclass))\
  {\
   MessageBox( NULL, _T("Program requires Windows NT!\n"), #name, MB_ICONERROR );\
   return 0;\
  }\
  hwnd = CreateWindow( #name, #name, style, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL, NULL, hInstance, NULL );\
  ShowWindow(hwnd,nShowCmd);\
  UpdateWindow(hwnd);\
  hwndWnd = hwnd;\
  while (TRUE)\
  {\
   if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){\
    if( msg.message == WM_QUIT )\
     break;\
    TranslateMessage(&msg);\
    DispatchMessage(&msg);\
   }else\
    Ondle();\
  }\
  return msg.wParam;\
 }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值