转载:https://blog.csdn.net/what951006/article/details/72729448

一、创建一个Win32项目

VC++ 实现程序重启-LMLPHP

二、窗口处理函数中,Create窗口时创建一个按钮,并在按钮点击事件中处理程序重启

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc; switch (message)
{
case WM_CREATE:
{
//创建按钮
CreateWindow(L"Button", L"重启", WS_VISIBLE | WS_CHILD,
, , , , hWnd, (HMENU)IDC_BUTTON_RESTART, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
}
break; case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDC_BUTTON_RESTART://重启按钮的响应
{
//::PostMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, NULL);
TCHAR szModuleName[MAX_PATH * ];
::GetModuleFileName(NULL, szModuleName, _countof(szModuleName) - ); STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW; PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi)); if (CreateProcess(NULL, szModuleName, NULL, NULL, FALSE, , NULL, NULL, &si, &pi))
{
// 既然我们不使用两个句柄,最好是立刻将它们关闭
::CloseHandle(pi.hThread);
::CloseHandle(pi.hProcess);
} ::PostMessage(hWnd, WM_QUIT, , );
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return ;
}
05-08 08:25