Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2621610
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: Windows平台

2013-07-28 00:19:48

      工程:
            
    
     WinMain.h

点击(此处)折叠或打开

  1. #ifndef _WINMAIN_H_
  2. #define _WINMAIN_H_

  3. #include <windows.h>

  4. #define SCREEN_WIDTH 600
  5. #define SCREEN_HEIGHT 400

  6. /************************************************************************/
  7. /* WinApplication窗口回调函数 */
  8. /************************************************************************/
  9. LRESULT CALLBACK WinLesson1Pro(
  10.     _In_ HWND hwnd,
  11.     _In_ UINT uMsg,
  12.     _In_ WPARAM wParam,
  13.     _In_ LPARAM lParam
  14.     );

  15. /************************************************************************/
  16. /* 消息提示框+处理事件 */
  17. /************************************************************************/
  18. int DisplayMessageBox(HWND hwnd, char * lpText, char * lpCaption, UINT uType);

  19. #endif    //_WINMAIN_H_
    WinMain.cpp

点击(此处)折叠或打开

  1. #include "WinMain.h"
  2. #include <stdio.h>

  3. int WINAPI WinMain(
  4.     HINSTANCE hInstance,
  5.     HINSTANCE hPrevInstance,
  6.     /*LPWSTR*/LPSTR lpCmdLine,
  7.     int nShowCmd
  8.     )
  9. {
  10.     WNDCLASS wndcls;

  11.     ///< 构造窗体类
  12.     wndcls.cbClsExtra = 0;
  13.     wndcls.cbWndExtra = 0;
  14.     wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  15.     wndcls.hCursor = LoadCursor(NULL, IDC_ARROW);
  16.     wndcls.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  17.     wndcls.hInstance = hInstance;
  18.     wndcls.lpfnWndProc = WinLesson1Pro;
  19.     wndcls.lpszClassName = "lesson1";
  20.     wndcls.lpszMenuName = NULL;
  21.     wndcls.style = CS_HREDRAW | CS_VREDRAW;

  22.     ///< 注册窗体
  23.     RegisterClass(&wndcls);

  24.     ///< 创建窗口
  25.     HWND hwnd;
  26.     hwnd = CreateWindow("lesson1", "小豆第一课学习",
  27.         WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
  28.         NULL, NULL, hInstance, NULL);
  29.     if (!hwnd)
  30.     {
  31.         return -1;
  32.     }

  33.     ///< 显示窗口
  34.     ShowWindow(hwnd, SW_SHOWNORMAL);

  35.     ///< 更新窗口
  36.     UpdateWindow(hwnd);

  37.     MSG msg;
  38.     ///< 消息循环
  39.     while(GetMessage(&msg, hwnd, 0, 0) > 0)
  40.     {
  41.         TranslateMessage(&msg);        ///< 转换消息
  42.         DispatchMessage(&msg);         ///< 分发消息【消息被分发到回调函数(过程函数)
  43.     }

  44.     return 0;
  45. }

  46. LRESULT CALLBACK WinLesson1Pro(
  47.     _In_ HWND hwnd,
  48.     _In_ UINT uMsg,
  49.     _In_ WPARAM wParam,
  50.     _In_ LPARAM lParam
  51.     )
  52. {
  53.     HDC hdc;
  54.     char szChar[20];

  55.     switch (uMsg)
  56.     {
  57.     case WM_CHAR:
  58.         // The keyboard.
  59.         sprintf(szChar, "char is %c", wParam);
  60.         DisplayMessageBox(hwnd, szChar, "消息", 0);
  61.         break;

  62.     case WM_LBUTTONDOWN:
  63.         // The left mouse button.
  64.         DisplayMessageBox(hwnd, "The left mouse clicked", "鼠标左键", 0);
  65.         ///< Text out
  66.         hdc = GetDC(hwnd);
  67.         TextOut(hdc, 0, 50, "文字", strlen("文字"));
  68.         ReleaseDC(hwnd, hdc);
  69.         break;

  70.     case WM_RBUTTONDOWN:
  71.         // The right mouse button.
  72.         break;

  73.     case WM_CLOSE:
  74.         // Close the window.
  75.         if (IDYES == DisplayMessageBox(hwnd, "是否结束?", "关闭", MB_YESNO))
  76.         {
  77.             DestroyWindow(hwnd);
  78.             return 0;
  79.         }
  80.         break;

  81.     case WM_CREATE:
  82.         // Initialize the window.
  83.         //DisplayResourceNAMessageBox(hwnd, "创建", "WM_CREATE", MB_HELP);
  84.         return 0;

  85.     case WM_PAINT:
  86.         // Paint the window's client area.
  87.         PAINTSTRUCT ptruct;
  88.         hdc = BeginPaint(hwnd, &ptruct);
  89.         TextOut(hdc, 0, 0, "绘制", strlen("绘制"));
  90.         EndPaint(hwnd, &ptruct);
  91.         return 0;

  92.     case WM_SIZE:
  93.         // Set the size and position of the window.
  94.         //DisplayResourceNAMessageBox(hwnd, "大小", "WM_SIZE", MB_HELP);
  95.         return 0;

  96.     case WM_DESTROY:
  97.         // Clean up window-specific data objects.
  98.         PostQuitMessage(0);
  99.         return 0;

  100.     default:
  101.         return DefWindowProc(hwnd, uMsg, wParam, lParam);
  102.     }
  103.     return 0;
  104. }

  105. int DisplayMessageBox(HWND hwnd, char * lpText, char * lpCaption, UINT uType)
  106. {
  107.     int msgboxID = MessageBox(hwnd, lpText, lpCaption, uType);

  108.     switch (msgboxID)
  109.     {
  110.     case IDCANCEL:
  111.         // TODO: add code
  112.         break;
  113.     case IDTRYAGAIN:
  114.         // TODO: add code
  115.         break;
  116.     case IDCONTINUE:
  117.         // TODO: add code
  118.         break;
  119.     }

  120.     return msgboxID;
  121. }
   
    运行:
        
阅读(1740) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~