Eject (Child) dialog box and manipulate its controls

Source: Internet
Author: User

"Pop-up dialog box"

1, add a dialog box.

To pop up a dialog box, you must first create a new dialog box. By default, you have established the main MFC scenario so that you have the Resource View tab in Solution Explorer.

"Resource View"--"Dialog"--right button "Insert Dialog"

A dialog is built.

2, add the corresponding class for dialog

Right-click "dialog"--"Add Class"-Enter the class name (assuming a) Click OK

With these two steps, a dialog box can be called.

3, Pop-up dialog box

For example, to click the OK button in the main dialog box to eject the above dialog, double-click the OK button, or right-click OK to add the message response function that you clicked.

To add:

A;

A.domodal ();

This domodal is shown in modal dialogs, where the dialog box pops up and the dialog cannot be used until the current dialog box is closed.

It is important to note that When you create a new dialog box, the default is OK and cancel two buttons, their IDs default to Idok and Idcancel, The domodal function returns a value that can be two IDs, indicating that a button was clicked. For example:

INT_PTR flag = A.domodal ();

if (flag = = IDOK)

{... }

====================================================================

"Working with its controls"

The most headache is how to manipulate the controls on the Sub-dialog box. Here is an example of "How to get the data typed in the child window edit box in the main program".

1, you must first associate a variable with this eidtbox.

Right-click the edit box control in dialog and add a variable, assuming g_getname.

Having done this, the variable is automatically associated with the ID of the edit box in the DoDataExchange function in A.cpp.

" However, this does not mean that the value of the input edit box is automatically assigned to the GetName variable, which means that the DoDataExchange function does not automatically execute "

2. Edit box variable value G_getname can respond to values entered by the user on the keyboard

before I tried to add a OnInitDialog virtual function and use it in a virtual function

 bool   Get_peopleid::oninitdialog () {    Cdialogex::oninitdialog ();  //  TODO: Add additional initialization  char  id_num[0   };    CString str;    str  = _t (   gfdgh   "  true  );        G_getname.getwindowtext (str);  return   

Similar to this approach, you want to be able to use the UpdateData function to update the value of the edit box variable. However, apart from being able to initialize the display GFDFH in the dialog box, none of the others will work.

Later read this article http://wenku.baidu.com/view/04a27d6a561252d380eb6e49.html. Remember, here just a corresponding message function.

// sample code, the following will also talk about void Get_peopleid::onbnclickedok () {    //  TODO:  Add control notification handler code here     CString str;    G_getname.getwindowtext (str);         << str << Std::endl;   // a    AfxMessageBox (str);                 // b     Cdialogex::onok ();}

So str is the value you enter.

3, get the value of the edit box

A, the handle says (Gets the child dialog space value, anyway I'm not workable)

Char id_num[]=""sizeof(id_num), LPARAM (Id_num));

It is said that it is possible to read the value of the H handle into id_num, so we just have to find the handle value of the edit box.

There are several ways to get a handle on the web, which is representative of:

A.1 GetDlgItem function, h= GetDlgItem (getName)->getsafehwnd ();

However, the resulting id_num is the value of the edit box in the main MFC. Yes, although I filled in the ID of the child dialog editbox, I still get the value of the main dialog box. The GetDlgItem () function is not able to get a handle to a child dialog box.

A.2 Spy + +

It is said that the "tool" has a spy in the East can see the handle of the control, and visual, point which shows where, try (self-Baidu Spy's usage), it is true to reputation. But,
hwnd H = (hwnd) 0x000003ed;//manual spy-checked editbox handle value
SendMessageA (H, Wm_gettext, sizeof (Id_num), LPARAM (Id_num));

Show the result is id_num empty, more than a few times found each time h value is different, originally each program will be assigned a new handle value! So how can I use the last value to find this handle?

A.3 FindWindowEx function

HWND hwnd =:: GetForegroundWindow ();
hwnd Handle1 =:: FindWindowEx (hwnd, 0, NULL, "getName"); handle1=00000

First find the handle value of the current window, the handle value of various windows to obtain please refer to:http://my.oschina.net/u/2314763/blog/422622?fromerr=SobXFhyw

But it turns out that it still doesn't show

A.4 Since you always get the handle value of the child dialog control in the main function, I can always set it back in the A.cpp.

// A.cpp Char* get_peopleid::get_handle () {    char id_num["";     sizeof // G_getname.m_hwnd getting control    handles directly Std::cout << id_num << std::endl;     return Id_num;}

Before calling Get_handle I have added G_getname.getwindowtext (str) in the Click OK button, and the value of the dialog box is connected to the variable. Click OK in Main.cpp to execute here, in call Get_handle, want to return id_num, no fruit. is still empty.

B, GetWindowText and CString to char* conversion

The input values have already been obtained in the sample code above and are saved in STR (a global variable that can be set to a, which is easy to get in other classes), but if you do not use the AfxMessageBox, what happens when you see an address-like output in STD? The original CString with cout output is the str address

If you simply want to display it in the console, just printf ("%s", str) is required, note S uppercase.

But if you want to use it later, you need to convert it, such as converting to char*, you can refer to the post http://blog.163.com/ming__nan/blog/static/6783288200991111281852/

This is where the most effective code is posted directly, and the other conversions on the web are mostly nonsense.

// CString str to char* c int 0, str,-10, NULL, NULL); // Cstring to char * in Unicode character set Char New Char 1  0, str,-1, C, Len, NULL, NULL);

Eject (Child) dialog box and manipulate its controls

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.