Windows 应用程序可以同时具有 GUI 和控制台窗口。只是没有人以这种方式设置它们。你必须自己处理。
以下是一些执行此操作的示例代码:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <iostream>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool ReconnectIO(bool OpenNewConsole);
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
HWND hwnd;
WNDCLASS wc;
if(!ReconnectIO(false))
printf("Started from command prompt\n");
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpszClassName = "Window";
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClass(&wc);
hwnd = CreateWindow(wc.lpszClassName, "Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 350, 250, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
printf("Entering GetMessage loop\n");
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
/*******************************************************************************
* NAME:
* ReconnectIO
*
* SYNOPSIS:
* bool ReconnectIO(bool OpenNewConsole);
*
* PARAMETERS:
* OpenNewConsole [I] -- This controls if we open a console window or not.
* True -- if the program was not started from an
* existing console open a new console window.
* False -- Only connect stdio if the program was
* started from an existing console.
*
* FUNCTION:
* This function connects up the stardard IO (stdout, stdin, stderr) to
* the windows console. It will open a new console window if needed
* (see 'OpenNewConsole').
*
* RETURNS:
* true -- A new console window was opened
* false -- Using an existing console window
*
* SEE ALSO:
*
******************************************************************************/
bool ReconnectIO(bool OpenNewConsole)
{
int hConHandle;
long lStdHandle;
FILE *fp;
bool MadeConsole;
MadeConsole=false;
if(!AttachConsole(ATTACH_PARENT_PROCESS))
{
if(!OpenNewConsole)
return false;
MadeConsole=true;
if(!AllocConsole())
return false; // Could throw here
}
// STDOUT to the console
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
// STDIN to the console
lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );
// STDERR to the console
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
setvbuf( stderr, NULL, _IONBF, 0 );
// C++ streams to console
std::ios_base::sync_with_stdio();
return MadeConsole;
}
程序的顶部只是一个带有 WinMain() 入口点的普通 Window 程序。魔法来自 ReconnectIO() 函数。如果需要,它将重新连接标准 io 并打开一个控制台窗口。
当你从命令行启动程序时,标准输出会转到它,当你从桌面启动时,只打开主窗口。
它确实有一个缺点,那就是当从命令行启动时,它会立即返回,而不是阻塞直到程序退出。启动代码正在这样做,我还没有研究如何停止它。
您可以查看http://dslweb.nwnexus.com/~ast/dload/guicon.htm 的更多信息,了解正在发生的事情。