How to have an executable file run without a console?

左心房为你撑大大i 提交于 2019-12-11 10:14:59

问题


Right, so I'm in a little bit of a pickle here. I've been coding for years (non-professionally, only algorithms for competitions, I'm still in high school) and I tried to create my very first actual program/application, something that runs a window.

With the information from MSDN and a couple of threads I found on google, I managed to type up something basic which simply opens a 500x100 blank window. The problem is, when I run the executable which I get after compiling, a blank console window opens up together with my window. I'd like some help with how to disable it. Here's my code:

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

static TCHAR szWindowClass[] = _T("Snake Class");
static TCHAR szTitle[] = _T("Snake");

HINSTANCE hInst;

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

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, 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 = NULL;//LoadIcon(hInstance,     MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = NULL;//LoadIcon(wcex.hInstance,     MAKEINTRESOURCE(IDI_APPLICATION));

    if(!RegisterClassEx(&wcex)){
        MessageBox(NULL,_T("Call to RegisterClassEx     failed!"),_T("Snake"),NULL);

        return 1;
    }

    HWND hWnd =     CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAU    LT,500,100,NULL,NULL,hInstance,NULL);

    if(!hWnd){
        MessageBox(NULL,_T("Call to CreateWindow     failed!"),_T("Snake"),NULL);

        return 1;
    }

    hInst = hInstance;

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

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

    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)    {
    PAINTSTRUCT ps;
    HDC hdc;

    TCHAR greeting[] = _T("Hello World!");

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

        //Paint

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

    return 0;
}

来源:https://stackoverflow.com/questions/29882262/how-to-have-an-executable-file-run-without-a-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!