Click here to Skip to main content
6,595,444 members and growing! (18,920 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » MFC » General     Beginner License: The Code Project Open License (CPOL)

A Customized Window for MFC Applications

By PrasadPerera

This article is to basically explain how to create a customized drawn window for MFC applications.
C++, Windows, .NET 2.0, MFC, Dev
Posted:1 Dec 2008
Views:15,045
Bookmarked:26 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 4.66 Rating: 3.58 out of 5
4 votes, 20.0%
1

2
3 votes, 15.0%
3
5 votes, 25.0%
4
8 votes, 40.0%
5

Introduction

This article came as a result of trying to create a custom border for the MFC application. The simplest way to customize the window of an application is to bind it to a specific theme that gives it the look and feel we want. But sometimes we can't get the specific design we want and more importantly, the shape we want. So that gave the idea of removing the existing borders and title bars and making the window handle the basic actions which the borders and title bar can.

So when we remove the title bar and the border from the window, we have to handle some basic actions which they perform. Those are:

  1. Moving the window
  2. Resizing the window
  3. Close, Restore and Minimize

The Code

First we can remove the title bar and clip the region for the window:

BOOL CCustomFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
	CRect rect;
	GetClientRect(rect);
	//Create the custom region for the window
	m_CustomRgn.CreateRoundRectRgn(rect.left + winStats.frm_clp_wdth,
		rect.top + winStats.frm_clp_wdth,
		rect.right - winStats.frm_clp_wdth,
		rect.bottom - winStats.frm_clp_wdth,
		20, 20);
		//Set the window region
        VERIFY(SetWindowRgn(m_CustomRgn , TRUE ));
	return CFrameWnd::OnCreateClient(lpcs, pContext);
}
BOOL CCustomFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle , 
			CWnd* pParentWnd , CCreateContext* pContext)
{
	BOOL bRet = CFrameWnd::LoadFrame
		(nIDResource, dwDefaultStyle, pParentWnd, pContext);
	if(bRet)
	{
	    //Remove the titlebar from the frame using the ModifyStyle
		ModifyStyle(WS_CAPTION, WS_CLIPCHILDREN);
	    //Remove the border
		ModifyStyleEx(WS_EX_CLIENTEDGE,0);
	}
	return true;
}

As above, we can override the functions OnCreateClient and LoadFrame to modify the window.

Next since we remove the borders and title bar, we can use the basic events to capture the actions which now the borderless window cannot. For this, we can use...

afx_msg void OnLButtonDown(UINT nFlags, CPoint point) 

... and capture the default message processing. Based on the region clicked, we can let the window process different messages which will perform the tasks. To process the messages, DefWindowProc can be used.

if (r_windRects.r_Move.PtInRect(point)){
		DefWindowProc(WM_SYSCOMMAND, 
				SC_MOVE + 1 ,MAKELPARAM(point.x,point.y));
		return 0;
	}
	if (Val == -1)
		return 0;

	::SetCursor(AfxGetApp()->LoadStandardCursor(cursor));
	//Send the resize messages
	DefWindowProc(WM_SYSCOMMAND, SC_SIZE + Val ,MAKELPARAM(point.x ,point.y));
	return 0;

With this part of the code, resizing and moving operations are handled. Since it processes the resizing, now we can resize the window and clip it so that we can keep the basic shape we want.

void CCustomFrame::OnSize(UINT nType, int cx, int cy)
{
	CRect rect;
	GetClientRect(rect);
         //Detach the previous region
	m_CustomRgn.Detach();
	//Create and set the new region for the window!
	m_CustomRgn.CreateRoundRectRgn(rect.left + winStats.frm_clp_wdth,
		rect.top + winStats.frm_clp_wdth
		, rect.right- winStats.frm_clp_wdth,
		rect.bottom - winStats.frm_clp_wdth, 30, 30);

	SetWindowRgn(m_CustomRgn, TRUE);
	CFrameWnd::OnSize(nType, cx, cy);
}

Another thing to handle is the maximization and restoration of the frame. We can't use ShowWindow(SW_SHOWMAXIMIZED) since we already clipped some of the area from the window. So the below code is used for that:

int CCustomFrame::maximize(void)
{
	//The custom maximize function. 
	//This is needed since we clipped the frame and the border.
	CRect rect,rectD, rect1;
	GetClientRect(rect);
	GetWindowRect(m_PrevRect);
	//Get the screen size!
	int scrWidth  = GetSystemMetrics(SM_CXSCREEN);
	int scrHeight = GetSystemMetrics(SM_CYSCREEN);

	rect.right = scrWidth + winStats.frm_clp_wdth;
	rect.bottom = scrHeight + winStats.frm_clp_wdth;
	rect.left -= winStats.frm_clp_wdth;
	rect.top -= winStats.frm_clp_wdth;
	//Get the taskbar rect and set the bottom to taskbar rect top parameter
	CWnd* pTray = FindWindow(_T("Shell_TrayWnd"), _T(""));
	pTray->GetWindowRect(&rectD);
	rect.bottom = rectD.top + winStats.frm_clp_wdth;
	MoveWindow(rect);
	b_maximized = TRUE;
	return 0;
}

Points of Interest

The good part of doing this is that by handling some basic window actions and events, we can create the borders as we like. Since we paint the borders, they won't change prior to the Windows platform it runs (e.g.: XP or Vista). We can also use the CDialog class instead of CFrameWnd to create the custom window.

But the bad point is handling processing of many messages. Moreover, there can be small defects in drawing things like flickering. So this code might not have a commercial value, but it can be a good learning material!

History

  • 30th November, 2008: Initial version

License

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

About the Author

PrasadPerera


Member
Prasad Perera is a software developer who's interested in parallel and distributed computing and graphics programming!
Occupation: Software Developer
Location: Sri Lanka Sri Lanka

Other popular MFC articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
Generalhow to fit an application from winexec in rightpane of splitter window PinmemberMember 333413523:48 25 Oct '09  
GeneralSplitting Window Pinmembersamal.subhashree21:29 29 Sep '09  
GeneralRe: Splitting Window PinmemberPrasadPerera4:26 30 Sep '09  
GeneralMDI Main Frame [modified] Pinmembertpchen23:54 13 Apr '09  
GeneralRe: MDI Main Frame PinmemberPrasadSRCSE17:34 16 Apr '09  
GeneralMy vote of 1 Pinmembermaheshvarma096:50 26 Mar '09  
GeneralRe: My vote of 1 PinmemberPrasadSRCSE18:21 26 Mar '09  
QuestionI like the look Pinmemberroni_rgk22:36 9 Feb '09  
AnswerRe: I like the look PinmemberPrasadSRCSE14:30 11 Feb '09  
GeneralLearning material PinmemberAlexEvans14:57 15 Dec '08  
GeneralRe: Learning material PinmemberPrasadSRCSE0:26 16 Dec '08  
GeneralRe: Learning material PinmemberAlexEvans11:34 16 Dec '08  
GeneralMy vote of 1 PinmemberMember 286955112:45 8 Dec '08  
GeneralRe: My vote of 1 PinmemberPrasadSRCSE18:16 8 Dec '08  
GeneralMy vote of 1 PinmemberAndre xxxxxxx2:43 3 Dec '08  
GeneralRe: My vote of 1 PinmemberPrasadSRCSE17:22 3 Dec '08  
GeneralUpdated demo! PinmemberPrasadSRCSE14:34 2 Dec '08  
Questionvery bad PinmemberMember 214676823:46 1 Dec '08  
AnswerRe: very bad PinmemberPrasadSRCSE1:44 2 Dec '08  
GeneralMy vote of 1 PinmemberKfeeder23:16 1 Dec '08  
GeneralRe: My vote of 1 PinmemberPrasadSRCSE1:45 2 Dec '08  
GeneralRe: My vote of 1 Pinmembereleqi4:42 2 Dec '08  
GeneralRe: My vote of 1 PinmemberPrasadSRCSE5:33 2 Dec '08  
Generalresource error. Pinmember15:37 1 Dec '08  
GeneralRe: resource error. PinmemberPrasadSRCSE18:21 1 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Dec 2008
Editor: Deeksha Shenoy
Copyright 2008 by PrasadPerera
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project