gpt4 book ai didi

c++ - CloseWindow 不会最小化我刚启动的进程中的窗口

转载 作者:太空宇宙 更新时间:2023-11-04 16:06:03 32 4
gpt4 key购买 nike

好吧,我想在启动窗口后将窗口最小化,但是它没有生效,我不知道我做错了什么。您能指出下面代码中的错误吗?

除了打开一个窗口外什么都没有发生。

HWND  g_hwnd;
int g_nFound;

BOOL CALLBACK FindHwndFromPID(HWND hwnd, LPARAM lParam);

HWND GetHwndFromPID(DWORD dwProcessId)
{
g_hwnd = NULL;
g_nFound = 0;

EnumWindows(FindHwndFromPID, (LPARAM)dwProcessId);

if (g_hwnd) // we found one...
return (g_hwnd);

// nothing found :-(

return (NULL);
}

BOOL CALLBACK FindHwndFromPID(HWND hwnd, LPARAM lParam)
{
DWORD dwPID2Find = (DWORD)lParam;
DWORD dwPID = 0;

if (GetWindowThreadProcessId(hwnd, &dwPID))
{
if (dwPID == dwPID2Find)
{
g_hwnd = hwnd;


return (FALSE);
}
}

return (TRUE);
}


int main
{
..../
if (!CreateProcessA(NULL, // No module name (use command line)
command_line.GetBuffer(),
NULL, // Process handle not inheritable
NULL, // Thread handle not inhberitable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{

//... error handling
return 0;
}

WaitForInputIdle(pi.hProcess, 1000);
HWND hwnds = GetHwndFromPID(pi.dwProcessId);

printf("Process Handle %d, hwnd id: %p ",pi.dwProcessId, hwnds);

CloseWindow(hwnds);

该代码应该最小化窗口,但我不知道为什么没有。

最佳答案

你的做法是错误的。 官方的和记录的方法是在调用 CreateProcess() 时使用 STARTUPINFO 结构,例如:

int main()
{
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cbSize = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_MINIMIZE;

//...

if (!CreateProcessA(..., &si, ...))
{
//... error handling
return 0;
}

//...

return 0;
}

STARTUPINFO structure

wShowWindow
If dwFlags specifies STARTF_USESHOWWINDOW, this member can be any of the values that can be specified in the nCmdShow parameter for the ShowWindow function, except for SW_SHOWDEFAULT. Otherwise, this member is ignored.

For GUI processes, the first time ShowWindow is called, its nCmdShow parameter is ignored wShowWindow specifies the default value. In subsequent calls to ShowWindow, the wShowWindow member is used if the nCmdShow parameter of ShowWindow is set to SW_SHOWDEFAULT.

ShowWindow function

nCmdShow [in]
Type: int

Controls how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides a STARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter.

...

The first time an application calls ShowWindow, it should use the WinMain function's nCmdShow parameter as its nCmdShow parameter. Subsequent calls to ShowWindow must use one of the values in the given list, instead of the one specified by the WinMain function's nCmdShow parameter.

As noted in the discussion of the nCmdShow parameter, the nCmdShow value is ignored in the first call to ShowWindow if the program that launched the application specifies startup information in the structure. In this case, ShowWindow uses the information specified in the STARTUPINFO structure to show the window. On subsequent calls, the application must call ShowWindow with nCmdShow set to SW_SHOWDEFAULT to use the startup information provided by the program that launched the application. This behavior is designed for the following situations:

• Applications create their main window by calling CreateWindow with the WS_VISIBLE flag set.

• Applications create their main window by calling CreateWindow with the WS_VISIBLE flag cleared, and later call ShowWindow with the SW_SHOW flag set to make it visible.

关于c++ - CloseWindow 不会最小化我刚启动的进程中的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156185/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com