我已经创建了一个简单的 W32 应用程序,以通过两个应用程序(硬编码窗口类 atm)循环。当我调用开始按钮(IDC_Start)时,一切正常,但是当我将焦点更改为 hwnd 时,应用程序挂起并且无法关闭。我只需要一个简单而干净的方法来停止循环,它在调用 IDC_Start 时开始。任何帮助将不胜感激!
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <resource.h>
#include <iostream>
using namespace std;
BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
HWND hwnd1,hwnd2;
hwnd1 = FindWindow("Notepad++",0);
hwnd2 = FindWindow("Notepad",0);
BOOL bDone;
switch(Message)
{
case WM_INITDIALOG:
// This is where we set up the dialog box, and initialise any default values
{
SetDlgItemInt(hwnd, IDC_NUMBER, 5, FALSE);
HICON hIcon, hIconSm;
hIcon = (HICON)LoadImage(NULL, "e32.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if(hIcon)
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
else
MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);
hIconSm = (HICON) LoadImage(NULL, "e16.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
if(hIconSm)
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
else
MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_START:
{
BOOL bSuccess;
int nDelay = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE);
nDelay= nDelay*1000;
int i=1;
ShowWindow(hwnd,SW_MINIMIZE);
if(bSuccess)
{
if (hwnd1 != 0&&hwnd2 != 0)
{
SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED);
while(i)
{
if(bDone!=TRUE)
{
SetDlgItemInt(hwnd, IDC_SHOWCOUNT, nDelay/1000, FALSE);
Sleep(nDelay);
SetForegroundWindow(hwnd1);
Sleep(nDelay);
SetForegroundWindow(hwnd2);
i++;
}
else
{
SetThreadExecutionState(ES_CONTINUOUS);
MessageBox(hwnd, "Stopped", "Warning", MB_OK);
break;
}
}
}
else
{
MessageBox(hwnd,"Cannot find suitable Window","AppDia",MB_OK);
}
}
else
{
MessageBox(hwnd, "Number not identified", "Warning", MB_OK);
}
}
break;
case IDC_STOP:
bDone==TRUE;
break;
}
break;
case WM_CLOSE:
EndDialog(hwnd, 0);
break;
default:
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
}