Windows c++界面开发初学(二)win32

今天跟着微软的文档和一些博客写了个win32的hello world。
因为代码注释写的比较详细了就不加太多描述了。

这里放一下文档链接和博客链接:
微软文档:创建传统的 Windows 桌面应用程序 (c + +)
参考博客1:跟我一起玩Win32开发(2):完整的开发流程
参考博客2:Win32 API编程简介Demo程序
参考博客3:Windows SDK编程 API入门系列之一:那‘烦人’的Windows数据类型

1、典型的Windows程序结构

图来自参考博客2

2、代码结构

1、 头文件

#include <windows.h>
#include <tchar.h>

2、程序入口点

int CALLBACK WinMain(
   _In_ HINSTANCE hInstance,
   _In_ HINSTANCE hPrevInstance,
   _In_ LPSTR     lpCmdLine,
   _In_ int       nCmdShow
);
  • 窗口信息
WNDCLASSEX wcex;

wcex.cbSize         = sizeof(WNDCLASSEX);
wcex.style          = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = WndProc;
wcex.cbClsExtra     = 0;
wcex.cbWndExtra     = 0;
wcex.hInstance      = hInstance;
wcex.hIcon          = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName   = NULL;
wcex.lpszClassName  = szWindowClass;
wcex.hIconSm        = LoadIcon(wcex.hInstance, IDI_APPLICATION);
  • 注册WNDCLASSEX
if (!RegisterClassEx(&wcex))
{
   MessageBox(NULL,
      _T("Call to RegisterClassEx failed!"),
      _T("Windows Desktop Guided Tour"),
      NULL);

   return 1;
}
  • 创建窗口
static TCHAR szWindowClass[] = _T("DesktopApp");
static TCHAR szTitle[] = _T("Windows Desktop Guided Tour Application");

// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
   szWindowClass,
   szTitle,
   WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT, CW_USEDEFAULT,
   500, 100,
   NULL,
   NULL,
   hInstance,
   NULL
);
if (!hWnd)
{
   MessageBox(NULL,
      _T("Call to CreateWindow failed!"),
      _T("Windows Desktop Guided Tour"),
      NULL);

   return 1;
}
  • 使窗口可见
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
   nCmdShow);
UpdateWindow(hWnd);
  • 处理消息
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
   TranslateMessage(&msg);
   DispatchMessage(&msg);
}

return (int) msg.wParam;

3、window-procedure function

LRESULT CALLBACK WndProc(
   _In_ HWND   hwnd,
   _In_ UINT   uMsg,
   _In_ WPARAM wParam,
   _In_ LPARAM lParam
);
  • 启用 WndProc 函数以处理应用程序收到的各种消息

    • WM_PAINT
      应用程序收到此消息,此时必须更新其显示窗口的一部分。 当用户将窗口移动前面窗口中,然后将其再次移动消失时,可能发生此事件。

    • WM_CREATE
      首次创建一个窗口

    • WM_DESTROY
      窗口关闭

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   PAINTSTRUCT ps;
   HDC hdc;
   TCHAR greeting[] = _T("Hello, Windows desktop!");

   switch (message)
   {
   case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);

      // Here your application is laid out.
      // For this introduction, we just print out "Hello, Windows desktop!"
      // in the top left corner.
      TextOut(hdc,
         5, 5,
         greeting, _tcslen(greeting));
      // End application specific layout section.

      EndPaint(hWnd, &ps);
      break;
   case WM_DESTROY:
      PostQuitMessage(0);
      break;
   default:
      return DefWindowProc(hWnd, message, wParam, lParam);
      break;
   }

   return 0;
}

3、完整代码以及运行结果

#include<Windows.h>
#include<tchar.h>
#include <stdlib.h>
#include <string.h>

static TCHAR szWindowClass[] = _T("My Class");
static TCHAR szTitle[] = _T("我的应用程序");

HINSTANCE hInst;

//handle messages that the application received from Windows when events occur.
LRESULT CALLBACK WindowProc(
    _In_ HWND hwnd,
    _In_ UINT uMsg,
    _In_ WPARAM wParam,
    _In_ LPARAM lParam
);

int CALLBACK WinMain(
    _In_ HINSTANCE hInstance,
    _In_ HINSTANCE hPrevInstance,
    _In_ LPSTR lpCmdLine,
    _In_ int nCmdShow
) {
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX); //specifies the size of the strcture

    /*
    CS_HREDRAW:
    Redraws the entire window
    if a movement or size adjustment changes the width of the client area.
    CS_VREDRAW:
    Redraws the entire window
    if a movement or size adjustment changes the height of the client area.
    */
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WindowProc; //A pointer to the window procedure.
    wcex.cbClsExtra = 0; //The number of extra bytes to allocate following the window-class structure.
    wcex.cbWndExtra = 0; // The number of extra bytes to allocate following the window instance. 
    wcex.hInstance = hInstance; //A handle to the instance that contains the window procedure for the class.
    wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION); //A handle to the class icon. This member must be a handle to an icon resource. 
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW); //A handle to the class cursor. This member must be a handle to a cursor resource. 
    /*
    The system automatically deletes class background brushes when the class is unregistered
    A handle to the class background brush.
    This member can be a handle to the brush to be used for painting the background,
    or it can be a color value.
    */
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL; // If this member is NULL, windows belonging to this class have no default menu.
    wcex.lpszClassName = szWindowClass;//If lpszClassName is a string, it specifies the window class name. 
    wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION); //A handle to a small icon that is associated with the window class. 
    if (!RegisterClassEx(&wcex)) {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Windows Desktop Guided Tour"),
            NULL);
        return 1;
    }

    // Store instance handle in our global variable
    hInst = hInstance;

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );
    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Windows Desktop Guided Tour"),
            NULL);

        return 1;
    }
    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);
    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

LRESULT CALLBACK WindowProc(
    _In_ HWND hwnd,
    _In_ UINT uMsg,
    _In_ WPARAM wParam,
    _In_ LPARAM lParam
) {
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Hello World!");

    switch (uMsg)
    {
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));

        EndPaint(hwnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
        break;
    }
    return 0;
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/unirrrrr/article/details/81168023