Contact VC 3: 2

zhaozj2021-02-16  54

Below, we come to a very important function on dialog box onInitDialog (), as the name refer to the initialization function of a dialog. After the dialog is created, call it before the first display.

Bool cdialogdlg :: oninitdialog ()

{

CDIALOG :: OnItDialog (); // Perform the parent class default initialization dialog operation

// IDM_Aboutbox must be within the system command range.

Assert (IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

Assert (IDM_AboutBox <0xf000);

// Add / "About ... /" menu item to the system menu.

CMenu * psysmenu = getSystemMenu (false); // gets the CMENU object pointer of this dialog system menu and assigns psysmenu;

IF (psysmenu! = null) // If it is not empty, then

{

CString straboutmenu; // Declare a string object

STRABOUTMENU.LOADSTRING (IDS_ABOUTBOX); / / A resource IDS_ABOUTBOX predefined string,

/ / String Table in the resource view check and set this predefined string

IF (! straboutmenu.isempty ()) // If not empty, then

{

psysmenu-> appendmenu (mf_separator); // Add a separator to the menu

Psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu);

/ / Add this string to the menu and set the message ID to IDM_Aboutbox

}

}

/ / Set the icon of this dialog. When the application main window is not a dialog, the framework will automatically

/ / Do this

Seticon (M_HICON, TRUE); // Set large icon

Seticon (M_HICON, FALSE); // Set small icon

// Todo: Add additional initialization code here

Return True; // should return true unless the control is set, you should return TRUE

}

The above is the basic code of this dialog-based MFC application. Now you can compile running directly to view the effect.

Below, I will add functions on this code to implement a copy of the file.

First, I want to modify the IDD_DIALOG_DIALOG template in the Dialog of the resource view:

I first deleted all buttons and static text on the dialog template, add two text boxes and four buttons. If you want to modify the ID value of the control, right-click on the control, click on the properties, enter any ID string in the ID box. The Basic Department is as follows:

Note: Bold represents the ID value of the control.

If you want to add a button event to the CDIALOGDLG class, there are two simple ways. The first kind is in the template design, double-click the button, and then add this button to the event. Another way is to use the wizard (add) in the incident bar in the CDIALOG class in .NET), first select the class wizard, pop-up the class wizard dialog in the View (View) menu, Classname drop-down box Select our class CDialogDLG to add events. The ID of the control is selected in the Object ID list box. In the Messages list box, select the event you want to add, press the Add Function.

Add four buttons to the event, and the system will automatically name the member function for us. If there is no error, it is OnBnclickedCance (); OnbnclicKedCopy (); OnbnclicKedsRbrowse (); OnBnclicKedtrBrowse (); because I use .NET, may be slightly different from the 5.0 generated function name. After adding the event, you'd better go see what the message map macro mentioned above is, whether you can read them. First add such a row of statements in the onbnclickedcancel () function:

This-> enddialog (idcancel);

The role of this row state is to close the current dialog and return in IDCANCEL, indicating that the user is using the cancellation to close the dialog. This is a method of CDIALOG class. We expect to turn off the current dialog if you click the Cancel button.

Let's handle the features of the browsing button. I expect a dialog box that selects a file to select the source file and destination file, and display the file name in the text box. This file dialog is just defined in the MFC class library, we can use it directly. First, we must add a plural header file to the header of the CDIALOGDLG.cpp.

#include

Then, add the following statement to the Source File Browse button (id_srbrowse).

CFiledialog Open (True / * If the true dialog is open to open the dialog, to save dialog box * /,

"" / * Default suffix name * /,

"" / * Default file name * /,

0 / * dialog box style * /,

"All file | *. * |",

THIS / * Parent window pointer * /);

CString strfilepath;

IF (Open.Domodal () == iDok) // There is a mode display dialog box, if the returns are returned, the representative has file selection, then

{

Strfilepath = Open.getPathName (); // Get file path string

SetdlgiteMtext (IDC_Source, StrfilePath); // Set the text of the ID of IDC_Source to the string

}

To explain, cstring is a string class of the MFC, which can be used as a character array. And it can also be used as the string of VB, and the string assignment is performed directly.

There is also setdlgitemtext, which is a method of the CWnd class, which will change the text of a control of the current window. This control can be button, text box, static text, drop-down list box, etc. The first parameter is the ID of the control, and the second parameter is a string ending with 0.

Pushing with this function, you can write the function code of the target browsing button as follows:

Cfiledialog save (false / ** /,

"" / * Default suffix name * /,

"" / * Default file name * /,

0 / * dialog box style * /,

"All file | *. * |",

THIS / * Parent window pointer * /);

CString strfilepath;

if (Save.Domodal () == iDOK)

{

Strfilepath = save.getpathname ();

Setdlgitemtext (IDC_Target, StrfilePath);

}

Finally, let's complete the function of the copy button. Add the following code in the click event response function onbnclickedtrbrowse:

CString strsource, startaget;

GetdlgiteMtext; // Get the text getDlgitemtext (IDC_TARGET, STRTARGET) that is named IDC_Source control; // Get the ID called IDC_TARGET control

IF (CopyFile (strsource, strtarget, false) // copy file, if returned to true,

{

MessageBox ("Replication Success!", "Report", MB_OK; // Pop up a certainty box

}

Here, getDLGItemText, which is also a method of CWnd, is the reverse process of SetDLGItemText, which is used to obtain the text of a control on the window. CopyFile is WinAPI, which is used to make file replication, the first parameter is a string representing the source file name, and the second parameter is a string representing the target file name. If successful, return true. The CWnd :: Messagebox function is used to display a message box. The first parameter is the message text. The second parameter is the title text. The third parameter is the type of message box. Here is the MB_OK determination box, but also MB_YESNO is, etc. These can be checked in MSDN.

This way, a simple dialog-based MFC applet is done. Is it not difficult? Also believe in the official, have some understanding of the MFC programming method.

If you want MFC programming, you are very very recommended that you often check the MSDN included with Visual Studio, and can master the skills of finding MSDN, which will make your work half a time.

Next part, I want to talk about the writing of dynamic link libraries, I hope to like it.

So I wish you all a happy.

Blue_atlantis400@hotmail.com

Release snow

2002.12.07

转载请注明原文地址:https://www.9cbs.com/read-24710.html

New Post(0)