【发布时间】:2018-11-24 21:38:19
【问题描述】:
我有一个 WinAPI/Win32 应用程序,它还会在打开主窗口之前打开一个控制台窗口(用于调试目的)。我添加了一个安全检查,当主窗口按下 X 按钮时,它会询问“你确定吗?”事物。但是,如果我在控制台上单击 X,它会立即终止应用程序而不询问任何内容。有什么办法可以防止这种情况发生吗? 这是我的代码的 sn-p:
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow)
{
EnableDebug();
WNDCLASSA MainWindow = { 0 };
MainWindow.hbrBackground = (HBRUSH) COLOR_WINDOW;
MainWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
MainWindow.hInstance = hInst;
MainWindow.lpszClassName = "WindowClass";
MainWindow.lpfnWndProc = WndProc;
ATOM result = RegisterClassA(&MainWindow);
if (!result)
{
MessageBoxA(NULL, "Failed to register window class", "Error", MB_OK);
return -1;
}
MSG msg = { 0 };
//here the app goes on
//here is the start of the debug function
void EnableDebug(){
if (AllocConsole() == 0)
{
MessageBoxA(NULL, "Unable to create a debug window!", "Error", MB_OK);
return;
}
freopen("CONOUT$", "w", stderr);
SetConsoleTitleA("Debug Window");
clog.clear();
【问题讨论】:
-
首先在后台启动您的程序?
-
不完整、无用的 sn-p 代码应该提供什么价值?
-
您要解决什么问题?这个问题只是在问,如何解决您提出的解决方案。
-
您似乎没有理解这个问题。代码的 sn-p 显示了我如何启动我的应用程序和控制台窗口。我要问的是当我关闭控制台时如何使应用程序不关闭。
-
如果您仅出于调试目的创建控制台 - 您可以轻松地在进程中创建常用窗口(例如创建单独的线程,
WC_EDITwindows)并将其 printf 用于调试