我是一个初学者,试图理解 GUI 在几秒钟后冻结的原因。尝试运行它,看看我的意思.. 它不应该永远打印 Hello1 Hello2 Hello3 Hello4 和 Hello5 吗?需要明确的是:停止工作的是 SetWindowText(),所以冻结的是 GUI。我的意思是,应用程序一直在运行,只是“显示文本”部分崩溃了。我知道这一点,因为我在发布之前对其进行了调试。那我错过了什么?为什么会结冰?你知道吗?提前致谢
这是main.cpp:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "resource.h"
using namespace std;
HINSTANCE hInst;
void sayHello(HWND handle)
{
BOOL working = true;
while (working)
{
SetWindowText(handle, "Hello1");
SetWindowText(handle, "Hello2");
SetWindowText(handle, "Hello3");
SetWindowText(handle, "Hello4");
SetWindowText(handle, "Hello5");
}
}
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
return TRUE;
case WM_CLOSE:
EndDialog(hwndDlg, 0);
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BTN_QUIT:
EndDialog(hwndDlg, 0);
return TRUE;
case IDC_BTN_TEST:
HWND handle = GetDlgItem(hwndDlg, 1003);
sayHello(handle);
return TRUE;
}
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst = hInstance;
return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}
这是资源.h:
#include <windows.h>
#define DLG_MAIN 101
#define IDC_BTN_TEST 1001
#define IDC_BTN_QUIT 1002
#define ID_CONTROLSTATIC 1003
这是resource.rc:
#include "resource.h"
DLG_MAIN DIALOGEX 6, 5, 194, 106
CAPTION "Test Application"
BEGIN
CONTROL "&Test", IDC_BTN_TEST, "Button", 0x10010000, 138, 5, 46, 15
CONTROL "TEXT HERE", ID_CONTROLSTATIC, "Static", 0x10010000, 35, 35, 50, 8
CONTROL "&Quit", IDC_BTN_QUIT, "Button", 0x10010000, 138, 29, 46, 15
END