Home | API | MFC | C++ | C | Previous | Next

Programming Windows API

Common Dialog Box

The Common Dialog Box Library consists of a set of functions for invoking standard dialog boxes performing common tasks, such as choosing colours, opening files and printing documents. This helps to maintain a consistent approach when designing an application's user interface. The common dialog box library files are found in the COMMDLG.H header file. To use these functions, the application must first initialise the fields of a structure and then pass a pointer to the structure to a function in the common dialog box library. When the user terminates the dialog box, control returns to the calling application with relevant information passed to the structure.

This code sections below demonstrate 3 common dialog boxes: text find and replace, the file open and save and the colour select. 

Select Colour Common Dialog Box

The Select Colour Dialog Box displays available colours for selection in addition to allowing a user to create custom colours. The syntax of the ChooseColour function is

BOOL ChooseColor(LPCHOOSECOLOR lpcc );

Where lpcc is a Pointer to the LPCHOOSECOLOR structure that contains information used to initialise the dialog box. When ChooseColor function returns, this structure also contains information about the user’s colour selection.

Display Code
Download Code


File Open and Save Common Dialog Box 

The ‘Common File-Open/Save’ dialog offers a consistent way to deal with file management operations, using the standard dialog interface that Windows users should be familiar with.

The Open dialog box lets the user specify the drive, directory, and the name of a file or set of files to open.

The Save As dialog box lets the user specify the drive, directory, and name of a file to save.

The following two functions bring up the file open and file save dialog boxes-

BOOL GetOpenFileName(LPOPENFILENAME lpofn);
BOOL GetSaveFileName(LPOPENFILENAME lpofn);

Both of these functions require a pointer to an OPENFILENAME structure. This structure controls such things as the required file extension, the starting path to look in and file access permissions. When the function returns, the structure will contain the name of the file selected.

Display Code
Download Code


Text Find and Replace Common Dialog Box

The Find and Replace Common Dialog Box displays a modeless dialog box that allows the user to specify a string to search for within a text documents, as well as additional options for text editing. The syntax of the FindText function is

HWND FindText(LPFINDREPLACE lpfr);

Where

LPFINDREPLACE lpfr is Pointer to a FINDREPLACE structure that contains information used to initialise the dialog box. If the function succeeds, the return value is the window handle to the dialog box. This handle can be used to communicate with or to close the dialog box. If the function fails, the return value is NULL.

Display Code
Download Code


Home | API | MFC | C++ | C | Previous | Next
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions