简体   繁体   English

C ++ Winapi while循环解决方法?

[英]C++ Winapi while-loop workaround?

So, I have this ordinary while loop (as shown) and while my program is running inside the while loop for an extended period of time, the window becomes unresponsive, saying "blah blah blah isn't responding." 因此,我有一个普通的while循环(如图所示),并且当我的程序在while循环内运行了很长一段时间后,该窗口变得无响应,并说“等等等等没有响应”。 Well... I know in WinAPI I should use the SetTimer() function and WM_TIMER, but guess what! 好吧...我知道在WinAPI中我应该使用SetTimer()函数和WM_TIMER,但是猜猜是什么! I'm programming for fun this time, and I would like to know if there is a workaround... such as putting something inside my while loop that will keep it "responsive". 这次我正在编程,很有趣,我想知道是否有解决方法...例如在while循环中放入一些东西,以使其保持“响应”状态。 I am not trying to be destructive. 我不是要破坏性。

Here is the Code: 这是代码:

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;
        }
    }

Is what I'm asking possible? 我要问的可能吗?

I appreciate any answers, even "Nope can't do that, You better use WM_TIMER!" 我很感激任何回答,甚至“不行,您最好使用WM_TIMER!” so thanks in advance for answers! 所以在此先感谢您的解答!

  • Operating system: windows 7 ultimate 操作系统:Windows 7 Ultimate

  • Compiler: Dev-C++ 编译器:Dev-C ++

  • Other: using winapi 其他:使用winapi

As you already know, you should use timers to handle your logic... 如您所知,您应该使用计时器来处理逻辑...

But if you really do not want to do this, you can use "DoEvents" do keep the window "responsive"... 但是,如果您真的不想这样做,则可以使用“ 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);
    }
  }
}

And in your while-loop you just need to call "DoEvents" periodically... 在while循环中,您只需要定期调用“ DoEvents”即可...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM