![]() |
Calling a Dialog Box From an SDI |
A Single Document Interface (SDI) is the type of application that presents a frame-based window equipped with a title bar, a menu, and thick borders. In most cases the one-frame window is enough to support the application. To provide support and various options to the frame of an SDI, you can add other dependent windows such as dialog boxes. When such a dialog box is needed, you can provide a menu item that the user would use to display the dialog box. Just as you can add one dialog box to an application, in the same way, you can add as many dialog boxes as you judge necessary to your SDI application and provide a way for the user to display them. To display a dialog from an SDI, the easiest and most usual way consists of creating a menu item on the main menu. When implementing the event of the menu item, as normally done in C++, in the source file of the class that would call it, include the header file of the dialog box. In the event that calls the dialog box, first declare a variable based on the name of the class of the dialog box. |
|
|



#define IDR_MAIN_MENU 1001 #define IDM_FILE_EXIT 1002 #define IDM_HELP_ABOUT 1003 #define IDD_ABOUTDLGBOX 1010 |

#include "Resource.h"
// Main Menu
IDR_MAIN_MENU MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", IDM_FILE_EXIT
END
POPUP "&Help"
BEGIN
MENUITEM "&About...", IDM_HELP_ABOUT
END
END
// Dialog Box: About
IDD_ABOUTDLGBOX DIALOGEX 0, 0, 184, 81
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION |
WS_SYSMENU
CAPTION "About SDI1"
FONT 8, "MS Shell Dlg"
BEGIN
DEFPUSHBUTTON "OK",IDOK,127,7,50,16,WS_GROUP
END
|
#include <windows.h>
#include "Resource.h"
#ifdef __BORLANDC__
#pragma argsused
#endif
//---------------------------------------------------------------------------
HINSTANCE hInst;
const char *ClsName = "CallingDlg";
const char *WndName = "Calling a Dialog Box From an SDI";
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
MSG Msg;
HWND hWnd;
WNDCLASSEX WndClsEx;
// Create the application window
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProcedure;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = MAKEINTRESOURCE(IDR_MAIN_MENU);
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// Register the application
RegisterClassEx(&WndClsEx);
hInst = hInstance;
// Create the window object
hWnd = CreateWindow(ClsName,
WndName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
// Find out if the window was created
if( !hWnd ) // If the window was not created,
return 0; // stop the application
// Display the window to the user
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
// Decode and treat the messages
// as long as the application is running
while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_FILE_EXIT:
PostQuitMessage(WM_QUIT);
break;
case IDM_HELP_ABOUT:
DialogBox(hInst,
MAKEINTRESOURCE(IDD_ABOUTDLGBOX),
hWnd,
reinterpret_cast<DLGPROC>(DlgProc));
break;
}
return 0;
// If the user wants to close the application
case WM_DESTROY:
// then close it
PostQuitMessage(WM_QUIT);
break;
default:
// Process the left-over messages
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
// If something was not done, let it go
return 0;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
|
|
|
||
| Home | Copyright © 2002-2005 FunctionX, Inc. | |
|
|
||