Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WTL

Form Designer

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

#include "stdafx.h"
#include "Fixes.h"

void DrawDragRect(CDC &dc, LPCRECT lpRect, SIZE size, LPCRECT lpRectLast, SIZE sizeLast, HBRUSH hBrush, HBRUSH hBrushLast)
{
	// first, determine the update region and select it
	HRGN hRgnNew;
	HRGN hRgnOutside, hRgnInside;
	hRgnOutside = ::CreateRectRgnIndirect(lpRect);
	RECT rect = *lpRect;
	::InflateRect(&rect, -size.cx, -size.cy);
	::IntersectRect(&rect, &rect, lpRect);
	hRgnInside = ::CreateRectRgnIndirect(&rect);
	hRgnNew = ::CreateRectRgn(0, 0, 0, 0);
	::CombineRgn(hRgnNew, hRgnOutside, hRgnInside, RGN_XOR);

	HBRUSH hBrushOld = NULL;
	if(hBrush == NULL)
		hBrush = CDCHandle::GetHalftoneBrush();
	if(hBrushLast == NULL)
		hBrushLast = hBrush;

	HRGN hRgnLast = NULL, hRgnUpdate = NULL;
	if(lpRectLast != NULL)
	{
		// find difference between new region and old region
		hRgnLast = ::CreateRectRgn(0, 0, 0, 0);
		::SetRectRgn(hRgnOutside, lpRectLast->left, lpRectLast->top, lpRectLast->right, lpRectLast->bottom);
		rect = *lpRectLast;
		::InflateRect(&rect, -sizeLast.cx, -sizeLast.cy);
		::IntersectRect(&rect, &rect, lpRectLast);
		::SetRectRgn(hRgnInside, rect.left, rect.top, rect.right, rect.bottom);
		::CombineRgn(hRgnLast, hRgnOutside, hRgnInside, RGN_XOR);

		// only diff them if brushes are the same
		if(hBrush == hBrushLast)
		{
			hRgnUpdate = ::CreateRectRgn(0, 0, 0, 0);
			::CombineRgn(hRgnUpdate, hRgnLast, hRgnNew, RGN_XOR);
		}
	}
	if(hBrush != hBrushLast && lpRectLast != NULL)
	{
		// brushes are different -- erase old region first
		SelectClipRgn(dc,hRgnLast);
		GetClipBox(dc,&rect);
		hBrushOld = (HBRUSH)SelectObject(dc,hBrushLast);
		PatBlt(dc,rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, PATINVERT);
		SelectObject(dc,hBrushOld);
		hBrushOld = NULL;
	}

	// draw into the update/new region
	SelectClipRgn(dc,hRgnUpdate != NULL ? hRgnUpdate : hRgnNew);
	GetClipBox(dc,&rect);
	hBrushOld = (HBRUSH)SelectObject(dc,hBrush);
	PatBlt(dc,rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, PATINVERT);

	// cleanup DC
	if(hBrushOld != NULL)
		SelectObject(dc,hBrushOld);
	SelectClipRgn(dc,NULL);
	
	DeleteObject(hRgnOutside);
	DeleteObject(hRgnInside);
	DeleteObject(hRgnNew);
	if(hRgnLast!=NULL)
	{
		DeleteObject(hRgnLast);
	}
	if(hRgnUpdate!=NULL)
	{
		DeleteObject(hRgnUpdate);
	}
	if(hBrush!=NULL)
	{
		DeleteObject(hBrush);
	}
}

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