Chapter 2 feelings (II) -- 4th. Hello Internet

Source: Internet
Author: User
Tags network function wxwidgets
[Back To Directory] vernacular C ++

4.4.hello Internet

(The animated version of this tutorial is provided on the supporting CD)

In this section, we will use the network function of the wxWidgets library to access the network resources provided by www.d2school.com. The process is: Enter the name in the edit box of the window and click the established button. The program uses the components provided by wxWidgets to access the preset network link of "second school, get a greeting from the Internet.

Connect your computer to the Internet. Perform a test: Open the browser and enter the following content in the address bar:

Http://www.d2school.com/hello.php? Name = Ding Xiaoming

After you press enter, the correct situation is that you will see in the browser:

"Hello Ding Xiaoming! Welcome to d2school ......".

4.4.1. Create a project

First, you need to create a wxWidgets framework, which is similar to the routine in section 4.1. Apart from changing the project name to "hellointernet", the biggest difference is that the last step in the "wxWidgets Projects" Project Wizard, we need to select three additional library modules:

Figure 84 wxnet module required for hello Internet

 

4.4.2. Interface Design

After the Wizard is complete, code: blocks also opens the default dialog box design page. The file name is "hellointernetdialog. WXS ". We will transform the default Interface.

The final transformation result is:

Figure 85 final design of Hello Internet

You can design the interface based on the content in the previous two sections. The following are some tips.

1. The "about" and "quit" buttons are also from the "about" and "quit" buttons;

2. The "proportion" of the "Welcome to wxWidgets" label is 1, while the two "boxsizer" and "proportion" on the left and right are both 0.

Third, the font attribute of the "Welcome to wxWidgets" label has been modified ("", "chinese_gb2312" Character Set, and "4th "). The length and width of the control are deliberately extended.

4. The "title" attribute of the dialog box is "Hello Internet ".

4.4.3. write code

Double
Click the "greeting" button on the design interface. Code: blocks will automatically generate its "onclick" event function for this button, and automatically switch to the code bit.
Set: the bottom of the hellointernetmain. cpp file. However, please first move the location to the top of the file. We will first add the header file required for this code writing.

  • Include header files

#include "HelloInternetMain.h"
#include <wx/msgdlg.h>

013 #include <wx/protocol/http.h>
014 #include <wx/mstream.h>

//(*InternalHeaders(HelloInternetDialog)


<Wx/protocol/HTTP. h> is used to introduce the wxhttp control, which allows you to conveniently access network resources of an HTTP protocol. WxWidgets provides simpler wxurl controls. However, compared with wxhttp, wxhttp does not support automatic encoding of Chinese Characters in URLs.

Tip: transcode Chinese characters using URLs

Open your browser and repeat the small test mentioned at the beginning of this section. This time, after entering the URL and pressing enter, let's take a look at the address bar of the browser. Its content is:

Http://www.d2school.com/hello.php? Name = % B6 % a1 % D0 % a1 % C3 % F7

The following % character is a Transcoding of the Chinese character "ding Xiaoming.

To better understand this section, we recommend that you learn the basics of HTTP.

 

Slave
The reply obtained by the above URL in the second school also contains Chinese characters and adopts the "gb2312" Character Set of non-unicode encoding. It must be converted to unicode encoding before it can be used.
It can be displayed on the "Unicode version" wxWidgets graphical interface. During the conversion process, we need to use "wxmemoryoutputstream", which comes from
Header file: <wx/mstream. h>.

  • Function: fromgb2312

Before double-clicking the onbutton3click function generated by the greeting button, add a function to convert gb2312 to Unicode.



#117 wxString FromGB2312(wxStreamBuffer const * buf)
{
return wxString ((char const *)buf->GetBufferStart()
, wxCSConv(wxT("gb2312"))
, buf->GetBufferSize());
}

With wxWidgets, you can easily convert the string of the gb2312 character set to unicode encoding. The specific meaning of the Code is described in this chapter.

TIPS: wxWidgets supports Chinese Characters

On the surface, wxstring only supports Chinese characters of "gb2312" (if the above Code converts "gb2312" to "GBK", an exception will occur during running ), but in fact, this routine can support some remote Chinese characters not included in "gb2312", such as "Hangzhou, Shanghai.

  • Function: onbutton3click

Finally, we complete the onbutton3click function:

#124 void hellointernetdialog: onbutton3click (wxcommandevent & event)
{
Wxhttp HTTP;

// Try to connect to the website:
If (! HTTP. Connect (_ T ("www.d2school.com ")))
{
Wxmessagebox (_ T ("cannot connect to second school! "));
Return;
}

// Assemble the URL:/Hello. php? Name = Ding Xiaoming
Wxstring url = _ T ("/Hello. php? Name = ");
URL + = This-> textctrl1-> getvalue ();

// An HTTP "input stream" refers to the content returned from the website.
Wxinputstream * In = http. getinputstream (URL );

If (! In)
{
Wxmessagebox (_ T ("cannot get the input stream of the specified URL! "));
Return;
}

// Read the HTTP returned content into a "memory stream"
Wxmemoryoutputstream MEM;
In-> Read (MEM );

// After reading, the input stream can be released.
Delete in;

// Convert the content in the memory stream to unicode encoding
Wxstring result = fromgb2312 (MEM. getoutputstreambuffer ());

// Display
Statictext1-> setlabel (result );
}

Save the project. Then compile and run the program. The following shows the running result:

Figure 86 Hello Internet running result

Because this project still uses a "static library", if you are interested, you can also compile a release version and then "publish" it to your friend's machine to run it.

 

Vernacular C ++

[Back To Directory]
Related Article

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.