我尝试了一个窗口示例,C 中的 window.c,并在我的窗口 7 上通过 gcc 将其编译为 window.exe。当我在命令控制台上运行 window.exe 时,它可以工作,但命令控制台无法退出,所以我不能在同一控制台中运行其他命令,为什么?当我在命令控制台上尝试 putty.exe 或其他带有 exe 扩展名的窗口应用程序时,它的命令控制台将在程序运行或加载时退出。是否是因为 window.c 中没有包含 main() ?另一个问题是为什么window.c中的代码可以在程序中不包含main()的情况下以自执行模式运行? 请指教
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 300,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
最佳答案
it works but the command console can NOT exit out so I can not run other command in the same console, Why ? When I try putty.exe or other window application with exe extension on command console, its command console will exit out when the program is ran or loaded.
从控制台启动的 Windows 应用程序不应阻止与控制台窗口的进一步交互。我对putty.exe一无所知,所以让我们以notepad.exe为例。这是一个标准的 Windows 应用程序,所以如果我从命令行运行它,记事本会在一个新窗口中启动,我会得到一个 C: 提示符。你认为这是正常的是完全正确的。
正如您在此处显示的那样,您的代码将正常运行。还有其他问题 — 可能是您的编译器或链接器设置。我建议您可能以控制台子系统为目标,但链接器应该会提示您的入口点错误。 (至少微软的工具链会;我不太确定 GCC。)至少这是要检查的东西。确保在构建时传递开关 -mwindows
。
Whether the reason is there is no main() including in the window.c or not ? And other question is why the code in the window.c can be ran in self-executed mode without main() including in the program ?
不,这与它无关。代码中没有main()
函数的原因是WinMain()
函数作为入口点。
从技术上讲,WinMain()
是用户定义 的入口点。这是 the conventional name and signature for the function in Windows applications .你可以认为它类似于 main()
,但实际上,main()
函数仍然存在,它只是隐藏在库代码中。您在构建 Windows 应用程序时链接的 C 运行时库定义了实际的入口点。它执行一些早期的初始化任务,然后调用用户定义的入口点 WinMain()
。
如果您在调试器下单步执行您的应用程序,您可以看到这一点,但如果您将断点放在 WinMain()
的顶部,您就会错过它。尽管这是您的 代码首先开始执行的地方,但正如我所解释的,在将控制权移交给您的WinMain()
之前,有一些代码首先执行。因此,您需要从 EXE 中的第一条指令开始单步执行。在 Visual Studio 中,您只需按 F11 即可进入。我不确定您使用的是什么调试器;您需要查看其文档。
或者您根本不用担心,因为它本质上是深奥的,您永远不需要真正理解。在编写 Windows 应用程序时,只需将 WinMain()
视为等同于 main()
即可!
关于c - 如何在 Window 7 后台运行 C 窗口程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37906719/