Click here to Skip to main content
Licence 
First Posted 8 Aug 2002
Views 83,933
Downloads 914
Bookmarked 21 times

Window Styles

By Barretto VN | 17 Aug 2002
Window Styles
3 votes, 23.1%
1

2
5 votes, 38.5%
3

4
5 votes, 38.5%
5
2.45/5 - 26 votes
4 removed
μ 2.10, σa 2.78 [?]

Sample Image - WindowStyles.gif

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 can create the following type of Windows

  • Overlapped
  • Child
  • Popup
  • Standard Dialog

You have the option of creating a Menu also

    Windows can be created either

  • Minimized
  • Maxmiized
  • To Size

    Windows can have the following buttons

  • Maximize
  • Minimize
  • System Menu

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:

//
//  Depending on the Type of Window to be created
//  we enable of disble various options as below
//
//  If you select the Overlapped Window option
//  the following checkboxes are Enabled / Disabled
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);	
}

//
//  If you select the Child Window option
//  the following checkboxes are Enabled / Disabled
//

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);
		
}

//
//  If you select the Popup Window option
//  the following checkboxes are Enabled / Disabled
//

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);		
}

//
//  If you select the Standard Window option
//  the following checkboxes are Enabled / Disabled
//
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


//
//  First of all we have to register the class 
//  the name of the Class is specified as a String
//  Name of the class is passed to the my member 
//  function eg RegisterClass("MyWindowStyles")
//
//
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;
}

//
//   Since the Window created at run-time cannot have its 
//   Messages in the Message Map of the Application , we
//   have the following procedure , which takes care
//   of the messages in the newly created Window
//
//   Note : WndProc is passed as the Windows procedure
//          to the above Registered Class as 
//
//          wndclass.lpfnWndProc = WndProc;
//
//   This procedure allows us to Destroy the Created
//   Window which would otherwise not be possible

LRESULT FAR PASCAL WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	if(Message == WM_COMMAND)
	{
		DestroyWindow(hWnd);
	}

	return (DefWindowProc(hWnd, Message, wParam, lParam)); 
}
//
// If the Class was registered successfully
// we proceed with creating the Window
// With the CreateWindowEx function as below
//
//  The following variables passed to CreateWindowEx
//  contain information as selected on the Formview
//  dExtendedStyle  = Extended Window Styles
//  m_EditCaption   = Window Caption
//  dStyle          = Style of The Window
//  x               = Left Position of the Window
//  y               = Top Position of the WIndow
//  nWidth          = Width of the Window
//  nHeight         = Height of the Window
//  hMenu           = Menu (if Checked)
//                    (Menu is created in the Resources)
//

 
hWndMain = CreateWindowEx(
                dExtendedStyle,
                szClassName,           
                m_EditCaption,         
                dStyle,
                x,                     
                y,
                nWidth,                
                nHeight,                  
                this->m_hWnd,  
                hMenu,                    
                AfxGetInstanceHandle(), 
                NULL);                  

 if(!hWndMain)
 {
	AfxMessageBox("Error Creating Window");
	return;
 }

//
//
//  We create a button on the newly 
//  created Window , to enable us to close
//  the Window in case the created Window 
//  did not have the Menu option enabled or
//  Window cannot have a Menu as a Standard
//
//

HWND hwndBtn = CreateWindow(_T("BUTTON"),
                            _T("&Close"), 
                            WS_VISIBLE|
                            WS_CHILD|
                            BS_DEFPUSHBUTTON, 
                            30,20,90,30,
                            hWndMain,
                            (HMENU)ID_CLOSE_BUTTON, 
                            NULL,
                            NULL); 

//
//  Finally we Display the Window as shown below
//

 ::ShowWindow(hWndMain, SW_SHOW);

Happy Windowing !!!!!


License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Barretto VN



India India

Member
Nothing to boast about

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAttach a class to new window Pinmemberdholshou9:34 19 Mar '07  
GeneralGood Job! Pinmemberdearlmh7618:04 19 Apr '06  
Generalwindow in a dialog PinsussAnonymous6:03 25 Aug '04  
GeneralRating PinmemberAndrew Lawrence13:06 27 Feb '04  
GeneralRe: Rating PinmemberBarretto VN21:42 29 Feb '04  
GeneralZip File missing PinmemberMarcoNedwig1:16 14 Aug '02  
GeneralGood Start Pinsupporteryarp21:06 9 Aug '02  
GeneralNot a race PinmemberTodd Jeffreys14:31 9 Aug '02  
GeneralProgressing... PinmemberAndreas Saurwein7:50 9 Aug '02  
GeneralRe: Progressing... PinsussAnonymous9:03 9 Aug '02  
GeneralPoor PinsussJohn Simpsons6:15 9 Aug '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 18 Aug 2002
Article Copyright 2002 by Barretto VN
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid