[Wxwidgets]_[0 basic]_[Regular Update progress bar program]

Source: Internet
Author: User
Tags wxwidgets


Scene:

1. Depending on the progress of the program, the business needs to be processed to update the progress bar, which is designed to let users know the progress of the business process. A progress bar program is more friendly and lets the user know in the program execution. Not without reaction.

2. Progress on these two approaches has now been updated. The sending of events (signals, queues) allows the main thread to update the progress bar according to the order in which it is sent, one is to set a global shaping variable,

Update the progress bar by executing the timer. The first is not suitable for the update frequency is higher, for example, a second sent 20 events, which will cause the main thread busy processing the event interface appears suspended animation state.

So the best way is to use the 2nd to update the progress bar through the timer, set a reasonable value, such as 500 milliseconds to see both the progress of the update, and will not make the main thread too busy.

3. Here are examples of wxwidgets. In fact, Mfc,wtl,cocoa are all the same.


Part of the code:

My_thread.cpp

#include "domain/my_thread.h" #include "window/my_frame.h" Wxthread::exitcode mythread::entry () {Sleep]; int i = 1; while (! Testdestroy () && i <= {        if (type_) {///first method, send event update progress bar, note that the progress bar control cannot be updated directly because the non-main thread cannot update the control wxcommandevent* EVT = new Wxcommandevent (wxevt_command_mythread_update); Evt->setint (i); Wxqueueevent (m_phandler-> Geteventhandler (), evt);} else{//another way to update the INT member variable directly in the MyFrame, the Wxtimer will actively update the progress bar control based on the value itself. M_phandler->progress_value_ = i; Sleep (200);} ++i;} 1. Finally, it is necessary to tell Wxtimer that it will be over. In short, the Notification interface task is finished, you do the things you do. Wxqueueevent (M_phandler->geteventhandler (), New Wxcommandevent (wxevt_command_mythread_completed)), return ( Wxthread::exitcode) 0;}

My_frame.cpp

#include "window/my_frame.h" #include "wx/xrc/xmlres.h" #include <iostream> #include "domain/my_thread.h" using namespace Std;    Begin_event_table (MyFrame, Wxframe) Evt_command (Wxid_any, Wxevt_command_mythread_update, MyFrame::OnThreadUpdate) Evt_command (Wxid_any, wxevt_command_mythread_completed,myframe::onthreadcompletion) EVT_TIMER (wxID_ANY,MyFrame: : OnTimer) end_event_table () wxdefine_event (wxevt_command_mythread_completed, wxthreadevent); Wxdefine_event (wxEVT_   Command_mythread_update, wxthreadevent); void Myframe::init () {Gauge_ = Xrcctrl (*this, "M_gauge1", WxGauge);   Event_update_button_ = Xrcctrl (*this, "M_button1", Wxbutton); Event_update_button_->bind (wxevt_command_button_clicked, Wxcommandeventhandler (MyFrame::OnEventUpdate), this,   Xrcid ("M_button1"));   Timer_update_button_ = Xrcctrl (*this, "M_button3", Wxbutton); Timer_update_button_->bind (wxevt_command_button_clicked, Wxcommandeventhandler (MyFrame::OnTimerUpdate), this,   Xrcid ("M_button3")); Timer. SeTowner (This->geteventhandler ());} Myframe::myframe (wxwindow* parent) {progress_value_ = 0;} Myframe::~myframe () {}void myframe::onthreadupdate (wxcommandevent& evt) {cout << onthreadupdate << Endl;gauge_->setvalue (evt. GetInt ());} void Myframe::onthreadcompletion (wxcommandevent&) {cout << "onthreadcompletion" << endl;if (!type_) { Timer. Stop (); Gauge_->setvalue (Progress_value_);} Timer_update_button_->enable (); event_update_button_->enable ();} void Myframe::ontimer (wxtimerevent&) {cout << "OnTimer" << endl;gauge_->setvalue (Progress_value_) ;}    void MyFrame::D otask (bool type) {timer_update_button_->enable (false); event_update_button_->enable (false);    MyThread *m_pthread = new MyThread (type,this); if (m_pthread->run () = wxthread_no_error) {cout << ' Can ' t create the thread!: ' << type <& Lt        Endl       Delete M_pthread;   M_pthread = NULL; }}void myframe::ontimerupdate (wxcommandevent& evt) {cout << "Ontimerupdate" << endl;gauge_->setvalue (0);p rogress_value_ = 0;type_ = False;timer. Start (+);D otask (type_);} void Myframe::oneventupdate (wxcommandevent& evt) {cout << "oneventupdate" << endl;gauge_->setvalue (0);p rogress_value_ = 0;type_ = True;dotask (type_);}

Gui.cpp

#include "wx/wxprec.h" #ifndef wx_precomp #include "wx/wx.h" #endif # include "Wx/xrc/xmlres.h" #include <assert.h> #include <iostream> #include <stdio.h> #include <io.h> #include <fcntl.h> #include "wx/ Listctrl.h "#include" wx/grid.h "#include" wx/event.h "#include" window/my_frame.h "using namespace std;static void    Openconsole () {allocconsole ();    HANDLE HANDLE = GetStdHandle (Std_output_handle);    int hcrt = _open_osfhandle ((long) handle,_o_text);    FILE * HF = _fdopen (HCRT, "w");    *stdout = *HF;    }static wxstring getapprundirectory () {wxstring directory (wxtheapp->argv[0]); Directory.    Replace (WxT ("\ \"), WxT ("/")); wxstring str = directory.    BeforeLast ('/'); if (str.    IsEmpty ()) {return WxT ("."); } return directory. BeforeLast ('/');} Static wxstring Getinstalldirectory () {wxstring directory = getapprundirectory (); #ifdef __qxwin32__ return directory. BeforeLast ('/'); #else return directory; #endif}class Myapp:public wxapp{public:virtual bool OnInit (); virtual int OnExit ();};i Mplement_app (MYAPP)//' Main program ' equivalent:the program Execution "starts" Herebool Myapp::oninit () {if (!wxapp::oni  NIT ()) {return false;  } openconsole ();  Wxinitallimagehandlers ();    Wxxmlresource::get ()->initallhandlers ();  Wxstring Ui_dir = Getinstalldirectory () +wxt ("/themes");  Wxxmlresource::get ()->loadallfiles (Ui_dir);  MyFrame *frame = new MyFrame (NULL);  BOOL loaded = Wxxmlresource::get ()->loadframe (frame, NULL, "MyFrame1");  ASSERT (loaded);  Frame->init ();  Frame->show (TRUE); return true;} int Myapp::onexit () {return 0;}

:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvaw5mb3dvcmxk/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">


Full Generation code Download:

http://download.csdn.net/detail/infoworld/8105011


[Wxwidgets]_[0 basic]_[Regular Update progress bar program]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.