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

Form Designer

26 Jul 2021CPOL24 min read 349.8K   82.5K   230  
Component for adding scriptable forms capabilities to an application.
// EditableForm.cpp: implementation of the CEditableForm class.
//
// Author : David Shepherd
//			Copyright (c) 2002, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EditableForm.h"

// default form width and height
#define DEFAULT_WIDTH			250
#define DEFAULT_HEIGHT			200
// default grid width and height
#define DEFAULT_GRID_WIDTH		8
#define DEFAULT_GRID_HEIGHT		8

/////////////////////////////////////////////////////////////////////////////
// CEditableForm

CEditableForm::CEditableForm()
{
	// initialise everything
	m_Width=DEFAULT_WIDTH;
	m_Height=DEFAULT_HEIGHT;
	m_BackColor=MAKE_OLE_COLOR(COLOR_BTNFACE);
	m_ForeColor=MAKE_OLE_COLOR(COLOR_BTNTEXT);
	m_GridWidth=DEFAULT_GRID_WIDTH;
	m_GridHeight=DEFAULT_GRID_HEIGHT;
	m_ShowGrid=TRUE;
}

CEditableForm::~CEditableForm()
{
	// clean up
}

void CEditableForm::SetDimensions(long Width,long Height)
{
	// if the dimensions are changing a redraw will be required
	if(m_Width!=Width or m_Height!=Height)
	{
		if(IsWindow())
		{
			(void)SetWindowPos(
				NULL,0,0,Width,Height,SWP_NOZORDER|SWP_NOMOVE);
		}
	}
	// set the dimensions
	m_Width=Width;
	m_Height=Height;
}

void CEditableForm::GetDimensions(long &Width,long &Height) const
{
	// return the dimensions
	Width=m_Width;
	Height=m_Height;
}

void CEditableForm::SetColors(OLE_COLOR BackColor,OLE_COLOR ForeColor)
{
	// if the colors are changing a redraw will be required
	if(m_BackColor!=BackColor or m_ForeColor!=ForeColor)
	{
		if(IsWindow())
		{
			(void)Invalidate(FALSE);
		}
	}
	// set the colors
	m_BackColor=BackColor;
	m_ForeColor=ForeColor;
}

void CEditableForm::GetColors(OLE_COLOR &BackColor,OLE_COLOR &ForeColor) const
{
	// return the colors
	BackColor=m_BackColor;
	ForeColor=m_ForeColor;
}

void CEditableForm::SetGrid(long Width,long Height)
{
	// check parameters
	ATLASSERT(Width > 0);
	ATLASSERT(Height > 0);
	// if either the width or height are changing a redraw will be required
	if(m_GridWidth!=Width or m_GridHeight!=Height)
	{
		if(IsWindow())
		{
			(void)Invalidate(FALSE);
		}
	}
	// set the grid width and height
	m_GridWidth=Width;
	m_GridHeight=Height;
}

void CEditableForm::GetGrid(long &Width,long &Height) const
{
	// return the grid width and height
	Width=m_GridWidth;
	Height=m_GridHeight;
}

void CEditableForm::ShowGrid(BOOL Show/*=TRUE*/)
{
	// if the visibility is changing a redraw will be required
	if(m_ShowGrid!=Show)
	{
		if(IsWindow())
		{
			(void)Invalidate(FALSE);
		}
	}
	// set the grid visibility
	m_ShowGrid=Show;
}

BOOL CEditableForm::IsGridVisible() const
{
	// return the grid visibility
	return m_ShowGrid;
}

LRESULT CEditableForm::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// get the paint device context
	CPaintDC dc(*this);
	if(dc==NULL)
	{
		return 0;
	}
	// get the clip box
	CRect ClipBox(0,0,0,0);
	dc.GetClipBox(ClipBox);
	// background color
	COLORREF RGBBackColor=RGB(0,0,0);
	if(!SUCCEEDED(OleTranslateColor(m_BackColor,NULL,&RGBBackColor)))
	{
		return 0;
	}
	// foreground color
	COLORREF RGBForeColor=RGB(0,0,0);
	if(!SUCCEEDED(OleTranslateColor(m_ForeColor,NULL,&RGBForeColor)))
	{
		return 0;
	}
	// fill in the background
	dc.FillSolidRect(ClipBox,RGBBackColor);
	// draw the grid
	if(m_ShowGrid)
	{
		// start x pixel
		long xStart=m_GridWidth*(ClipBox.left/m_GridWidth);
		// start y pixel
		long yStart=m_GridHeight*(ClipBox.top/m_GridHeight);
		// draw left->right and top->bottom for a more natural update
		for(long y=yStart; y<ClipBox.bottom; y+=m_GridHeight)
		{
			for(long x=xStart; x<ClipBox.right; x+=m_GridWidth)
			{
				(void)dc.SetPixel(x,y,RGBForeColor);
			}
		}
	}
	return 0;
}

LRESULT CEditableForm::OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// to prevent flicker do not erase the background
	return TRUE;
}

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