gpt4 book ai didi

c++ - Winapi C++ : DialogBox hangs when breaking a loop

转载 作者:行者123 更新时间:2023-11-28 07:37:57 24 4
gpt4 key购买 nike

我创建了一个简单的 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);
}

最佳答案

您的逻辑循环使用 Sleep,它没有为对话框提供处理消息的方法,因此它似乎挂起 - 尝试使用 Timer,例如:

static BOOL bWnd1 = TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{ case IDC_START:
{ int nDelay = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE);
nDelay= nDelay*1000;
ShowWindow(hwnd,SW_MINIMIZE);
SetTimer(hwnd, 1, nDelay, NULL);
break;
}

case IDC_STOP:
KillTimer(hwnd, 1);
break;
}
break;

case WM_TIMER:
{ HWND hwnd = (bWnd1 ? FindWindow("Notepad++",0) : FindWindow("Notepad",0));
SetForegroundWindow(hwnd);
bWnd1 = !bWnd1;
break;
}

case WM_CLOSE:
KillTimer(hwnd, 1);
EndDialog(hwnd, 0);
break;

default:
return FALSE;

这只是一个示例代码,您需要为其添加错误检查...

关于c++ - Winapi C++ : DialogBox hangs when breaking a loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16374318/

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