Click here to Skip to main content
15,891,473 members
Articles / Desktop Programming / MFC

Customizable Window Layouts

Rate me:
Please Sign up or sign in to vote.
5.00/5 (16 votes)
28 Jul 2008CPOL6 min read 40K   1.8K   51  
A class to facilitate user defined dialog and window control layouts.
// positionwndtestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "positionwndtest.h"
#include "positionwndtestDlg.h"
#include <direct.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPositionwndtestDlg dialog

CPositionwndtestDlg::CPositionwndtestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPositionwndtestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPositionwndtestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPositionwndtestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPositionwndtestDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPositionwndtestDlg, CDialog)
	//{{AFX_MSG_MAP(CPositionwndtestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_SAVE_LAYOUT, OnButtonSaveconfig)
	ON_BN_CLICKED(IDC_BUTTON_RESTORE_LAYOUT, OnButtonRestoreconfig)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPositionwndtestDlg message handlers

BOOL CPositionwndtestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

    // Attach the position window code to this dialog...
    m_posWnd.SubclassWindowEx(this);

    // Set the text in the read-only multiline edit control, describing 
    //  how to use the position window controls...
    char strHelp[] = 
        "Help\r\n"
        "---------------------------------------------------------------------------\r\n"
        "This application is a demonstration and test environment for\r\n"
        "the CPositionWnd class. The CPositionWnd class allows a user\r\n"
        "to customize - at runtime - a control/window's layout.\r\n"
        "\r\n"
        "Please hold-down the Ctrl button and click the right mouse button\r\n"
        "to enable a control/window for layout (size and position) editing.\r\n"
        "\r\n"
        "Once a control has been selected and put into layout edit mode,\r\n"
        "the user can reposition and resize the control.\r\n"
        "The user is also given a limited ability to specify how the\r\n"
        "control should resize and reposition with a change in its\r\n"
        "parent window's dimensions.\r\n"
        "\r\n"
        "Using this dialog as an example, Ctrl+RMB each control to toggle\r\n"
        "it into its layout edit mode. Once in the layout edit mode,\r\n"
        "the control/window should be highlighted in orange, with three\r\n"
        "orange squares in the upper-lefthand and lower-righthand corners\r\n"
        "of the control.\r\n"
        "\r\n"
        "The orange squares are control points for moving, sizing and docking\r\n"
        "the control/window. As you hover the mouse over each point, the cursor\r\n"
        "will change to reflect the action that will be taken if the user\r\n"
        "clicks the left mouse button.\r\n"
        "\r\n"
        "The upper-left corner control point allows the user to move the control.\r\n"
        "The lower-right corner control point allows the user to size the control.\r\n"
        "\r\n"
        "The control point to the right of the move control point allows the user\r\n"
        "to \"dock\" the upper-left hand corner of the control to the top of its\r\n"
        "parent window.\r\n"
        "The control point below the move control point allows the user\r\n"
        "to \"dock\" the upper-left hand corner of the control to the left side \r\n"
        "of its parent window.\r\n"
        "\r\n"
        "Please note that the \"docking\" control points are used to control\r\n"
        "how the control will be moved and resized when its parent control/window\r\n"
        "is resized.\r\n"
        "In the case of the upper-left docking points, the points allow the user\r\n"
        "to essentially anchor the upper-left hand corner of the control, so\r\n"
        "when the parent is resized, the control's upper-left hand corner does\r\n"
        "not move - essentially docking the control into the position where the\r\n"
        "user placed it.\r\n"
        "\r\n"
        "The control point to the left of the size control point allows the user\r\n"
        "to \"dock\" the lower-right hand corner of the control to the bottom of\r\n"
        "its parent window.\r\n"
        "The control point to above the size control point allows the user to\r\n"
        "\"dock\" the lower-right hand corner of the control to the right size of\r\n"
        "its parent window.\r\n"
        "\r\n"
        "Please note that the \"docking\" control points are used to control\r\n"
        "how the control will be moved and resized when its parent control/window\r\n"
        "is resized.\r\n"
        "In the case of the lower-right docking points, the points allow the user\r\n"
        "to essentially anchor the lower-right hand corner of the control, so\r\n"
        "when the parent is resized, the control's lower-right hand corner moves\r\n"
        "with the parent's bottom and right sides - allowing for resizing of the\r\n"
        "control with a resize of the parent.\r\n"
        "\r\n"
        "The layout configuration can be persisted via XML. Please try loading some\r\n"
        "test layouts, that should have been included in this distribution, by \r\n"
        "clicking the \"Restore Layout from User Specified File...\" button\r\n"
        "\r\n";
    GetDlgItem(IDC_EDIT)->SetWindowText(strHelp);

    // Fill the tree control...
    CString strLabel;
    CTreeCtrl* pTreeCtrl = (CTreeCtrl*)GetDlgItem(IDC_TREE2);
    HTREEITEM hTreeItemRoot = pTreeCtrl->InsertItem("Root");
    int i, j, k;
    for (i = 0; i < 20; i++)
    {
        strLabel.Format("Level1 %d", i);
        HTREEITEM hTreeItemI = pTreeCtrl->InsertItem(strLabel, 0, 0, hTreeItemRoot);
        for (j = 0; j < 20; j++)
        {
            strLabel.Format("Level2 %d", j);
            HTREEITEM hTreeItemJ = pTreeCtrl->InsertItem(strLabel, 0, 0, hTreeItemI);
            for (k = 0; k < 20; k++)
            {
                strLabel.Format("Level3 %d", k);
                pTreeCtrl->InsertItem(strLabel, 0, 0, hTreeItemJ);
            }
        }
    }

    // Fill the list control...
    CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1);
    for (i = 0; i < 20; i++)
    {
        strLabel.Format("ListBox Item %d", i);
        pListBox->AddString(strLabel);
    }

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPositionwndtestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPositionwndtestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPositionwndtestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPositionwndtestDlg::OnButtonSaveconfig() 
{
    // Give the user a dialog to pick the file...
    char strCWD[1024];
    _getcwd(strCWD, sizeof(strCWD));
    CString strPath;
    strPath.Format("%s\\*.xml", strCWD);
    CFileDialog	dlg(FALSE, "xml", strPath, OFN_READONLY, "XML Files (*.xml)|*.xml|All Files (*.*)|*.*||" ,this);
    if (dlg.DoModal() == IDOK)
    {
        // Save the config to the file...
        CString strLayoutConfig = m_posWnd.SaveConfigToString();
        FILE* fp = fopen(dlg.GetPathName(), "w");
        if (fp)
        {
            fprintf(fp, "%s", strLayoutConfig);
            fclose(fp);
        }
        strLayoutConfig.Replace("\n", "\r\n");
        GetDlgItem(IDC_EDIT_LAYOUT_XML)->SetWindowText(strLayoutConfig);
    }
}

void CPositionwndtestDlg::OnButtonRestoreconfig() 
{
    // Give the user a dialog to pick the file...
    char strCWD[1024];
    _getcwd(strCWD, sizeof(strCWD));
    CString strPath;
    strPath.Format("%s\\*.xml", strCWD);
    CFileDialog	dlg(TRUE, "xml", strPath, OFN_READONLY, "XML Files (*.xml)|*.xml|All Files (*.*)|*.*||",this);
    if (dlg.DoModal() == IDOK)
    {
        // Load the config from the file...
        FILE* fp = fopen(dlg.GetPathName(), "r");
        if (fp)
        {
            fseek(fp, 0, SEEK_END);
            long lSize = ftell(fp);
            fseek(fp, 0, SEEK_SET);
            char* strFileData = new char[lSize];
            fread(strFileData, lSize, 1, fp);
            m_posWnd.LoadConfigFromString(strFileData);

            CString strLayoutConfig = strFileData;
            strLayoutConfig.Replace("\n", "\r\n");
            fclose(fp);
            delete [] strFileData;
            GetDlgItem(IDC_EDIT_LAYOUT_XML)->SetWindowText(strLayoutConfig);
        }

        // Restore the dialog's store size...
        int nWidth = m_posWnd.GetLayoutWidth();
        int nHeight = m_posWnd.GetLayoutHeight();
        SetWindowPos(NULL, 0, 0, nWidth, nHeight, SWP_NOMOVE | SWP_NOZORDER);
    }
}

void CPositionwndtestDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);

    // Have the position window resize itself, moving and resizing
    //  configured child controls...
    m_posWnd.OnSize(nType, cx, cy);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions