hello world 程序优化

这样使添加处理消息函数变得简单数组

步骤1: 头文件中添加相关项ide

步骤2: cpp添加具体实现方法函数

步骤3: 结构体数组中添加相关项ui

.h 文件:spa

  
  
  
  
  1. struct DecodeUint{  
  2.  UINT meesage;  
  3.  LONG (*fun)(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);  
  4. };  
  5.  
  6. #define dim(x) (sizeof(x)/sizeof(x[0]))  
  7.  
  8. LONG OnDestory(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);  
  9. LONG OnPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);  
  10.  

.c文件:code

  
  
  
  
  1. #include <Windows.h>
    #include "Border.h"
  2.  
  3.  
  4. struct DecodeUint MainMessageProc[] = {  
  5.  WM_DESTROY,OnDestory,  
  6.  WM_PAINT,OnPaint,  
  7. };  
  8.  
  9. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);  
  10.  
  11. int WinMain(HINSTANCE hInstance,  
  12.    HINSTANCE hPrevInstance,  
  13.    LPTSTR    lpCmdLine,  
  14.    int       nCmdShow)  
  15. {  
  16.    
  17.  WNDCLASS wc;  
  18.  
  19.  wc.cbClsExtra    = 0;  
  20.  wc.cbWndExtra    = 0;  
  21.  wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);  
  22.  wc.hCursor       = NULL;   
  23.  wc.hIcon         = NULL;  
  24.  wc.hInstance     = hInstance;  
  25.  wc.lpfnWndProc   = WndProc;  
  26.  wc.lpszClassName = TEXT("hello");  
  27.  wc.lpszMenuName  = NULL;  
  28.  wc.style         = CS_HREDRAW | CS_VREDRAW;  
  29.  
  30.    
  31.  if (!RegisterClass(&wc)) return -1;  
  32.  
  33.    
  34.  HWND hWnd = CreateWindow(TEXT("hello"),TEXT("helloworld"), WS_VISIBLE | WS_BORDER | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CAPTION,  
  35.   10,10,320,240,NULL,NULL,hInstance,NULL);  
  36.  
  37.    
  38.  UpdateWindow(hWnd);  
  39.  ShowWindow(hWnd,nCmdShow);  
  40.  
  41.  MSG msg;  
  42.  
  43.  while (GetMessage(&msg, NULL, 0, 0))  
  44.  {  
  45.   TranslateMessage(&msg);  
  46.   DispatchMessage(&msg);  
  47.  }  
  48.  
  49.  return 1;  
  50. }  
  51.  
  52. LONG OnDestory(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
  53. {  
  54.  PostQuitMessage(1);  
  55.  return TRUE;  
  56. }  
  57.  
  58. LONG OnPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
  59. {  
  60.  PAINTSTRUCT ps;  
  61.  HDC hdc;  
  62.  RECT rect;  
  63.  
  64.  GetClientRect(hWnd,&rect);  
  65.  hdc = BeginPaint(hWnd,&ps);  
  66.  DrawText(hdc,TEXT("hello world"),-1,&rect,DT_VCENTER | DT_CENTER);  
  67.  EndPaint(hWnd,&ps);  
  68.  
  69.  return TRUE;  
  70. }  
  71.  
  72.  
  73. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
  74. {  
  75.  
  76.  for (int i = 0;i < dim(MainMessageProc); i++)  
  77.  {  
  78.   if (MainMessageProc[i].meesage == message)  
  79.   {  
  80.    (*MainMessageProc[i].fun)(hWnd,message,wParam,lParam);  
  81.   }  
  82.  }  
  83.  
  84.  return DefWindowProc(hWnd,message,wParam,lParam);  
  85. }