How does a wxWidgets Framework Program obtain input parameters of a program?

Source: Internet
Author: User
Tags wxwidgets
How does a wxWidgets Framework Program obtain input parameters of a program?
For the console program, the input parameters of the program are obtained through two parameters of the main function: argv argc. With these two parameters, we can obtain the input parameters, the execution method of the program is determined based on the parameters. How can I know the input parameters of a Windows program written in the wxWidgets framework? In fact, the app object in the wxWidgets framework has two members: argv argc stores the input parameters of the program. The data type and usage of argv argc are the same as those of the main function of the console program. The only difference is that for console programs, argv and argc are passed in as parameters of the main function, which is a feature of the era of structured programming. Today, object-oriented systems are all over the world. Although the names of argv and argc have not changed, they have changed to member variables of APP objects. Although the modern windows program still has a main function such as winmain (), which implements a message loop, almost all programming frameworks hide the main function, programmers do not deal with it directly. Okay, so much nonsense. We can get the input parameters and number of parameters through argv and argc members of the app object, and then assign them to the main dialog box, in the main dialog box, let's take a look.

The following example program is based on the wxWidgets dialog box. It implements an asynchronous msgbox. You can use ShellExecute in the main program to call this program and pass in the information you want to display with parameters. The advantage of doing so is that the main program will not be blocked. If it is slightly improved, such as blocking multiple instances, debugging information can be output in the loop, neither blocking the main program, there will be no large number of dialogs.

//---------------------------------------------------------------------------
//
// Name: asynmsgapp. cpp
// Author: Administrator
// Created: 21:23:07
// Description:
//
//---------------------------------------------------------------------------

# Include "asynmsgapp. H"
# Include "asynmsgdlg. H"

Implement_app (asynmsgdlgapp)

Bool asynmsgdlgapp: oninit ()
{
Asynmsgdlg * dialog = new asynmsgdlg (null );

// Pass the input parameters to the main form;
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: 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: 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, 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
// 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 (404,129 );
Center ();

Wxstatictext1 = new wxstatictext (this, struct, wxt ("wxstatictext1"), wxpoint (378), wxsize (, 40), struct, wxt ("wxstatictext1 "));
Wxstatictext1-> setfont (wxfont (9, wxswiss, wxnormal, wxnormal, false, wxt (" ")));

Wxbutton1 = new wxbutton (this, id_wxbutton1, wxt ("OK"), wxpoint (152,64), wxsize (), 0, wxdefavalivalidator, 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: 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
// 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 = 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
// 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
// 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

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.