wxWidgets 架構程式如何擷取程式的傳入參數

來源:互聯網
上載者:User
wxWidgets 架構程式如何擷取程式的傳入參數?
對於控制台程式,程式的傳入參數是通過main函數的兩個參數:argv argc 來得到的,通過這兩個參數,我們就可以獲得傳入的參數,並且根據參數決定程式的不同執行方式。那麼對於wxWidgets架構下編寫的Windows程式,用什麼辦法知道程式的傳入參數呢,實際上,wxWidgets架構下的App對象,本身即具有兩個成員:argv argc 儲存的正是程式的傳入參數,其資料類型和使用方法和控制台程式main函數的兩個參數argv argc一模一樣。唯一不同的是,對於控制台程式,argv 和argc是做為main函數的參數傳進來的,是結構化編程時代的特徵。而如今物件導向一統天下,argv和argc雖然名稱沒變,但卻已搖身一變為app對象的成員變數。現代的Windows程式,雖然仍然是有WinMain()這樣的主函數,在其中實現了一個訊息迴圈,但幾乎所有的編程架構都把主函數深深的隱藏,程式員並不直接跟它打交道。好了,廢話說了這麼多,我們通過APP對象的成員argv、argc擷取傳入的參數和參數個數,然後把它們賦值給主對話方塊即可,在主對話方塊中如何響應,自己看著辦吧。

下面這個樣本程式是基於wxWidgets對話方塊的程式,實現的功能是一個非同步MsgBox,在主程式中用ShellExecute調用這個程式即可,並且用參數傳入想要顯示的資訊。這樣做的優點是不會阻塞主程式,如果稍加完善,比如阻止運行多個執行個體,就可以用來在迴圈中輸出調試資訊,既不會阻塞主程式,也不會湧現大量對話方塊。

//---------------------------------------------------------------------------
//
// Name:        AsynMsgApp.cpp
// Author:      Administrator
// Created:     2008-5-23 21:23:07
// Description:
//
//---------------------------------------------------------------------------

#include "AsynMsgApp.h"
#include "AsynMsgDlg.h"

IMPLEMENT_APP(AsynMsgDlgApp)

bool AsynMsgDlgApp::OnInit()
{
    AsynMsgDlg* dialog = new AsynMsgDlg(NULL);

    //把傳入的參數傳遞給主表單;
    long i;
    for (i=1;i<argc;i++)
    {
        switch(i)
        {
            case 1:
                strcpy(dialog->arg1,argv[i]);
                break;
            case 2:
                strcpy(dialog->arg2,argv[i]);
                break;
            case 3:
                strcpy(dialog->arg3,argv[i]);
                break;
        }
    }

    SetTopWindow(dialog);
    dialog->Show(true);
    return true;
}

int AsynMsgDlgApp::OnExit()
{
    return 0;
}

//---------------------------------------------------------------------------
//
// Name:        AsynMsgApp.h
// Author:      Administrator
// Created:     2008-5-23 21:23:07
// Description:
//
//---------------------------------------------------------------------------

#ifndef __ASYNMSGDLGApp_h__
#define __ASYNMSGDLGApp_h__

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#else
    #include <wx/wxprec.h>
#endif

class AsynMsgDlgApp : public wxApp
{
    public:
        bool OnInit();
        int OnExit();
};

#endif

//---------------------------------------------------------------------------
//
// Name:        AsynMsgDlg.cpp
// Author:      Administrator
// Created:     2008-5-23 21:23:07
// Description: AsynMsgDlg class implementation
//
//---------------------------------------------------------------------------

#include "AsynMsgDlg.h"

//Do not add custom headers
//wxDev-C++ designer will remove them
////Header Include Start
////Header Include End

//----------------------------------------------------------------------------
// AsynMsgDlg
//----------------------------------------------------------------------------
//Add Custom Events only in the appropriate block.
//Code added in other places will be removed by wxDev-C++
////Event Table Start
BEGIN_EVENT_TABLE(AsynMsgDlg,wxDialog)
    ////Manual Code Start
    ////Manual Code End

    EVT_CLOSE(AsynMsgDlg::OnClose)
    EVT_INIT_DIALOG(AsynMsgDlg::AsynMsgDlgInitDialog)
    EVT_BUTTON(ID_WXBUTTON1,AsynMsgDlg::WxButton1Click)
END_EVENT_TABLE()
////Event Table End

AsynMsgDlg::AsynMsgDlg(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxDialog(parent, id, title, position, size, style)
{
    CreateGUIControls();
}

AsynMsgDlg::~AsynMsgDlg()
{
}

void AsynMsgDlg::CreateGUIControls()
{
    //Do not add custom code between
    //GUI Items Creation Start and GUI Items Creation End.
    //wxDev-C++ designer will remove them.
    //Add the custom code before or after the blocks
    ////GUI Items Creation Start

    SetTitle(wxT("AsynMsg"));
    SetIcon(wxNullIcon);
    SetSize(8,8,404,129);
    Center();

    WxStaticText1 = new wxStaticText(this, ID_WXSTATICTEXT1, wxT("WxStaticText1"), wxPoint(8,16), wxSize(378, 40), wxST_NO_AUTORESIZE, wxT("WxStaticText1"));
    WxStaticText1->SetFont(wxFont(9, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("宋體")));

    WxButton1 = new wxButton(this, ID_WXBUTTON1, wxT("OK"), wxPoint(152,64), wxSize(105,25), 0, wxDefaultValidator, wxT("WxButton1"));
    WxButton1->SetFont(wxFont(9, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("宋體")));
    ////GUI Items Creation End
}

void AsynMsgDlg::OnClose(wxCloseEvent& /*event*/)
{
    Destroy();
}

/*
 * WxButton1Click
 */
void AsynMsgDlg::WxButton1Click(wxCommandEvent& event)
{
    // insert your code here
    ::PostMessage((HWND__* )this->GetHandle(),WM_CLOSE,0,0);
}

/*
 * AsynMsgDlgInitDialog
 */
void AsynMsgDlg::AsynMsgDlgInitDialog(wxInitDialogEvent& event)
{
    // insert your code here
    this->SetLabel(arg1);
    WxStaticText1->SetLabel(arg2);
}

//---------------------------------------------------------------------------
//
// Name:        AsynMsgDlg.h
// Author:      Administrator
// Created:     2008-5-23 21:23:07
// Description: AsynMsgDlg class declaration
//
//---------------------------------------------------------------------------

#ifndef __ASYNMSGDLG_h__
#define __ASYNMSGDLG_h__

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include <wx/wx.h>
    #include <wx/dialog.h>
#else
    #include <wx/wxprec.h>
#endif

//Do not add custom headers between
//Header Include Start and Header Include End.
//wxDev-C++ designer will remove them. Add custom headers after the block.
////Header Include Start
#include <wx/stattext.h>
#include <wx/button.h>
////Header Include End

////Dialog Style Start
#undef AsynMsgDlg_STYLE
#define AsynMsgDlg_STYLE wxCAPTION | wxSYSTEM_MENU | wxDIALOG_NO_PARENT | wxCLOSE_BOX
////Dialog Style End

class AsynMsgDlg : public wxDialog
{
    private:
        DECLARE_EVENT_TABLE();

    public:
        AsynMsgDlg(wxWindow *parent, wxWindowID id = 1, const wxString &title = wxT("AsynMsg"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = AsynMsgDlg_STYLE);
        virtual ~AsynMsgDlg();
        void WxButton1Click(wxCommandEvent& event);

    private:
        //Do not add custom control declarations between
        //GUI Control Declaration Start and GUI Control Declaration End.
        //wxDev-C++ will remove them. Add custom code after the block.
        ////GUI Control Declaration Start
        wxStaticText *WxStaticText1;
        wxButton *WxButton1;
        ////GUI Control Declaration End

    private:
        //Note: if you receive any error with these enum IDs, then you need to
        //change your old form code that are based on the #define control IDs.
        //#defines may replace a numeric value for the enum names.
        //Try copy and pasting the below block in your old form header files.
        enum
        {
            ////GUI Enum Control ID Start
            ID_WXSTATICTEXT1 = 1004,
            ID_WXBUTTON1 = 1001,
            ////GUI Enum Control ID End
            ID_DUMMY_VALUE_ //don't remove this value unless you have other enum values
        };

    private:
        void OnClose(wxCloseEvent& event);
        void CreateGUIControls();

    public:
        char arg1[1000],arg2[1000],arg3[1000];
        void AsynMsgDlgInitDialog(wxInitDialogEvent& event);
};

#endif

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.