
What's New:
Introduction:
This Article demonstrates the technique of creating different style Windows at run-time, by selecting various options on the displayed form view
Features:
You have the option of creating a Menu also
You can create the Window with default sizes and at the default position or specify the Left, Top Values and also the specify Width and Height
Note:
If you continue clicking on the "Create Window" button , Windows will be created one over the other as many times as you go on clicking , and will have to close each Window by clicking on the Close button as many times
About The Code:
void CMainForm::SetOverlappedProperties()
{
GetDlgItem(IDC_DIALOGFRAME)->EnableWindow(FALSE);
GetDlgItem(IDC_MINIMIZE_BOX)->EnableWindow(TRUE);
GetDlgItem(IDC_MAXIMIZE_BOX)->EnableWindow(TRUE);
GetDlgItem(IDC_SYSTEM_MENU)->EnableWindow(TRUE);
GetDlgItem(IDC_DEFAULT_TOPLEFT)->EnableWindow(TRUE);
GetDlgItem(IDC_DEFAULT_WIDTHHEIGHT)->EnableWindow(TRUE);
GetDlgItem(IDC_TOP)->EnableWindow(FALSE);
GetDlgItem(IDC_LEFT)->EnableWindow(FALSE);
GetDlgItem(IDC_WIDTH)->EnableWindow(FALSE);
GetDlgItem(IDC_HEIGHT)->EnableWindow(FALSE);
m_DefaultTopLeft = m_DefaultWidthHeight = TRUE;
m_MinimizeBox = m_MaximizeBox = m_SystemMenu =
m_Overlapped = TRUE;
m_ToSize = TRUE;
m_Popup = m_Child = FALSE;
UpdateData(FALSE);
}
void CMainForm::SetChildProperties()
{
m_MinimizeBox = m_MaximizeBox = m_SystemMenu = m_Overlapped =
m_Popup = FALSE;
m_ToSize = TRUE;
m_Child = TRUE;
GetDlgItem(IDC_DIALOGFRAME)->EnableWindow(TRUE);
GetDlgItem(IDC_MINIMIZE_BOX)->EnableWindow(FALSE);
GetDlgItem(IDC_MAXIMIZE_BOX)->EnableWindow(FALSE);
GetDlgItem(IDC_SYSTEM_MENU)->EnableWindow(FALSE);
GetDlgItem(IDC_CAPTION)->EnableWindow(TRUE);
m_Caption = FALSE;
UpdateData(FALSE);
}
void CMainForm::SetPopupProperties()
{
m_MinimizeBox = m_MaximizeBox = m_SystemMenu =
m_Overlapped = m_Child = FALSE;
m_Popup = TRUE;
m_ToSize = TRUE;
m_SystemMenu = TRUE;
GetDlgItem(IDC_MINIMIZE_BOX)->EnableWindow(TRUE);
GetDlgItem(IDC_MAXIMIZE_BOX)->EnableWindow(TRUE);
GetDlgItem(IDC_SYSTEM_MENU)->EnableWindow(TRUE);
GetDlgItem(IDC_CAPTION)->EnableWindow(TRUE);
m_Caption = FALSE;
UpdateData(FALSE);
}
void CMainForm::SetStandardDialogProperties()
{
CheckDlgButton(IDC_CAPTION, 1);
GetDlgItem(IDC_CAPTION)->EnableWindow(TRUE);
CheckDlgButton(IDC_SYSTEM_MENU,1);
GetDlgItem(IDC_SYSTEM_MENU)->EnableWindow(TRUE);
GetDlgItem(IDC_MINIMIZE_BOX)->EnableWindow(TRUE);
GetDlgItem(IDC_MAXIMIZE_BOX)->EnableWindow(TRUE);
GetDlgItem(IDC_SYSTEM_MENU)->EnableWindow(TRUE);
CheckDlgButton(IDC_MAXIMIZE_BOX, 0);
CheckDlgButton(IDC_MINIMIZE_BOX, 0);
CheckDlgButton(IDC_MODAL_DIALOG_FRAME, 1);
GetDlgItem(IDC_DEFAULT_WIDTHHEIGHT)->EnableWindow(FALSE);
GetDlgItem(IDC_DEFAULT_TOPLEFT)->EnableWindow(FALSE);
GetDlgItem(IDC_WIDTH)->EnableWindow(TRUE);
GetDlgItem(IDC_HEIGHT)->EnableWindow(TRUE);
GetDlgItem(IDC_TOP)->EnableWindow(TRUE);
GetDlgItem(IDC_LEFT)->EnableWindow(TRUE);
UpdateData(TRUE);
}
Lets take a look at the coding that goes into creating the Window at run-time after you have selected the necessary options and clicked on the "Create Window" button
BOOL CMainForm::RegisterClass(CString stringClass)
{
WNDCLASS wndclass;
wndclass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = AfxGetInstanceHandle();
wndclass.hIcon = LoadIcon(NULL, _T(""));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW );
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = stringClass;
if( !AfxRegisterClass( &wndclass ))
{
AfxMessageBox("Error Registering Class\n");
return FALSE;
}
return TRUE;
}
LRESULT FAR PASCAL WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
if(Message == WM_COMMAND)
{
DestroyWindow(hWnd);
}
return (DefWindowProc(hWnd, Message, wParam, lParam));
}
hWndMain = CreateWindowEx(
dExtendedStyle,
szClassName,
m_EditCaption,
dStyle,
x,
y,
nWidth,
nHeight,
this->m_hWnd,
hMenu,
AfxGetInstanceHandle(),
NULL);
if(!hWndMain)
{
AfxMessageBox("Error Creating Window");
return;
}
HWND hwndBtn = CreateWindow(_T("BUTTON"),
_T("&Close"),
WS_VISIBLE|
WS_CHILD|
BS_DEFPUSHBUTTON,
30,20,90,30,
hWndMain,
(HMENU)ID_CLOSE_BUTTON,
NULL,
NULL);
::ShowWindow(hWndMain, SW_SHOW);
Happy Windowing !!!!!