C++Winapi while loop解决方法
C++ Winapi while-loop workaround?
所以,我有一个普通的while循环(如图所示),当我的程序在while循环中运行很长一段时间时,窗口变得没有响应,说"废话废话没有响应。"好吧。。。我知道在WinAPI中我应该使用SetTimer()函数和WM_TIMER,但你猜怎么着!我这次编程是为了好玩,我想知道是否有变通办法。。。比如在while循环中放入一些东西,使其保持"响应性"。我不是想破坏别人。
这是代码:
while (battleupdate == 1)
{
RECT wrect;
wrect.left = 0;
wrect.right = 570;
wrect.top = 0;
wrect.bottom = 432;
if (FILLRECT == 0){
FillRect(hdc, &wrect, (HBRUSH) GetStockObject (WHITE_BRUSH));
cout << "battleupdated" << endl; FILLRECT = 1; }
/*OPPONENT STATS*/
if (1 == 1) { stringstream stream; stream << opname << " ";
TextOut (hdc, 20, 50, stream.str().c_str(), 12); }
//if (1 == 1) { stringstream stream; stream << itemslot4name << " ";
//TextOut (hdc, 465, 188, stream.str().c_str(), 12); }
//if (1 == 1) { stringstream stream; stream << itemslot4effect << " ";
//TextOut (hdc, 465, 204, stream.str().c_str(), 4); }
battleupdate = 1;
//I DON'T CARE IF MY DELAY CODE IS LAME YES I KNOW ABOUT SLEEP().
while (delay > 0)
{
delay -= 1;
}
if (delay == 0)
{
delay = resetdelay;
}
}
我所问的可能吗?
我很感激任何答案,即使是"不,不能那样做,你最好使用WM_TIMER!"因此,提前感谢您的回答!
操作系统:windows 7终极
编译器:Dev-C++
其他:使用winapi
如您所知,您应该使用计时器来处理您的逻辑。。。
但如果你真的不想这样做,你可以使用"DoEvents"来保持窗口的"响应性"。。。
void DoEvents()
{
MSG msg;
BOOL result;
while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
{
result = ::GetMessage(&msg, NULL, 0, 0);
if (result == 0) // WM_QUIT
{
::PostQuitMessage(msg.wParam);
break;
}
else if (result == -1)
{
// Handle errors/exit application, etc.
}
else
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
}
在while循环中,您只需要定期调用"DoEvents"。。。
相关文章:
- 有没有办法在 c++ 中同时生成随机数?如果没有,是否有解决方法?
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- 在 c++ 中解决段树以外的范围查询的有效方法是什么?
- 此解决方案中生成更改的方法数量(自上而下)有什么问题?
- 从 int 中剥离位时,编译器会警告一个转换,但不警告其他转换.有解决方法吗?
- 是否有解决方法可以在 c++ 中为 short 定义用户定义的文字?
- 不为 emplace() 定义构造函数的解决方法
- 当只有静态方法受到影响时,如何解决C++中的链接器错误?
- 删除复制构造函数的 Intel 13.1.2 中不良C++行为的解决方法
- 函数模板部分专业化-有什么解决方法吗
- 带boost的过载模糊性:可选,解决方法
- 继承构造函数和其他变量的解决方法
- 解决虚拟方法的歧义继承的两种方法
- 解决歧义的方法
- C++ 解决方法:"从类型"B*"的表达式初始化类型"C*&"的引用无效"
- 对前向声明类型进行unique_ptr的解决方法
- 必须使用尾随返回类型的示例,因为无法用旧方法解决问题
- 如何通过动态规划方法解决这个问题?
- extern可以解决这个问题吗,或者我可以通过其他方法解决这个问题吗?
- 从析构函数调用虚拟方法-解决方法