VC tips summary of dialog skills

  • 2020-04-02 02:31:20
  • OfStack

This paper collects and summarizes some common techniques about dialog box in VC, for the development of VC due to certain reference value.

1. How to modify the background color of the dialog box:

Add the following statement to the OnPaint() function of the dialog box:


CRect rect;
GetClientRect(&rect);   //Calculate the size of the dialog box
dc.FillSolidRect(&rect,RGB(192,248,202));  //Draws dialog box background color

2. How to make the pop-up dialog box have a uniform background color:

Add the following statement to the InitInstance() function of the application class CxxxApp:


SetDialogBkColor( RGB(192,248,202) );

Then all user-defined pop-up dialog boxes have RGB(192,248,202) as the background color, so you don't need to set them individually.

3. How to open the file dialog box with multiple choices:

Adding the OFN_ALLOWMULTISELECT attribute when customizing the open file dialog makes it possible to open the file dialog for multiple selections.
Such as:


CFileDialog m_Dlg( TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT
    | OFN_ALLOWMULTISELECT, NULL, NULL );
 After use GetStartPosition() The start file location () function gets the selected start file location GetNextPathName() The function gets the file name at each location. 
 Such as: 
if( m_Dlg.DoModal() == IDOK )
{
   POSITION pos;
   pos = m_Dlg.GetStartPosition();
   while( pos )
   {
     m_Path = m_Dlg.GetNextPathName(pos);
      ..................... 
   }
}

4. Why the open file dialog box is used to select multiple files to a certain number of files, but the file is not open?

CFileDialog sets a buffer for the list of files. When too many files are selected, the buffer overflows and some files are not opened. You can use a custom large buffer to replace the system buffer.
Such as:


CFileDialog m_Dlg( TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT
    | OFN_ALLOWMULTISELECT, NULL, NULL );//Custom open file dialog box
char* pBuf = new char[20480];    //Request buffer
m_Dlg.m_ofn.nMaxFile = 20480;    //Replace the CFileDialog buffer with pBuf
m_Dlg.m_ofn.lpstrFile = pBuf;
m_Dlg.m_ofn.lpstrFile[0] = NULL;
 ..................... 
delete []pBuf;    //Recovery buffer

5. Prompt dialog box (MessageBox)

The MessageBox() function in the MFC function pops up the prompt dialog box in the visual and dialog classes. The function prototype is:


int MessageBox(LPCTSTR lpszText,LPCTSTR lpsCaption=NULL,UINT nType=MB_OK);

Parameters:
The string displayed by lpszText
Title of the lpsCaption dialog box
NType style, can be a combination of the following values:
Specify one of the following flags to display the button in the message box, which means the following.
MB_ABORTRETRYIGNORE: the message box contains three buttons: Abort, Retry, and Ignore.
MB_OK: the message box contains a button: OK. This is the default value.
MB_OKCANCEL: the message box contains two buttons: OK and Cancel.
MB_RETRYCANCEL: the message box contains two buttons: Retry and Cancel.
MB_YESNO: the message box contains two buttons: Yes and No.
MB_YESNOCANCEL: the message box contains three buttons: Yes, No, and Cancel.
Specify one of the following flags to display the icon in the message box: the flag has the following meaning.
MB_ICONEXCLAMATION:
MB_ICONWARNING: an exclamation point appears in the message box.
MB_ICONINFORMATION:
MB_ICONASTERISK: an icon with the letter I written in a circle appears in the message box.
MB_ICONOUESTION: a problem flag icon appears in the message box.
MB_ICONSTOP:
MB_ICONERROR:
MB_ICONHAND: a stop message icon appears in the message box.

Specify one of the following flags to specify the default button: flags have the following meaning.

MB_DEFBUTTON1: the first button is the default button. If MB_DEFBUTTON2, MB_DEFBUTTON3, and MB_DEFBUTTON4 are not specified, then MB_DEFBUTTON1 is the default value.
MB_DEFBUTTON2; The second button is the default button.
MB_DEFBUTTON3: the third button is the default button.
MB_DEFBUTTON4: the fourth button is the default button.

Example: prompt whether the file is saved:


int t;
t=MessageBox(m_PathName+" The text has changed. Do you want to save it? "," warning ",MB_YESNOCANCEL | MB_ICONWARNING);
if(t==0 || t==IDCANCEL)
  return;
if(t==IDYES)
  OnFileSave();

In other classes such as document class, the function MessageBox() in MFC cannot be used, and the function MessageBox() in API function can only be used:


int MessageBox(HWND hWnd,LPCTSTR lpszText,LPCTSTR lpCaption,UINT UType);

HWnd: identifies the owning window of the message box to be created. If this parameter is NULL, the message box does not have a window.
The last three parameters are the same as MessageBox for the visual class, but have no default values and must be set.
Ex. :


::MessageBox(NULL,m_PathName+" The text has changed. Do you want to save it? "," warning ",MB_YESNOCANCEL | MB_ICONWARNING);

Related articles: