转载:https://blog.csdn.net/what951006/article/details/72729448
一、创建一个Win32项目
二、窗口处理函数中,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, 600, 240, 100, 50, 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 * 2]; ::GetModuleFileName(NULL, szModuleName, _countof(szModuleName) - 2); 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, 0, NULL, NULL, &si, &pi)) { // 既然我们不使用两个句柄,最好是立刻将它们关闭 ::CloseHandle(pi.hThread); ::CloseHandle(pi.hProcess); } ::PostMessage(hWnd, WM_QUIT, 0, 0); } 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(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }