首页 > 解决方案 > 如何使控制台进程不打开?

问题描述

所以,我正在尝试学习如何制作 C/C++ Windows 桌面应用程序,但是我遇到了一个很大的问题,当创建一个完全正常的窗口时,会出现运行控制台应用程序的 cmd。有没有办法让它甚至不打开?我一直在看到一些方法,但它们似乎隐藏了控制台,但过程仍然存在。
这是我的代码:

#define UNICODE
#define _UNICODE
#include <Windows.h>

LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow) {
    WNDCLASSW wc = { 0 };

    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hInstance = hInstance;
    wc.lpszClassName = L"windowClass";
    wc.lpfnWndProc = WinProc;
    
     if (!RegisterClass(&wc))
        return 1;

    HWND hWnd = CreateWindow(L"windowClass", L"Window 1",
        WS_OVERLAPPEDWINDOW,
        100, 100,
        256, 256,
        NULL, NULL, NULL, NULL);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    MSG msg = { 0 };

    while (GetMessage(&msg, NULL, NULL, NULL)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            DefWindowProc(hWnd, msg, wParam, lParam);
    }
}

标签: c++cwindows

解决方案


推荐阅读