c++ - WinApi 和 C++ - 状态栏不更新

标签 c++ winapi statusbar

我的状态栏在程序执行过程中没有刷新,我不知道为什么。首先是伪代码。我已经删除了大部分,只留下想法。

#include ... (many includes)

using namespace std;

#include "MyHeaderFile.hpp"

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);

//here some global variables. Thereare more of them, I leave only teh important ones.

HWND g_hButtonStart;
MSG msg;

//----------------------------------- windows ------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{ WNDCLASSEX wc; 
  HWND hwnd;


  memset(&wc,0,sizeof(wc));
  wc.cbSize      = sizeof(WNDCLASSEX);
  wc.style           = 0;
  wc.lpfnWndProc     = WndProc;
  wc.cbClsExtra  = 0;
  wc.cbWndExtra = 0;
  wc.hInstance   = hInstance;
  wc.hCursor         = LoadCursor(NULL, IDC_ARROW);

  wc.hbrBackground = CreateSolidBrush(RGB(240,240,240));//(HBRUSH)(COLOR_WINDOW+1);
  wc.lpszMenuName  = NULL; 
  wc.lpszClassName = "WindowClass";
  wc.hIcon       = LoadIcon(NULL, IDI_APPLICATION);
  wc.hIconSm         = LoadIcon(NULL, IDI_APPLICATION);

    //window class register
  if(!RegisterClassEx(&wc))
  { MessageBox(NULL, "Rejestracja klasy okna nie powiodła się!","BMP->DXF: Błąd!",MB_ICONEXCLAMATION|MB_OK);
     return 0;
  }

  hwnd = CreateWindowEx(WS_EX_WINDOWEDGE,"WindowClass","BMP -> DXF",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  500, 
  275,
  NULL,NULL,hInstance,NULL);

  if(hwnd == NULL)
  { MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
     return 0;
  }

//group boxes, text boxes, buttons ...
//most important are the "START" button and the status bar: 

  g_hButtonStart = CreateWindowEx( 0, "BUTTON", "S T R T", WS_CHILD | WS_VISIBLE,   243, 133, 100, 30, hwnd, NULL, hInstance, NULL );

 ShowWindow( hwnd, nCmdShow );
  UpdateWindow( hwnd );


//status bar things  
  INITCOMMONCONTROLSEX icmc;
    icmc.dwSize = sizeof( INITCOMMONCONTROLSEX );
    icmc.dwICC = ICC_BAR_CLASSES;
    InitCommonControlsEx( & icmc );

    g_hStatusBar = CreateWindowEx( 0, STATUSCLASSNAME, NULL, SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd,( HMENU ) 200, hInstance, NULL );

    SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "some info" );
//status bar - end


    while(GetMessage(&msg, NULL, 0, 0))
    {   if (!IsDialogMessage(hwnd, &msg))
            {TranslateMessage(&msg); 
        DispatchMessage(&msg); }
    }

    return msg.wParam;
}

//messages
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{ bool ParameterWarning=false;
  string Info;

  switch(Message)
  {  case WM_CLOSE:
     {  DestroyWindow( hwnd );
     }
     break;

      case WM_DESTROY:
        {   PostQuitMessage(0);
            break;
        }

//here is the most important part of the code
        case WM_COMMAND:
        {   if(( HWND ) lParam == g_hButtonStart )
            {   //enabling and disabling some buttons, edit boxes etc.

                //some variables declarations and initializations...

                SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "message 1" );

                //opening the in file ...

                //reading file parameters and checking for errors ...

                SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "message 2" );

                //here some large calculations start, lasting for 30mins, for example; written in C++

                for(int N=0;N<one_of_the_variables;N++)
                {
                //calculations part 1; also writing to out file ...
                SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "message 3" );

                //calculations part 2; also writing to out file ...
                SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "message 4" );

                //calculations part 3; also writing to out file ...
                SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "message 5" );
                }



    //enabling and disabling some buttons, edit boxes etc. ...
    //closing the out file ...

    }               

    default:
            return DefWindowProc(hwnd, Message, wParam, lParam);

        }

    return 0;
}

在计算过程中,我的主窗口在标题中显示“(Not Responding)”。它不会打扰我(还)。状态栏仅显示部分消息 - 它似乎是随机的。有时只有最后一条消息,有时有两三条。但在整个计算过程中,它从未正确显示它们。我应该怎么做才能让它发挥作用?

最佳答案

没有响应 是该问题的另一个症状。 GUI 应用程序需要经常处理其消息队列。正是这种抽取消息队列的行为允许 UI 更新。您长时间运行的任务会阻止消息队列被抽取并导致您报告的各种问题。

解决方案是频繁地抽取消息队列。不要在主线程中执行长时间运行的任务,因为这会阻止您为消息队列提供服务。将这些任务移到一个单独的线程中。

关于c++ - WinApi 和 C++ - 状态栏不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31745064/

相关文章:

C++ 堆分配和内存重用

c++ - 在 C++11 中从 std::deque move 一个元素

c# - 用于检查源设备 ID 的低级键盘 Hook

c++ - 如何使用 win32 C 向对话框添加旋转控件?

uiview - 导航后如何移动某些UIView?

java - Android隐藏状态栏、兼容性

c++ - 来自 c++11 的 std::thread 问题

c++ - 为什么这个流缓冲区不工作?

windows - 如何在 Delphi 中隐藏 MDI 子窗体?

python - wxPython状态栏