WxWidgets beginner Guide (3) -- wxWidgets application initial experience

Source: Internet
Author: User
Tags wxwidgets

WxWidgets full directory for beginnersPDF and attachment download 1 Preface
2. Download and install wxWidgets
3 wxWidgets application initial experience
4 wxWidgets learning materials and usage instructions
5. Use wxSmith for Visual Design
Appendix: learning material list

3 wxWidgets application initial experience

All the experiences in this article are carried out in Code: Blocks.

To compile and run the wxWidgets program written in C ++ in Code: Blocks, you need to make some further settings.

First, add a wxWidgets root directory environment variable to the environment variable. The setting method is similar to setting the PATH variable in 2.3. In Win7, right-click the "computer" icon on the desktop, select "properties" from the menu, and complete a series of operations from 1 to 5 in the "System Properties" dialog box. The new variable is named wxwin with the value X: \ wxWidgets-3.0.0.

The following settings should be performed in Code: Blocks.

Open Code: Blocks, select Settings-> Global Variables ..., Create a new wx variable under set default. Under Build-in fields:, enter "$ {wxwin}" in base (wxwin is a variable just set ), enter "$ {wxwin} \ include" in "include", and "$ {wxwin} \ lib" in "lib". These are part of the "Environment" required for development.


3.1 create and run a GUI application from an empty project

The following describes how to create an empty project from scratch and make a simple application. The program is adapted from the First program in the "First programs in wxWidgets" section of the online tutorial wxWidgets tutorial (http://zetcode.com/gui/wxwidgets. This tutorial will be one of the main tutorials in the recommended learning solution.


3.1.1 create a project

The project creation process is as follows:

(1) choose "File"> "New"> "Project…" from the menu ...", Select "Empty project" to create an Empty project

  

(2) After clicking the "go" button, you will have a welcome page. Click "next". The page appears and enter the project name. The name of the project I created is wxTest.

(3) After clicking "next", select the compiler and the generated target file type, and select

(4) Click "Finish" to generate an empty project,

(5) click File> New> File... to create a source program File for the project. In the dialog box that appears consecutively, select "C/C ++ source" (that is, the source file) as the file type to be added ), in the dialog box, select "C ++ ". Next, in the displayed dialog box, the source file name with the complete path (main. cpp is used in this example) is displayed. Check the Debug check box.

(6) Click "Finish" and input (or paste) the following source code to the main. cpp file.

#include <wx/wx.h>class Simple : public wxFrame{public:    Simple(const wxString& title);}; Simple::Simple(const wxString& title)   : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150)){  Centre();} class MyApp : public wxApp{  public:    virtual bool OnInit();}; IMPLEMENT_APP(MyApp) bool MyApp::OnInit(){    Simple *simple = new Simple(wxT("Simple"));    simple->Show(true);    return true;}

Shows the project after source code is added:

You may not consider the meaning of the statements in the program for the time being. It is our current task to complete the complete process of running the program. You can run the program, and then look at the "Portal ".

Next we will compile the project and see the running result.


3.1.2 compile and run a project

Select the "Build" option in the "Build" menu (or the corresponding button in the toolbar) to compile and connect the project. The first line of the program is an error. The error message is:

fatal error: wx/wx.h: No such file or directory

That is to say, the header file wx \ wx. h to be included cannot be found.

You need to set "Search Path. Select Project> Build options... and set Compiler in the Search directories tab. Add directories X: \ wxWidgets-3.0.0 \ lib \ gcc_dll \ mswud and X: \ wxWidgets-3.0.0 \ include through Add, as shown in:

  

Then Build, there will be no syntax errors.

There are a lot of error prompts, from the connection link. The problem is that the library file cannot be found.

Choose Project> Build options.... On the Linker settings tab, add the target file to be connected "., Add all. a files in the X: \ wxWidgets-3.0.0 \ lib \ gcc_dll folder through the Add button (in fact, you can select a few of them. Because I don't know which ones are needed, it is the most convenient way to select all ):

  

Then compile, 0 errors, 0 warnings. Successful!

However, an error occurs when running the program, such:

Follow the prompts to find the wxmsw30ud_gcc_custom.dll file in X: \ wxWidgets-3.0.0 \ lib \ gcc_dll, copy it to the folder where the project is located, and then run it to see the expected window ,.

The preceding running error occurs because I added the. a file in the lib \ gcc_dll folder to Linker settings. These files belong to the "Dynamic Link Library" (This term can be used by Baidu ). The advantage of this method is that the compilation speed is fast and the target code is small. However, when the compiled program runs, you must find the required. dll file. The simplest way is to copy the. dll file.

This program is short and the result is just an empty window. However, it is sufficient to compile and run the window program.

The above settings and file replication can be completed in advance after "experience. The method described above is to give readers more feelings about the problems in each link.


3.2 use the Code: Blocks Wizard to create an application

Another way to create a wxWidgets Project in Code: Blocks is to develop the application through the "Wizard. This method is not widely used and can be understood.

The materials below this section are not original to the author and are organized from http://www.cnzui.com/archives/962.

The specific steps for development using the wizard are as follows:

(1) choose "File"> "New"> "Project…" from the menu ...", Select the wxWidgets project at the end.

  

(2) Click "Go" to enter the project Configuration Wizard. A welcome window will be displayed first. Click "next" and select "Install wxWidgets.

Note: In Code: Blocks 13.12, wxWidgets 3.0.x is supported. You must select the correct version. Otherwise, the application can be generated but cannot be correctly connected and run.

(3) Press "Next", enter the project name "wxtest", and select the folder to save the project.

  

(4) continue "Next" and enter the author and some copyright notice information.

  

(5) continue "Next", select the GUI Design Tool and program type, and use wxSmith and Dialog based.

  

(6) Press "Next", and the Next step is some settings in the wxWidgets environment. Here we enter the wxWidgets root directory we just set and enter "$ (# wx)" directly).

  

(7) Press "Next". here we can see that the MinGW compiler is selected by default. We only use the Debug version below, so we only select "Create" Debug "configuration ".

  

(9) continue to "Next". Next, you need to select how to use the wxWidgets library. Here, we will refer to the compiled wxWidgets library.

  

(10) Press "Next". Because "Configure Advanced Options" is selected, you need to make more settings for the wxWidgets library, here we choose to use lib for calling (the SHARED = 0 parameter is required for compiling wxWidgets to generate a static library file ).

  

(11) Press "Next" to go to the last step. We will select the library we need. If we don't know, We will select all the databases we need.

  

(12) Click "Finish" and the project is created successfully.

In this case, you can view the automatically generated files in the project, including the. cpp source files and. h header files. Read the program carefully, which is similar to the program entered in 3.1.

In fact, the role of the Wizard is to automatically generate an application by using a series of options.

The running result of the application created in the preceding step is:

  

Some errors may occur during compilation and running. This is generally not a problem with the program itself, but is caused by incomplete Code: Blocks compilation environment and running support files.

See section 3.1.2, which may help you solve the problem and make the program run correctly.




======================= Author he Lijian CSDN blog = | = Category of IT student growth guidance column (occasionally updated) ==||== C ++ online class column he Lijian Course Teaching Link (by course grade) ==|||== my book-"attacking the university against the normal energy passed to IT students" ==|==== paving the runway for IT cainiao to take off, A university that enjoys happiness and passion with its students ====



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.