Preparectrl (int nidc); // return the handle of the control window with the specified ID

Source: Internet
Author: User

Dodataexchange (cdataexchange * PDX) favorites
 

There is such a function in the dialog box.

It is used to exchange data with the corresponding variable dialog box.

 

Msdn explains:

 

Called by the Framework to exchange and validate dialog data.


Virtual void dodataexchange (
Cdataexchange * PDX
);

PDX
A pointer to a cdataexchange object.

 

 

 

The data exchange in the dialog box refers to the following two operations:

 

First, write the memory data to the corresponding control window

First, read the corresponding data from the control window and store it in memory variables.

To simplify these operations, MFC provides a data exchange and validation mechanism based on the cdataexchange class and some data functions.

 

Data exchange method:

 

First, define the memory variable for storing data-that is, add a member variable to the dialog box. Each control window can correspond to a member variable.

For example, for an edit control window in the dialog box, you can define a member variable of the cedit type or a member variable of the cstring type.

 

Second, reload the dialog box virtual function dodataexchange for data exchange and verification

In vc6.0, you can use classwizard to automatically add member variables and modify dodataexchange.

 

For example, a dialog box has two windows. One is an editing box that represents the name, and the other is an editing box that represents the age.

 

Use classwizard to add member variables. One is defined as cedit, and the other is defined as Int. These definitions are referenced by // {afx_data and //} afx_data, indicating that they are added by classwizard, programmers do not have to modify them

 

The corresponding dodataexchange implementation is as follows:

 

Void cexdialog: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );

// {Afx_data_map (cftpdialog)
Ddx_control (PDX, idc_name, m_name );
Ddx_text (PDX, idc_age, m_nage );
Ddv_minmaxint (PDX, m_nage, 1,100 );
//} Afx_data_map
}

 

The preceding function indicates the meaning of DDX:

 

Ddx_control: transfers the content of the idc_name subwindow to m_name, or transfers the value of m_name to the idc_name subwindow.

 

Ddx_text indicates that the content of the idc_age subwindow is saved to m_nage by integer type, or the value of m_nage is transmitted to the update display in the idc_age window.

 

Ddv_minmaxint indicates that the value of m_nage should be between 1 and 100.

 

The preceding ddx_xxx data exchange function can exchange data in two directions. How do they know the data transmission direction?

This is determined by the cdataexchange object referred to by the first PDX parameter of the ddx_xxxx function (that is, the dodataexchange parameter PDX,

Cdataexchange is defined as follows:

Class cdataexchange
{
// Attributes
Public:
Bool m_bsaveandvalidate; // true stores and verifies the data.
Cwnd * m_pdlgwnd; // point to a dialog box
// Operations (for implementors of DDX and DDV procs)
Hwnd preparectrl (int nidc); // return the handle of the control window with the specified ID
Hwnd prepareeditctrl (int nidc); // return the editing control window handle of the specified ID
Void fail (); // used to throw exceptions
# Ifndef _ afx_no_occ_support // OLE Control
Cwnd * prepareolectrl (int nidc); // used in the ole control window in the dialog box
# Endif
// Implementation
Cdataexchange (cwnd * pdlgwnd, bool bsaveandvalidate );
Hwnd m_hwndlastcontrol; // last control used (for validation)
Bool m_beditlastcontrol; // last control was an edit item
};

 

Dodataexchange is similar to the serialize function, and cdataexchange is similar to carchive. Cdataexchange uses the member variable m_pdlgwnd to save the dialog box for data exchange, and uses the member variable m_bsaveandvalidate to indicate the direction of data transmission. If the variable is true, the control window data is written to the member variable, if false, the data from the member variable is read to the control window.

 

 

When constructing a cdataexchange object, information is saved in the member variable of the object. The constructor is as follows:

Cdataexchange: cdataexchange (cwnd * pdlgwnd, bool bsaveandvalidate)
{
Assert_valid (pdlgwnd );
M_bsaveandvalidate = bsaveandvalidate;
M_pdlgwnd = pdlgwnd;
M_hwndlastcontrol = NULL;
}

 

The constructor parameter specifies the pdlgwnd dialog box for data exchange and the bsaveandvalidate data transmission direction.

 

When performing data exchange or verification, the data exchange and verification functions first use preparectrl or prepareeditctrl to obtain the control window handle, and then use: getwindowstext to read data from the control window, or use :: setwindowstext writes data to the control window. Below are several examples

 

 

Static void afx_cdecl ddx_textwithformat (cdataexchange * PDX,
Int NIDC, lpctstr lpszformat, uint nidprompt ,...)
{
Va_list pdata; // this parameter is used to process variable numbers.
Va_start (pdata, nidprompt); // obtain the Parameter
// Get the handle of the edit box
Hwnd hwndctrl = PDX-> prepareeditctrl (NIDC );
Tchar Szt [32];
If (PDX-> m_bsaveandvalidate) // true, read data from the edit box
{
// The following works for % d, % u, % lD, % lu
// Obtain the content from the edit box.
: Getwindowtext (hwndctrl, Szt, _ countof (Szt ));
// The content of the conversion edit box is in the specified format. The options include "% d, % u, % lD, % lu"
If (! Afxsimplescanf (Szt, lpszformat, pdata ))
{
Afxmessagebox (nidprompt );
PDX-> fail (); // data exchange failed
}
}
Else // false, write data to the edit box
{
// Convert the content to be written into the specified format
Wvsprintf (Szt, lpszformat, pdata); // floating point operations are not supported
// Set the content in the edit box
Afxsetwindowtext (hwndctrl, Szt );
}
Va_end (pdata); // end Parameter Analysis
}

 

 

Ddx_textwithformat is used to write or read data from the edit box in a certain format. First, it obtains the handle hwndctrl of the editing box, then reads the content from the editing Box Based on the Transmission Direction and converts it to the specified format (when reading the content ), or, the converted content is in the specified format and then written into the edit box (when writing ). This function can process an indefinite number of parameters, which is the basis for multiple data exchange and verification functions.

 

 

 

Void afxapi ddx_text (cdataexchange * PDX, int NIDC, long & value)
{
If (PDX-> m_bsaveandvalidate)
Ddx_textwithformat (PDX, NIDC, _ T ("% lD"), afx_idp_parse_int, & value );
Else
Ddx_textwithformat (PDX, NIDC, _ T ("% lD"), afx_idp_parse_int, value );
}

 

The preceding ddx_text is used to exchange data between the edit box and long data members. MFC provides multiple overload functions of ddx_text to process data exchange between editing boxes and different types of data members.

 

 

Void afxapi ddx_lbstring (cdataexchange * PDX, int NIDC, cstring & value)
{
// Obtain The ListBox handle.
Hwnd hwndctrl = PDX-> preparectrl (NIDC );
If (PDX-> m_bsaveandvalidate) // true, read data
{
// Determine the currently selected entries in the list box
Int nindex = (INT): sendmessage (hwndctrl, lb_getcursel, 0, 0l );
If (nindex! =-1) // an entry in the list box is selected.
{
// Obtain the length of the current entry
Int nlen = (INT): sendmessage (hwndctrl, lb_gettextlen, nindex, 0l );
// Read the content of the current entry to the value
: Sendmessage (hwndctrl, lb_gettext, nindex,
(Lparam) (lpvoid) value. getbuffersetlength (nlen ));
}
Else // no entries in the current list box are selected
{
Value. Empty ();
}
Value. releasebuffer ();
}
Else // false, write content to the list box
{
// Write the value string to the currently selected entry
If (: sendmessage (hwndctrl, lb_selectstring,
(Wparam)-1, (lparam) (lpctstr) value) = lb_err)
{
// No Selection Match
Trace0 ("Warning: No ListBox item selected.
");
}
}
}

 

Ddx_lbstring is used to exchange data between the list box and cstring type member data. First, obtain the handle of the list box. Then, call the list box operation function of Win32 to read or modify the content of the list box.

 

 

 

The following ddx_control is used to obtain a valid control type window object (MFC object ).

 

 

Void afxapi ddx_control (cdataexchange * PDX, int NIDC, cwnd & rcontrol)
{
If (rcontrol. m_hwnd = NULL) // No subclass is available
{
Assert (! PDX-> m_bsaveandvalidate );
// Obtain the control window handle
Hwnd hwndctrl = PDX-> preparectrl (NIDC );
// Bind the hwndctrl window and the rcontrol object of the MFC window.
If (! Rcontrol. subclasswindow (hwndctrl ))
{
Assert (false); // two subclasses are not allowed
Afxthrownotsupportedexception ();
}
# Ifndef _ afx_no_occ_support // ole control related operations
Else
{
// If the control has reparented itself (e.g., invisible control ),
// Make sure that the cwnd gets properly wired to its control site.
If (PDX-> m_pdlgwnd-> m_hwnd! =: Getparent (rcontrol. m_hwnd ))
Rcontrol. attachcontrolsite (PDX-> m_pdlgwnd );
}
# Endif //! _ Afx_no_occ_support
}
}

 

Ddx_control is used to bind a control window (Windows window) with a dialog box member (MFC window object). This process is completed through the subclasswindow function. In this way, programmers can use member variables to operate the control window, read, write, and modify the content of the control window.

MFC also provides many other data exchange functions ("DDX _" is the prefix) and data verification functions ("DDV _" is the prefix ). The DDV function is similar to the DDX function.

 

Programmers can create and use their own data exchange and verification functions, and manually add these functions to dodataexchange. If you want classwizard to use these functions, you can modify DDX. CLW file, add the self-created function to the DDX and DDV function entries.

 

 

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

 

Updatedata Function

With data exchange classes and data exchange functions, how can we use them? MFC designed the updatedata function to complete the above data exchange and verification processing.

 

First, updatedata creates a cdataexchange object and then calls the dodataexchange function. The implementation is as follows:

 

Bool cwnd: updatedata (bool bsaveandvalidate)
{
Assert (: iswindow (m_hwnd); // calling updatedata before domodal?
// Create a cdatachange object
Cdataexchange dx (this, bsaveandvalidate );
// Prevent sending notification messages to the window during updatedata
_ Afx_thread_state * pthreadstate = afxgetthreadstate ();
Hwnd hwndoldlockout = pthreadstate-> m_hlockoutpolicywindow;
Assert (hwndoldlockout! = M_hwnd); // must not recurse
Pthreadstate-> m_hlockoutpolicywindow = m_hwnd;
Bool Bok = false; // assume failure
Try
{
// Data exchange
Dodataexchange (& dx );
Bok = true; // it worked
}
Catch (cuserexception, e) // exception
{
// Validation failed-user already alerted, fall through
Assert (Bok = false );
// Note: delete_exception _ (e) not required
}
And_catch_all (E)
{
// Validation failed due to oom or other resource failure
E-> reporterror (mb_iconexclamation, fx_idp_internal_failure );
Assert (! Bok );
Delete_exception (E );
}
End_catch_all
// Restore the original value
Pthreadstate-> m_hlockoutpolicywindow = hwndoldlockout;
Return Bok;
}

 

 

 

Updatedata creates a cdataexchange object DX Based on the parameter. If the parameter is true, dx is used to write data, otherwise dx is used to read data, and dodataexchange is called to exchange data. During data exchange, to prevent the current window from receiving and processing command notification messages, the handle of the window is recorded in the thread status of the current thread to prevent sending notification messages to the window.

 

Writing data means writing external data into memory variables (changing memory data)

 

The so-called read data, display window read memory data (do not change the memory data)

 

Updatedata (true): Write Data, write window control variables to memory (update data)

 

Updatedata (flase): indicates read data, that is, the data read from the memory in the display window for real-time display.

 

The two are often used in the following situations:

 

When you set the window data default value or pass the window data as a control parameter to the memory, you need to write data to the memory. In this case, you should first call updatedata (true)

 

After writing, you need to observe the changes in the memory data. At this time, you should always read the memory data. At this time, you should call updatedata (false)

 

The data exchange and verification mechanism of MFC greatly simplifies the work of programmers. Generally, in oninitdialog, MFC calls updatedata (false) to send data to the control window for display. In onok, updatedata (true) is called to write data from the control window to the memory.

 

----------------------------------------------------------------------------

----------------------------------------------------------------------------

Differences between functions for closing the dialog box

 

Onok first exchanges data, obtains the data of each control sub-window in the dialog box, writes the data to the memory, and then calls the enddialog end dialog box.

 

The oncancle enddialog end dialog box is displayed.

 

Enddialog first modifies the m_nflag value to indicate the end mode loop, and then calls: enddialog to close the dialog box window.

 

 

 

 

 

 

 

 

 

Example:

Void cpenwidthsdlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );

Ddx_text (PDX, idc_thin_pen_width, m_nthinwidth );

Ddv_minmaxint (PDX, m_nthinwidth, 1, 20 );

Ddx_text (PDX, idc_thick_pen_width, m_nthickwidth );

Ddv_minmaxint (PDX, m_nthickwidth, 1, 20 );
}

Example:

 

Void cmy_uc_tooldlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );

Ddx_text (PDX, idc_edit_minx, m_min.x );
Ddx_text (PDX, idc_edit_miny, m_min.y );
Ddx_text (PDX, idc_edit_minz, m_min.z );
Ddx_text (PDX, idc_edit_maxx, m_max.x );
Ddx_text (PDX, idc_edit_maxy, m_max.y );
Ddx_text (PDX, idc_edit_maxz, m_max.z );
Ddx_text (PDX, idc_edit_oftx, m_oft.x );
Ddx_text (PDX, idc_edit_ofty, m_oft.y );
Ddx_text (PDX, idc_edit_oftz, m_oft.z );

}

 

 

 

 

References:

 

Http://tech.ddvip.com/2007-03/117415921421640.html

 

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.