问题
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