widgets3.2.1中 wxFile文件apend写入操作、定时任务wxTimer等使用

1.内容:

   WXWIDGETS版本是3.2.1,DEMO主要有wxFile文件内容APPEND方式写入,wxTimer定时任务类使用、工具条使用、MENU菜单使用。给新手学习参考,同时做个记录,便于后续学习参考。

注:本文参考了网上代码综合编写而成,如有侵权,请联系本人修改。

2.程序代码

#include <wx/wx.h>

#include <wx/string.h>

#include <wx/utils.h>

#include <wx/timer.h>

#include <wx/file.h>

#include <wx/log.h>

#include <string>

//指定定时器的ID

#define TIMER_ID 1000

#define START_ID 1001

using namespace std;

  //框架类wxFrame

  class MyFrame : public wxFrame

  {

  private:

      wxTimer *m_timer;

   

  public:

     void writeLog( const string& txt){

          //获取当前系统时间

           wxString nowTime;

           wxDateTime now = wxDateTime::Now();

          //中国时区GMT+8,常数为A_WST

           nowTime=now.Format( "%c", wxDateTime::A_WST).c_str();

        wxFile file("log.txt", wxFile::write_append);

        file.Write(nowTime+":"+txt);

        file.Close();

     }

     

      void  OnQuit(wxCommandEvent & event){

        wxPrintf("on quit");

         wxMessageDialog *dial = new wxMessageDialog(NULL,wxT("确定要退出吗?"), wxT("Question"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);

        int ret = dial->ShowModal();

        dial->Destroy();

       if (ret == wxID_YES) {

           m_timer->Stop();

           Destroy();

        }

       }

      void  OnAbout(wxCommandEvent & event){

       writeLog("on about in\n");

             wxMessageBox( "This is a wxWidgets' Hello world sample",

                  "About Hello World", wxOK | wxICON_INFORMATION );

       }

      void  OnTimer(wxTimerEvent & event){

          wxPrintf("\n onTimer");

           //获取当前系统时间

           wxString nowTime;

           wxDateTime now = wxDateTime::Now();

          //中国时区GMT+8,常数为A_WST

           nowTime=now.Format( "%c", wxDateTime::A_WST).c_str();

          SetStatusText(nowTime);

           //动态在控制台输出当前时间

           std::cout << nowTime << std::endl;

            static int x = 0;

           if (x<1000)

            {

                x=x+10;

             //   wxLogMessage(x);

             }

      }

      void  OnStart(wxCommandEvent & event){

             wxPrintf("on start");

             m_timer->Start(1000);

       }

   

     

      MyFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(750, 550))

     {

        wxMenuBar *menuBar = new wxMenuBar();

        wxMenu *fileMenu = new wxMenu;

        wxMenu *helpMenu = new wxMenu;

        helpMenu->Append(wxID_ABOUT, "&About...\tF1", "FKU!");

        fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "FKU too!");

        menuBar->Append(fileMenu, "&File");

        menuBar->Append(helpMenu, "&Help");

        Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(OnQuit));

        Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(OnAbout));

        wxPanel *panel = new wxPanel(this, wxID_ANY);

        wxButton *button = new wxButton(panel, wxID_EXIT, wxT("Quit"), wxPoint(120, 200));

        Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(OnQuit));

        //将定时器ID和定时执行函数关联起来

        button->SetFocus();

        m_timer = new wxTimer(this, TIMER_ID);

        wxImage::AddHandler(new wxPNGHandler);

        wxBitmap exit(wxT("cha.png"), wxBITMAP_TYPE_PNG);

        wxBitmap start(wxT("start.png"), wxBITMAP_TYPE_PNG);

        wxToolBar *toolbar = CreateToolBar();

        toolbar->SetBackgroundColour(wxColour(_T("#7F7F7F")));

        toolbar->AddTool(wxID_EXIT, wxT("Exit application"), exit);

        toolbar->AddTool(START_ID, wxT("start"), start);

        toolbar->Realize();

        Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(OnQuit));

        Connect(START_ID, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(OnStart));

        SetMenuBar(menuBar);

        //添加状态栏

        CreateStatusBar();

        Centre();

        SetStatusText(wxT("Welcome to wxWidgets!"));

      }

      DECLARE_EVENT_TABLE()

 };

//应用程序类APP

 class MyApp : public wxApp

 {

 public:

     //Frame的实例化

     virtual bool OnInit()

     {

         MyFrame * pframe = new MyFrame(wxT("主程序"));

         pframe->Show(true);

         return true;

    }

 };

//声明应用程序

IMPLEMENT_APP(MyApp)

BEGIN_EVENT_TABLE(MyFrame, wxFrame)

        EVT_TIMER(TIMER_ID, MyFrame::OnTimer)

END_EVENT_TABLE()

3.程序执行结果:

日志截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值