MFC control instructions (combo box button check box radio button list control edit box hot key IP address ....)

Source: Internet
Author: User

 

Combo box control usage:

Select a control from the drop-down list. First, drag the control to the place where you want to use the control and associate it with a variable using classwizard. We noticed that the variable type is cstring. Right-click Properties. In general, you can set the content selected from the drop-down menu of the control in general property class data. In Styles, you can set the combo box control style and extended styles to set the extended style, the main setting is data. Data sets all the drop-down content of the drop-down menu ,.

In data, each row serves as the content of a row in the drop-down menu. Note that Ctrl + enter should be used for line breaks.

Add the message response function

UpdateData(TRUE);char *   sz   =   m_nihao.GetBuffer(m_nihao.GetLength());MessageBox(sz,"ss",MB_OK);

 

Button:

The button control is the simplest control. Drag the button to the desired place, right-click the ID and name of the Property setting button, or use setdlgitemtext to set the text of the button, double-click the drag button to add a message response function to the button. You can click the button to change the text of the button. Simply add the setdlgitemtext function to the Message response function, when a button is clicked, the text of the button is changed.

Int ret; updatedata (true); setdlgitemtext (idok, "pressed"); char * SZ = m_nihao.getbuffer (m_nihao.getlength (); ret = MessageBox (SZ, "SS ", mb_ OK); If (ret = idok) {setdlgitemtext (idok, "OK ");}

 

 

Check box control:

The check box control is a multiple-choice control. First, add a check box control to your user interface. When you associate variables with classwizard, you will find that the variable type of this control is bool, then we will basically understand the usage of this control. A simple way to check whether the control is pressed is to determine whether the virtual variable value of this control is true or flase. However, you first need updatedata (true );

Then we will add a message response function for the onok button:

Updatedata (true); If (m_check = true) {MessageBox ("check box press", null, mb_ OK );}

To implement our functions.

 

 

Radio button:

The purpose of this button is similar to that of check box, but the difference is that the check box control checks whether this option is selected only in the message response function to be executed, the radio button responds immediately after it is pressed and executes the message response function of this button. Therefore, this control does not have a virtual variable, but directly adds a message RESPONSE event to this control, the method is the same as that used to add buttons.

 

 

List control:

The list control is very interesting, and I use a little of it. Here I only provide one way to use it. Here we take a network sniffer I have done as an example:

Here, *** represents the project name, such as sniffer.

First, drag the control to the place where you want to use the control, open classwizard, and associate it with a variable, such as m_ctrlist. Then find *** DLG. CPP file. Add the following code to bool C *** DLG: oninitdialog () in the dialog box function (the project of the document may be different, but all of them are in the main window function) to set the space style:

DWORD dwStyle=GetWindowLong(m_CtrList.GetSafeHwnd(),GWL_STYLE);                 dwStyle&=~LVS_TYPEMASK;dwStyle|=LVS_REPORT;SetWindowLong(m_CtrList.GetSafeHwnd(),GWL_STYLE,dwStyle);

Finally, insert the column name you want to enter:

Values (0, "data", lvcfmt_left, 525); m_ctrlist.insertcolumn (0, "Destination Port", lvcfmt_left, 60); m_ctrlist.insertcolumn (0, "Source Port", lvcfmt_left, 50); m_ctrlist.insertcolumn (0, "protocol", lvcfmt_left, 40); m_ctrlist.insertcolumn (0, "Destination IP address", lvcfmt_left, 100); m_ctrlist.insertcolumn (0, "source IP address ", lvcfmt_left, 100 );

The control is basically set. The following describes how to display the text to be output:

Assume that the data in each column is in the S [3], port1, port2, and date columns.

Index = m_ctrlist.insertitem (0, S1); // create a row m_ctrlist.setitemtext (index, 1, S2); m_ctrlist.setitemtext (index, 2, S3); m_ctrlist.setitemtext (index, 3, port1 ); m_ctrlist.setitemtext (index, 4, port2); m_ctrlist.setitemtext (index, 5, date );

In this way, the correct text is displayed.

 

 

Edit box:

This control is the main input control of MFC. It can be seen from the English name.

This control is also quite simple to use. Drag the control with the mouse to the desired place and open classwizard to associate it with the virtual variable. When associating, you can select the type of the variable with int, cstring, uint .... And so on. N covers almost all common variable types. You can use it after associating variables.

UpdateData(TRUE);char *   sz   =m_edit.GetBuffer(m_edit.GetLength());MessageBox(sz,"ss",MB_OK);

In addition, you can use the edit box control to output some content. You only need to assign the content to the associated variable of the control and refresh the data.

Void caadlg: onok () {// todo: add extra validation herem_edit = "hello"; updatedata (false); // note that the updatedata parameter here is false. As for parameter problems, you can find msdn on your own}

 

 

Process:

The progress bar control is also a common control. When we drag the progress bar to the interface to add a message response function, we can see that the type of the message response function is cprocessctrl, by querying msdn, we can easily find that Microsoft provides many progress bar functions. The most common function is the three functions.

Setrange: set the maximum and minimum values of the progress bar.

Setstep: Set the progress bar step

Step 1

As for the progress bar style, you can right-click the progress bar attribute settings or the member functions in the cprocessctrl class.

First, like the list box control, we need to set the maximum and minimum values of the progress bar, step size, and other necessary attributes in the C *** DLG: oninitdialog () function, including the progress bar style.

Then we need to increase the step size as needed. We also use the network sniffer above as an example. We cycle 500 times and sniff 500 network packets, then set the minimum value of the progress bar to 0 and the maximum value to 500, if the step size is 1, you can add one step for each loop. This is easy to understand.

C *** DLG: oninitdialog () Add the following code: m_process.setrange (0,500); // set the progress bar range m_process.setstep (1); // set the progress bar step

Add the following code in the loop section:

M_process.stepit () in each loop ();

 

 

Hot Key:

The hotkey is one of the most important controls in MFC. The hotkey can easily implement one or more key combinations. If there is no hotkey, if you want to use dynamic link library DLL or other global hook methods to implement the hotkey function, it can be quite troublesome, or even difficult to solve the hotkeys of multiple combinations of a number of variable keys, so mastering the hotkeys can help us with many things.

The MFC-encapsulated hotkeys make programming quite easy. Enter the hotkey-> register the hotkey-> Fill in the message ing-> Fill in the hotkey response function, here there are two small details.

1. Based on the information, there are two methods to implement the hotkey after registering the hotkey. Here, the hotkey is written by the registerhotkey method.

2. according to the information on the Internet, there is an error between the macro definition of the MFC hotkey and the macro definition of the API function registerhotkey. This is a bug that leads to the reversal of CTRL and shift keys, however, unfortunately, I did not encounter this situation when writing a program, and I cannot draw a conclusion.

Let's take a look at how to use the hot key control.

1. Enter the hotkey according to the following steps:

Drag a hot key control to the desired location, and use classwizard to associate it with a variable, such as m_hotkey, so that the input hot key can be accepted.

2. Registration hotkey:

The purpose of this step is to register a hotkey for the window and send the wm_hotkey message to the window when the hotkey comes. We add the following code to the Message response function of our "register" button:

Word virtualcode, modifiers; m_hotkey.gethotkey (virtualcode, modifiers); // save if (! Registerhotkey (this-> m_hwnd, 100, modifiers, virtualcode) // register the hotkey {MessageBox ("hotkey setting Conflict", "error", mb_ OK );} elsemessagebox ("hotkey setting successful", "successful", mb_ OK );

3. Fill in message ing:

At this step, our hot key has been successfully registered, but we did not respond when we press the hot key, because we did not tell the computer what to do after pressing the hot key. MFC is a message-driven framework code. Therefore, we need to add message ing for the window for receiving wm_hotkey so that our hotkey function can be executed when the window receives the wm_hotkey message. Open classwizard

We did not find the wm_hotkey message in it, and Microsoft did not encapsulate it for us, so we had to do it ourselves.

Find the following code in the *** DLG. cpp file:

BEGIN_MESSAGE_MAP(CMFCHotKeyDlg, CDialog)//{{AFX_MSG_MAP(CMFCHotKeyDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDOK, OnSet)//}}AFX_MSG_MAPEND_MESSAGE_MAP()

This is a message ing processing code customized by MFC. Add on_message (wm_hotkey, myhotkey) to it. Here myhotkey is a custom hotkey processing function that we will need to define.

4. Fill in the hotkey response function:

Find the header file of *** DLG. h and add a message response function declarative afx_msg void myhotkey (wparam WP, lparam LP) in protected of the window class );

Then, add the definition of the message Response Function in the *** DLG. cpp file:

Void cmfchotkeydlg: myhotkey (wparam WP, lparam LP) {MessageBox ("The hotkey runs successfully", "success", mb_ OK );}

This basically works.

 

 

IP Address:

The IP control is also one of the commonly used controls and the simplest one. The MFC encapsulation of it is nothing more than processing a string. It is also quite easy to use.

First, drag the control to the specified place and associate a variable with classwizard. We can see that the type of the variable is either a class or an ipaddressctrl class. The following describes several common operations:

1. output the IP address to the IP Control: only two lines of code are required. If the IP address to be displayed is 192.168.1.1, add

CString a="192.168.1.1";m_ip.SetWindowText(a);

 

2. Enter the IP address in the program:

Byte A1, A2, A3, A4; m_ip.getaddress (A1, A2, A3, A4); cstring STR; Str. format ("% d. % d. % d. % d ", A1, A2, A3, A4); // The NF value here is the IP value. messageBox (STR );

 

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.