C++编写的Windows窗口Hello,World程序

2021/4/19 7:25:09

本文主要是介绍C++编写的Windows窗口Hello,World程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C++编写的Windows窗口Hello,World程序

 

#include <windows.h>
#include <tchar.h>
/* 使类名成为全局变量 */
TCHAR szClassName[ ] = TEXT("WindowsApp");
/* 这个函数由Windows函数DispatchMessage()调用 */
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    HDC hdc ;
    PAINTSTRUCT ps ;
    RECT rect ;
    switch (message)                  
    /* 处理信息 */ {
        case WM_PAINT:
                hdc = BeginPaint (hWnd, &ps) ;
        GetClientRect (hWnd, &rect) ;
        DrawText (hdc, TEXT ("Hello World!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
        EndPaint (hWnd, &ps) ;
        break ;
        case WM_DESTROY:
                PostQuitMessage (0);
        /* 发送WM_QUIT到消息队列 */
        break;
        default:                      
        /* 不想处理的消息 */
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
    return 0;
}
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) {
    HWND hwnd;
    /* 窗口的句柄 */
    MSG messages;
    /* 用于储存应用程序的消息 */
    WNDCLASSEX wincl;
    /* 窗口类的数据结构 */
    /* 窗口结构 */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    /* 被Windows调用的函数 */
    wincl.style = CS_DBLCLKS;
    /* 捕获双击事件 */
    wincl.cbSize = sizeof (WNDCLASSEX);
    /* 使用默认的图表和鼠标指针 */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    /* 没有菜单 */
    wincl.cbClsExtra = 0;
    /* 窗口类后面没有额外的字节 */
    wincl.cbWndExtra = 0;
    /* 窗口实例化结构 */
    /* 使用Windows的默认颜色作为窗口的背景色 */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    /* 注册窗口类,如果失败,退出程序 */
    if (!RegisterClassEx (&wincl))
        return 0;
    /* 如果类被注册,创建窗口 */
    hwnd = CreateWindowEx (
            0,                   
    /* 扩展的变化信息 */
    szClassName,         
    /* 类名 */"Hello World",       
    /* 标题栏文本 */
    WS_OVERLAPPEDWINDOW, 
    /* 默认窗口 */
    CW_USEDEFAULT,       
    /* 使用默认的位置 */
    CW_USEDEFAULT,       
    /* 使用默认的位置 */
    544,                 
    /* 窗口宽度(以像素点为单位) */
    375,                 
    /* 窗口高度(以像素点为单位) */
    HWND_DESKTOP,        
    /* 此窗口是桌面的字窗口 */
    NULL,                
    /* 没有菜单 */
    hThisInstance,       
    /* 程序实例化句柄 */
    NULL                 
    /* 没有创建数据的窗口 */
    );
    /* 显示窗口 */
    ShowWindow (hwnd, nFunsterStil);
    /* 运行消息循环。它将在GetMessage()返回零的时候退出 */
    while (GetMessage (&messages, NULL, 0, 0)) {
        /* 把虚拟按键消息翻译成字符消息 */
        TranslateMessage(&messages);
        /* 把消息发送到WindowProcedure函数 */
        DispatchMessage(&messages);
    }
    /* 程序的返回值,由PostQuitMessage()提供。 */
    return messages.wParam;
}

 

 

 



这篇关于C++编写的Windows窗口Hello,World程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程