Click here to Skip to main content
15,895,557 members
Articles / Desktop Programming / WTL

Form Designer

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

#include "stdafx.h"
#include "TabNumber.h"

// border width
#define BORDER_WIDTH	4
// border height
#define BORDER_HEIGHT	0

/////////////////////////////////////////////////////////////////////////////
// CTabNumber

CTabNumber::CTabNumber()
{
	// initialise everything
	m_BackColor=MAKE_OLE_COLOR(COLOR_HIGHLIGHT);
	m_ForeColor=MAKE_OLE_COLOR(COLOR_HIGHLIGHTTEXT);
	m_TabNumber=0;
}

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

CSize CTabNumber::CalcRequiredWindowSize(DWORD MaxTabNumber)
{
	// get the number of digits in the maximum tab number
	TCHAR Buf[16]=_T("");
	(void)_itot(MaxTabNumber,Buf,10);
	long MaxDigits=_tcslen(Buf);

	// create the widest possible string
	FillMemory(Buf,MaxDigits,_T('8'));

	// calculate the required window size
	CClientDC dc(*this);
	CSize Size(0,0);
	(void)dc.GetTextExtent(Buf,MaxDigits,&Size);

	// add the border width and height
	Size.cx+=2*BORDER_WIDTH;
	Size.cy+=2*BORDER_HEIGHT;
	return Size;
}

void CTabNumber::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 CTabNumber::GetColors(OLE_COLOR &BackColor,OLE_COLOR &ForeColor) const
{
	// return the colors
	BackColor=m_BackColor;
	ForeColor=m_ForeColor;
}

BOOL CTabNumber::Create(const CWindow &Parent,
	const CWindow &AssociatedWindow,DWORD MaxTabNumber)
{
	// check parameters
	ATLASSERT(::IsWindow(Parent));
	ATLASSERT(::IsWindow(AssociatedWindow));

	// save settings
	m_AssociatedWindow=AssociatedWindow;

	// create the tab number
	ATLASSERT(IsWindow()==FALSE);
	if(CWindowImpl<CTabNumber>::Create(Parent,
		CRect(CPoint(0,0),CalcRequiredWindowSize(MaxTabNumber)),
			_T("TabNumber"),WS_CHILD|WS_VISIBLE|WS_DISABLED)==NULL)
	{
		return FALSE;
	}
	AutoPosition();	// auto position
	return TRUE;
}

void CTabNumber::AutoPosition()
{
	// get the associated window rectangle
	CRect Rect(0,0,0,0);
	(void)m_AssociatedWindow.GetWindowRect(Rect);

	// convert to our parents coordinates
	CWindow Parent(GetParent());
	(void)Parent.ScreenToClient(Rect);

	// auto position the tab number
	(void)SetWindowPos(NULL,Rect,SWP_NOZORDER|SWP_NOSIZE);
}

void CTabNumber::SetTabNumber(DWORD TabNumber)
{
	// set the tab number
	if(m_TabNumber!=TabNumber)
	{
		m_TabNumber=TabNumber;
		(void)Invalidate(FALSE);
	}
}

DWORD CTabNumber::GetTabNumber() const
{
	// get the tab number
	return m_TabNumber;
}

LRESULT CTabNumber::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// get the paint device context
	CPaintDC dc(*this);
	if(dc==NULL)
	{
		return 0;
	}
	// get the client rect
	CRect ClientRect(0,0,0,0);
	if(GetClientRect(ClientRect)==FALSE)
	{
		return 0;
	}
	// set background color
	COLORREF RGBBackColor=RGB(0,0,0);
	if(!SUCCEEDED(OleTranslateColor(m_BackColor,NULL,&RGBBackColor)))
	{
		return 0;
	}
	if(dc.SetBkColor(RGBBackColor)==CLR_INVALID)
	{
		return 0;
	}
	// set foreground color
	COLORREF RGBForeColor=RGB(0,0,0);
	if(!SUCCEEDED(OleTranslateColor(m_ForeColor,NULL,&RGBForeColor)))
	{
		return 0;
	}
	if(dc.SetTextColor(RGBForeColor)==CLR_INVALID)
	{
		return 0;
	}
	// fill in the background
	dc.FillSolidRect(ClientRect,RGBBackColor);
	// convert the tab number to text
	TCHAR Buf[16]=_T("");
	(void)_itot(m_TabNumber,Buf,10);
	// draw the text
	if(dc.DrawText(
		Buf,-1,ClientRect,DT_CENTER|DT_SINGLELINE|DT_VCENTER)==0)
	{
		return 0;
	}
	return 0;
}

LRESULT CTabNumber::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