Click here to Skip to main content
15,860,943 members
Articles / Desktop Programming / WTL

Form Designer

26 Jul 2021CPOL24 min read 349.7K   82.5K   230  
Component for adding scriptable forms capabilities to an application.
// PreviewDlg.cpp : implementation file
//
// Author : David Shepherd
//			Copyright (c) 2002, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MFCDemo.h"
#include "PreviewDlg.h"

#include "ViewableForm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPreviewDlg dialog

CPreviewDlg::CPreviewDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPreviewDlg::IDD, pParent),
	m_AutoSizing(FALSE)
{
	//{{AFX_DATA_INIT(CPreviewDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

void CPreviewDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPreviewDlg)
	DDX_Control(pDX, IDC_FORMVIEWER, m_FormViewer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPreviewDlg, CDialog)
	//{{AFX_MSG_MAP(CPreviewDlg)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPreviewDlg message handlers

BOOL CPreviewDlg::OnInitDialog() 
{
BLOCK_BEGIN
	CDialog::OnInitDialog();

	// set the caption
	CString Caption;
	GetWindowText(Caption);
	Caption+=m_AutoSizing ? _T(" (Auto Sizing)") : _T(" (None Auto Sizing)");
	SetWindowText(Caption);

	// expose objects
	for(long l=0; l<m_ExposedObjectInfoArray.GetSize(); l++)
	{
		m_FormViewer.Expose(
			m_ExposedObjectInfoArray.GetAt(l).m_Name,
			m_ExposedObjectInfoArray.GetAt(l).m_spDispatch);
	}

	// load the form
	m_FormViewer.LoadFromFile(m_FileName);

	// set the form viewer background color (optional but looks better)
	m_FormViewer.SetBackColor(m_FormViewer.GetForm().GetBackColor());

	// size the dialog
	if(m_AutoSizing)
	{
		// calculate the none client area
		CRect WindowRect;
		GetWindowRect(WindowRect);
		CRect ClientRect;
		GetClientRect(ClientRect);
		long NCWidth=WindowRect.Width()-ClientRect.Width();
		long NCHeight=WindowRect.Height()-ClientRect.Height();

		// calculate the required dialog dimensions
		long Width=NCWidth+m_FormViewer.GetForm().GetWidth();
		long Height=NCHeight+m_FormViewer.GetForm().GetHeight();

		// prevent screen overrun
		Width=min(Width,GetSystemMetrics(SM_CXSCREEN));
		Height=min(Height,GetSystemMetrics(SM_CYSCREEN));

		// size and position the dialog
		(void)SetWindowPos(NULL,0,0,Width,Height,SWP_NOZORDER);
		CenterWindow();
	}

	// size the form viewer
	CRect ClientRect(0,0,0,0);
	GetClientRect(ClientRect);
	ASSERT(IsWindow(m_FormViewer));
	(void)m_FormViewer.SetWindowPos(NULL,0,0,
		ClientRect.Width(),ClientRect.Height(),SWP_NOZORDER);

	// give focus to the form viewer
	(void)m_FormViewer.SetFocus();
BLOCK_END
	return FALSE; // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPreviewDlg::OnSize(UINT nType, int cx, int cy) 
{
BLOCK_BEGIN
	CDialog::OnSize(nType, cx, cy);
	
	// size the form viewer
	if(IsWindow(m_FormViewer))
	{
		(void)m_FormViewer.SetWindowPos(NULL,0,0,cx,cy,SWP_NOZORDER);
	}
BLOCK_END
}

BOOL CPreviewDlg::PreTranslateMessage(MSG* pMsg) 
{
BLOCK_BEGIN
	// this section of code is required to work around MFC problems
	// when hosting DaeDoe Forms

	// ignore none keyboard messages
	if(pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST)
	{
		return CDialog::PreTranslateMessage(pMsg);
	}
	// obtain a pointer to the message target window
	CWnd *pWndMsg=FromHandle(pMsg->hwnd);
	if(pWndMsg==NULL)
	{
		return CDialog::PreTranslateMessage(pMsg);
	}
	// determine which component the target window belongs to
	CComPtr<IUnknown> spUnknown;
	if(IsWindow(m_FormViewer))			// form viewer
	{
		if(*pWndMsg==m_FormViewer || 
			m_FormViewer.IsChild(pWndMsg))
		{
			spUnknown=m_FormViewer.GetControlUnknown();
		}
	}
	// if we need to handle the message
	if(spUnknown!=NULL)
	{
		// allow tooltip messages to be filtered
		if(CWnd::PreTranslateMessage(pMsg))
		{
			return TRUE;	// message handled
		}
		// give the component chance to translate accelerators
		CComQIPtr<IOleInPlaceActiveObject> spOleInPlaceActiveObject(spUnknown);
		if(spOleInPlaceActiveObject!=NULL &&
			spOleInPlaceActiveObject->TranslateAccelerator(pMsg)!=S_FALSE)
		{
			return TRUE;	// message handled
		}
		// let the component consume all input
		(void)TranslateMessage(pMsg);
		(void)pWndMsg->SendMessage(pMsg->message,pMsg->wParam,pMsg->lParam);
		return TRUE;		// message handled
	}
	return CDialog::PreTranslateMessage(pMsg);
BLOCK_END
	return FALSE;	// message not handled
}

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
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions