From: scstech@solomon.technet.sg (Arthur Lim)
Subject: Help with MFC !!!!
Organization: Singapore Computer Systems Ltd
Date: Thu, 10 Sep 1992 10:34:40 GMT

Hi,

I encountered a couple of problem converting a Windows C program to use
MFC. Here's the senerio :

In a FrameWindow (of CFrameWnd class) I have a couple of windows derived
from CWnd. These child windows are borderless. The task I wanted to to
was to be able to resize and move these child windows, using rubberband
lines, before finally moving/resizing the actual window.

The problems I encountered were :

1) The rubberbanding lines won't appear. (Well, somestimes they do and
   sometimes they don't). It still performs the wanted function though.

2) After a move, the windows won't repaint even though I made a specific
   call to InvalidateRect(NULL, TRUE) at the point after moving the window.
   If the window is resized, then it's OK.

I have appended the segment of my codes that does the above. I hope some
kind souls could shine some light for me.

Thanks in advance.

+======================================================================+
| Arthur Lim                     | #include <std.disclaimer>           |
| Advanced Software R&D Centre   |                                     |
| Singapore Computer Systems Ltd | scstech@solomon.technet.sg          |
|                                |                                     |
| (Voice) 065-240-3850           | Please prefix "For Arthur :" in the |
| (Fax)   065-441-2811           | subject line when mailing. Thanks.  |
+======================================================================+

======================= C U T   H E R E ===============================
**********************
******PANEL.H*********
**********************

class MyWindow;

class MyPanel : public CWnd
{ 
	CString *	csPanelName;
	BOOL		bStartMove;
	CRect		rect;
	CRect		rectParent;
	CRect		rectNew;
	CPoint		ptOrigin;
	CDC *		hdc;
	CPen		hpen;
	char		MoveMode;
	CPen *		old_pen;
	CBrush *	old_brush;
	int		old_ROP;

	char		DetermineMoveMode(CPoint &point);
	
public:
			MyPanel(MyWindow *, char *, BOOL dynamic=FALSE);
	afx_msg void	OnPaint();
	afx_msg void	OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void	OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void	OnMouseMove(UINT nFlags, CPoint point);

	DECLARE_MESSAGE_MAP()
};

#endif
---------------------------------------------------------------------------
***********************
*******PANEL.CPP*******
***********************

#include "panel.h"

#define MV_LIMIT	5
#define MV_LEFT 	1
#define MV_RIGHT	2
#define MV_TOP		4
#define MV_BOTTOM	8
#define MV_WINDOW	15

BEGIN_MESSAGE_MAP (MyPanel, CWnd)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_PAINT()
END_MESSAGE_MAP()

MyPanel::MyPanel(MyWindow *parent, char *pnl_name, BOOL dynamic) 
{
	CRect		rectDefault;

	csPanelName = new CString(pnl_name);
	bDynamic = dynamic;
	bStartMove = FALSE;
	rectDefault.SetRect(10, 10, 110, 110);
	Create(VTPANEL, "", WS_CHILD, rectDefault, (CWnd *)parent, 0);
	ShowWindow(SW_SHOW);
	UpdateWindow();
}

char MyPanel::DetermineMoveMode(CPoint &point)
{
	CRect		rect;
	char		mode = 0;

	GetClientRect(&rect);
	if(point.x < MV_LIMIT)
	{
		mode = MV_LEFT;
		if(point.y < 2*MV_LIMIT)
			mode |= MV_TOP;
		else
		if(point.y > rect.Height() - 2*MV_LIMIT)
			mode |= MV_BOTTOM;
	}
	else
	if(point.x > rect.Width() - MV_LIMIT)
	{
		mode = MV_RIGHT;
		if(point.y < 2*MV_LIMIT)
			mode |= MV_TOP;
		else
		if(point.y > rect.Height() - 2*MV_LIMIT)
			mode |= MV_BOTTOM;
	}
	else
	if(point.y < MV_LIMIT)
	{
		mode = MV_TOP;
		if(point.x < 2*MV_LIMIT)
			mode |= MV_LEFT;
		else
		if(point.x > rect.Width() - 2*MV_LIMIT)
			mode |= MV_RIGHT;
	}
	else
	if(point.y > rect.Height() - MV_LIMIT)
	{
		mode = MV_BOTTOM;
		if(point.x < 2*MV_LIMIT)
			mode |= MV_LEFT;
		else
		if(point.x > rect.Width() - 2*MV_LIMIT)
			mode |= MV_RIGHT;
	}
	if(!mode)
		mode = MV_WINDOW;

	return mode;
}

afx_msg void MyPanel::OnPaint()
{
	CRect		rect;
	PAINTSTRUCT	ps;
	CDC *		dc;
	CPen		whitepen,
			blackpen;

	dc = BeginPaint(&ps);
	GetClientRect(&rect);
	rect.bottom -= 2;
	rect.right  -= 2;
	whitepen.CreatePen(PS_SOLID | PS_INSIDEFRAME, 2, RGB(255, 255, 255));
	blackpen.CreatePen(PS_SOLID | PS_INSIDEFRAME, 2, RGB(0, 0, 0));
	dc->SelectObject(&blackpen);
	dc->MoveTo(rect.left, rect.bottom);
	dc->LineTo(rect.left, rect.top);
	dc->LineTo(rect.right, rect.top);
	blackpen.DeleteObject();
	dc->SelectObject(&whitepen);
	dc->LineTo(rect.right, rect.bottom);
	dc->LineTo(rect.left, rect.bottom);
	whitepen.DeleteObject();
	EndPaint(&ps);
}


afx_msg void MyPanel::OnLButtonDown(UINT nFlags, CPoint point)
{
	if(nFlags & MK_SHIFT)
	{
		ClientToScreen(&point);
		GetParent()->ScreenToClient(&point);
		ptOrigin.x = point.x;
		ptOrigin.y = point.y;
		GetWindowRect(&rect);
		GetParent()->ScreenToClient(&rect);
		GetParent()->GetClientRect(&rectParent);
		bStartMove = TRUE;
		rectNew = rect;
		SetCapture();
		hdc = GetParent()->GetDC();
		hpen.CreatePen(PS_SOLID, 1, RGB(0,0,0));
		old_pen   = hdc->SelectObject(&hpen);
		old_brush = (CBrush *)hdc->SelectStockObject(NULL_BRUSH);
		old_ROP   = hdc->SetROP2(R2_NOT);
		GetParent()->ClientToScreen(&point);
		ScreenToClient(&point);
		MoveMode = DetermineMoveMode(point);
		DrawRubberBand(hdc, &rectNew);
	}
}

afx_msg void MyPanel::OnLButtonUp(UINT nFlags, CPoint point)
{
	if(bStartMove)
	{
		DrawRubberBand(hdc, (CRect *)NULL);
		MoveWindow(&rectNew, TRUE);
		ReleaseCapture();
		hpen.DeleteObject();
		hdc->SelectObject(old_pen);
		hdc->SelectObject(old_brush);
		hdc->SetROP2(old_ROP);
		GetParent()->ReleaseDC(hdc);
		bStartMove = FALSE;
		InvalidateRect((CRect *)NULL, TRUE);
	}
						
}

afx_msg void MyPanel::OnMouseMove(UINT nFlags, CPoint point)
{
	if(bStartMove && (nFlags & (MK_SHIFT|MK_LBUTTON)))
	{
		ClientToScreen(&point);
		GetParent()->ScreenToClient(&point);
		int     x_offset = point.x - ptOrigin.x;
		int	y_offset = point.y - ptOrigin.y;
		hdc->SetROP2(R2_NOT);
		if(rectNew.left+x_offset < 0)
		        x_offset = -rectNew.left;
		else
		if(rectNew.right+x_offset > rectParent.right)
		        x_offset = rectParent.right - rectNew.right;
		if(rectNew.top+y_offset < 0)
		        y_offset = -rectNew.top;
		else
		if(rectNew.bottom+y_offset > rectParent.bottom)
		        y_offset = rectParent.bottom - rectNew.bottom;

		if((MoveMode & MV_LEFT) && (MoveMode & MV_RIGHT))
		{
		        rectNew.left += x_offset;
		        rectNew.right += x_offset;
		}
		else
		if(MoveMode & MV_LEFT)
		{
			if((rectNew.left + x_offset) > (rectNew.right - MV_LIMIT))
				x_offset = rectNew.right - MV_LIMIT - rectNew.left;
			rectNew.left += x_offset;
		}
		else
		if(MoveMode & MV_RIGHT)
		{
			if((rectNew.right + x_offset) < (rectNew.left + MV_LIMIT))
				x_offset = rectNew.left + MV_LIMIT - rectNew.right;
			rectNew.right += x_offset;
		}
		if((MoveMode & MV_TOP) && (MoveMode & MV_BOTTOM))
		{
			rectNew.top += y_offset;
			rectNew.bottom += y_offset;
		}
		else
		if(MoveMode & MV_TOP)
		{
			if((rectNew.top + y_offset) > (rectNew.bottom - MV_LIMIT))
				y_offset = rectNew.bottom - MV_LIMIT - rectNew.top;
			rectNew.top += y_offset;
		}
		else
		if(MoveMode & MV_BOTTOM)
		{
			if((rectNew.bottom + y_offset) < (rectNew.top + MV_LIMIT))
				y_offset = rectNew.top + MV_LIMIT - rectNew.bottom;
			rectNew.bottom += y_offset;
		}

		//DrawRubberBand(hdc, &rectNew);
		MoveWindow(&rectNew, TRUE);
		ptOrigin.Offset(x_offset, y_offset);
	}
}
======================= E N D   O F   S E G M E N T ===================