| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1676 人关注过本帖
标题:【Escapist:003】C++ 仅仅使用 Windows API 创建一个窗口
只看楼主 加入收藏
Escapist
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2020-4-27
结帖率:75%
收藏
 问题点数:0 回复次数:3 
【Escapist:003】C++ 仅仅使用 Windows API 创建一个窗口
仅仅使用 Windows API 创建一个窗口,并且让它显示在屏幕中央。
代码:
程序代码:
#include<Windows.h>
#pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
const char WindowClassName[] = "WindowClass";
const char WindowCaption[] = "CreateWindowExA";
HINSTANCE InstanceHandle;
//HWND WindowHandle;
long __stdcall WindowProcess(HWND WindowHandle, unsigned int Message, unsigned int wParam, long lParam);

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE PreInstanceHandle, char* lpCmdLine, int nCmdShow)
{
    InstanceHandle = hInstance;
    WNDCLASSEXA WindowClass;
    WindowClass.cbSize = sizeof(WNDCLASSEXW);
    WindowClass.style = CS_HREDRAW | CS_VREDRAW;
    WindowClass.lpfnWndProc = WindowProcess;
    WindowClass.cbClsExtra = 0;
    WindowClass.cbWndExtra = 0;
    WindowClass.hInstance = InstanceHandle;
    WindowClass.hIcon = LoadIconA(InstanceHandle, NULL);
    WindowClass.hCursor = LoadCursorA(InstanceHandle, MAKEINTRESOURCEA(32512));
    WindowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    WindowClass.lpszMenuName = NULL;
    WindowClass.lpszClassName = WindowClassName;
    WindowClass.hIconSm = LoadIconA(InstanceHandle, NULL);
    RegisterClassExA(&WindowClass);
    HWND WindowHandle = CreateWindowExA(WS_EX_CLIENTEDGE, WindowClassName, WindowCaption, WS_OVERLAPPEDWINDOW, 0, 0, 750, 500, NULL, NULL, InstanceHandle, NULL);
    if (!WindowHandle)
        return 0;
    ShowWindow(WindowHandle, SW_SHOW);
    MSG Message;
    while (GetMessageA(&Message, nullptr, 0, 0))
    {
        TranslateMessage(&Message);
        DispatchMessageA(&Message);
    }
    return 0;
}
long __stdcall WindowProcess(HWND WindowHandle, unsigned int Message, unsigned int wParam, long lParam)
{
    switch (Message)
    {
    case WM_CREATE:
    {
        RECT Rect;
        GetWindowRect(WindowHandle, &Rect);
        SetWindowPos(WindowHandle, HWND_TOP, ((GetSystemMetrics(SM_CXSCREEN) - Rect.right) / 2), ((GetSystemMetrics(SM_CYSCREEN) - Rect.bottom) / 2), Rect.right, Rect.bottom, SWP_SHOWWINDOW);
        break;
    }
    case WM_PAINT:
    {
        PAINTSTRUCT PaintStruct;
        HDC DCHandle = BeginPaint(WindowHandle, &PaintStruct);
        //添加绘图代码
        EndPaint(WindowHandle, &PaintStruct);
        break;
    }
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        break;
    }
    default:
    {
        return DefWindowProcA(WindowHandle, Message, wParam, lParam);
    }
    }
    return 0;
}

Main.rar (1.08 KB)

将代码拷贝到空白cpp文件中即可运行。
如果反响较高,我会详细出几期教程,谢谢~
搜索更多相关主题的帖子: Message long int Windows NULL 
2020-05-05 18:29
Escapist
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2020-4-27
收藏
得分:0 
想让我出后续教程的请务必评论,谢谢!
2020-05-05 18:47
吕孟伟
Rank: 8Rank: 8
等 级:贵宾
威 望:27
帖 子:200
专家分:870
注 册:2018-10-4
收藏
得分:0 
写个教学文档吧,谢谢。

借用一下侯捷的话:勿在浮沙筑高台。
2020-05-05 22:05
Escapist
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2020-4-27
收藏
得分:0 
2020-05-10 09:29
快速回复:【Escapist:003】C++ 仅仅使用 Windows API 创建一个窗口
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061131 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved