Sizing window

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi every one
I have create a window that have a hole in it
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/68288
I want to write event to resize this window.
I handle three event:
LButtonDown
MouseMove
LButtonUp
But the window does not do as my expect.
Here is the code
<pre class="prettyprint // TransparentWindowDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TransparentWindow1.h"
#include "TransparentWindowDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTransparentWindow1Dlg dialog

#define TopLeftDirection 0
#define TopRightDirection 1
#define BottomLeftDirection 2
#define BottomRightDirection 3
#define LeftDirection 4
#define RightDirection 5
#define TopDirection 6
#define BottomDirection 7



CTransparentWindow1Dlg::CTransparentWindow1Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CTransparentWindow1Dlg::IDD, pParent)
{
IsReadyCapture = false;
SizeDirection = -1;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTransparentWindow1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CTransparentWindow1Dlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SIZE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_MBUTTONUP()
END_MESSAGE_MAP()


// CTransparentWindow1Dlg message handlers

BOOL CTransparentWindow1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the applications main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CTransparentWindow1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);


}
else
{
CPaintDC dc(this);
CRect clientRect;
GetClientRect(clientRect);
CBrush blueBrush (RGB(0, 0, 255));
dc.FillRect(clientRect, &blueBrush);

CBrush redBrush(RGB(255, 0, 0));
CRect rect(clientRect.left, clientRect.top, clientRect.left + 10, clientRect.top + 10);
dc.FillRect(rect, &redBrush);

rect.OffsetRect(clientRect.Width() - 10, 0);
dc.FillRect(rect, &redBrush);

rect.OffsetRect(0, clientRect.Height() - 10);
dc.FillRect(rect, &redBrush);

rect.OffsetRect(10 - clientRect.Width(), 0);
dc.FillRect(rect, &redBrush);
CDialogEx::OnPaint();
}
}

// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTransparentWindow1Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}

void CTransparentWindow1Dlg::ShowHoles()
{
CRect winRect;
GetWindowRect(winRect);
winRect -= winRect.TopLeft();

CRgn rgnWnd;
rgnWnd.CreateRectRgn(winRect.left, winRect.top,winRect.right,winRect.bottom);

CRect rcClient(winRect);
rcClient.DeflateRect(5, 5);

CRgn HoleRgn;
HoleRgn.CreateRectRgn(rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);

CRgn rgnDiff;
rgnDiff.CreateRectRgn(0,0,0,0);
rgnDiff.CombineRgn(&rgnWnd,&HoleRgn,RGN_DIFF);

SetWindowRgn(rgnDiff,TRUE);

}


void CTransparentWindow1Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
ShowHoles();
// TODO: Add your message handler code here
}


void CTransparentWindow1Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect clientRect;
GetClientRect(clientRect);

IsReadyCapture = true;

CRect topLeftRect(0, 0, 10, 10);
topLeftRect.OffsetRect(clientRect.left, clientRect.top);

CRect topRightRect(0, 0, 10, 10);
topRightRect.OffsetRect(clientRect.right - 10, clientRect.top);

CRect bottomLeftRect(0, 0, 10, 10);
topRightRect.OffsetRect(clientRect.left, clientRect.bottom - 10);

CRect bottomRightRect(0, 0, 10, 10);
topRightRect.OffsetRect(clientRect.right - 10, clientRect.bottom - 10);

CRect leftRect(0, 0, 5, clientRect.Height() - 20);
leftRect.OffsetRect(clientRect.left, clientRect.top + 10);

CRect rightRect(0, 0, 5, clientRect.Height() - 20);
rightRect.OffsetRect(clientRect.right - 5, clientRect.top + 10);

CRect topRect(0, 0, clientRect.Width() - 20, clientRect.top + 5);
topRect.OffsetRect(10, 0);

CRect bottomRect(0, 0, clientRect.Width() - 20, 5);
bottomRect.OffsetRect(10, clientRect.bottom - 5);

if (topLeftRect.PtInRect(point))
SizeDirection = TopLeftDirection;
else if (topRightRect.PtInRect(point))
SizeDirection = TopRightDirection;
else if (bottomLeftRect.PtInRect(point))
SizeDirection = BottomLeftDirection;
else if (bottomRightRect.PtInRect(point))
SizeDirection = BottomRightDirection;
else if (leftRect.PtInRect(point))
SizeDirection = LeftDirection;
else if (rightRect.PtInRect(point))
SizeDirection = RightDirection;
else if (topRect.PtInRect(point))
SizeDirection = TopDirection;
else if (bottomRect.PtInRect(point))
SizeDirection = BottomDirection;
else SizeDirection = -1;

OldPoint.SetPoint(point.x, point.y);

CDialogEx::OnLButtonDown(nFlags, point);
}


void CTransparentWindow1Dlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
IsReadyCapture = false;
SizeDirection = -1;

CDialogEx::OnLButtonUp(nFlags, point);
}


void CTransparentWindow1Dlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (IsReadyCapture && SizeDirection >= TopLeftDirection)
{
int dx = point.x - OldPoint.x;
int dy = point.y - OldPoint.y;

CRect winRect;
GetWindowRect(winRect);

CPoint topLeft = winRect.TopLeft();
int width = winRect.Width();
int height = winRect.Height();

switch(SizeDirection)
{
case TopLeftDirection:
topLeft.Offset(dx, dy);
width -= dx;
height -= dy;
break;
case TopRightDirection:
topLeft.Offset(0, dy);
width += dx;
height -= dy;
break;
case BottomLeftDirection:
topLeft.Offset(dx, 0);
width -= dx;
height += dy;
break;
case BottomRightDirection:
width += dx;
height += dy;
break;
case LeftDirection:
topLeft.Offset(dx, 0);
width -= dx;
break;
case RightDirection:
width += dx;
break;
case TopDirection:
topLeft.Offset(0, dy);
height -= dy;
break;
case BottomDirection:
height += dy;
break;
}

if (width >= 50 && height >= 50)
{
OldPoint.SetPoint(point.x, point.y);
MoveWindow(topLeft.x, topLeft.y, width, height, TRUE);
}

}
CDialogEx::OnMouseMove(nFlags, point);
}


void CTransparentWindow1Dlg::OnMButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CDialogEx::OnMButtonUp(nFlags, point);
}
[/code]

Please give me some advises



View the full article
 
Back
Top