窗体资源中要把窗体边框风格设置为RESIZING。
MyDialog.cpp
// MyDialog.cpp : implementation file
//
#include "stdafx.h"
#include "Window.h"
#include "MyDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CMyDialog dialog
CMyDialog::CMyDialog(CWnd* pParent /*=NULL*/)
: CDialog(CMyDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDialog)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_bFlag = FALSE;
}
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDialog)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
//{{AFX_MSG_MAP(CMyDialog)
ON_WM_LBUTTONDOWN()
ON_WM_DESTROY()
ON_WM_NCPAINT()
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_WM_NCLBUTTONDOWN()
ON_WM_NCLBUTTONUP()
ON_WM_NCMOUSEMOVE()
ON_WM_NCHITTEST()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CMyDialog message handlers
void CMyDialog::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
void CMyDialog::OnCancel()
{
// TODO: Add extra cleanup here
//CDialog::OnCancel();
}
void CMyDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x, point.y));
CDialog::OnLButtonDown(nFlags, point);
}
void CMyDialog::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
}
void CMyDialog::OnNcPaint()
{
// TODO: Add your message handler code here
// Do not call CDialog::OnNcPaint() for painting messages
CDC * pDC = this->GetWindowDC();
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindow.OffsetRect(-rcWindow.left, -rcWindow.top);
pDC->FillSolidRect(rcWindow, RGB(0, 0, 128));
this->ReleaseDC(pDC);
}
BOOL CMyDialog::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return FALSE;
return CDialog::OnEraseBkgnd(pDC);
}
const WS_EX_LAYERED = 0x00080000;
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CRgn rgn;
CRect rect;
GetWindowRect(rect);
rect.OffsetRect(-rect.left, -rect.top);
rgn.CreateRoundRectRgn(rect.left, rect.top, rect.right, rect.bottom, 10, 10);
this->SetWindowRgn(rgn, TRUE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CMyDialog::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_bFlag)
{
HRGN temp;
int res = this->GetWindowRgn(temp);
if (res == ERROR || res == NULLREGION)
{
CRgn rgn;
CRect rect;
GetWindowRect(rect);
ClientToScreen(&point);
if (m_nHitTest == HTRIGHT)
rect.right = point.x;
else if (m_nHitTest == HTLEFT)
rect.left = point.x;
else if (m_nHitTest == HTTOP)
rect.top = point.y;
else if (m_nHitTest == HTBOTTOM)
rect.bottom = point.y;
else if (m_nHitTest == HTTOPLEFT)
{
rect.left = point.x;
rect.top = point.y;
}
else if (m_nHitTest == HTTOPRIGHT)
{
rect.right = point.x;
rect.top = point.y;
}
else if (m_nHitTest == HTBOTTOMLEFT)
{
rect.left = point.x;
rect.bottom = point.y;
}
else if (m_nHitTest == HTBOTTOMRIGHT)
{
rect.right = point.x;
rect.bottom = point.y;
}
this->SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_SHOWWINDOW);
rect.OffsetRect(-rect.left, -rect.top);
rgn.CreateRoundRectRgn(rect.left, rect.top, rect.right, rect.bottom, 10, 10);
this->SetWindowRgn(rgn, TRUE);
TRACE("SET RGN\n");
}
}
CDialog::OnMouseMove(nFlags, point);
}
void CMyDialog::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_bFlag)
{
TRACE("OnLButtonUp\n");
ReleaseCapture();
{
Sleep(0);
HRGN temp;
int res = this->GetWindowRgn(temp);
if (res == ERROR || res == NULLREGION)
{
CRgn rgn;
CRect rect;
GetWindowRect(rect);
ClientToScreen(&point);
if (m_nHitTest == HTRIGHT)
rect.right = point.x;
else if (m_nHitTest == HTLEFT)
rect.left = point.x;
else if (m_nHitTest == HTTOP)
rect.top = point.y;
else if (m_nHitTest == HTBOTTOM)
rect.bottom = point.y;
else if (m_nHitTest == HTTOPLEFT)
{
rect.left = point.x;
rect.top = point.y;
}
else if (m_nHitTest == HTTOPRIGHT)
{
rect.right = point.x;
rect.top = point.y;
}
else if (m_nHitTest == HTBOTTOMLEFT)
{
rect.left = point.x;
rect.bottom = point.y;
}
else if (m_nHitTest == HTBOTTOMRIGHT)
{
rect.right = point.x;
rect.bottom = point.y;
}
this->SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_SHOWWINDOW);
rect.OffsetRect(-rect.left, -rect.top);
rgn.CreateRoundRectRgn(rect.left, rect.top, rect.right, rect.bottom, 10, 10);
this->SetWindowRgn(rgn, TRUE);
TRACE("SET RGN\n");
}
}
m_bFlag = FALSE;
}
CDialog::OnLButtonUp(nFlags, point);
}
void CMyDialog::OnButton1()
{
// TODO: Add your control notification handler code here
GetParent()->Invalidate();
}
void CMyDialog::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (nHitTest == HTRIGHT || nHitTest == HTLEFT || nHitTest == HTTOP || nHitTest == HTBOTTOM
|| nHitTest == HTBOTTOMLEFT || nHitTest == HTBOTTOMRIGHT || nHitTest == HTTOPLEFT
|| nHitTest == HTTOPRIGHT)
{
m_nHitTest = nHitTest;
TRACE("OnNcLButtonDown\n");
SetCapture();
m_bFlag = TRUE;
this->SetWindowRgn(NULL, FALSE);
}
CDialog::OnNcLButtonDown(nHitTest, point);
}
void CMyDialog::OnNcLButtonUp(UINT nHitTest, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnNcLButtonUp(nHitTest, point);
}
void CMyDialog::OnNcMouseMove(UINT nHitTest, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnNcMouseMove(nHitTest, point);
}
UINT CMyDialog::OnNcHitTest(CPoint point)
{
// TODO: Add your message handler code here and/or call default
return CDialog::OnNcHitTest(point);
}
MyDialog.h
#if !defined(AFX_MYDIALOG_H__1E993F12_ADCE_4FAB_B85C_F09E1ABE0435__INCLUDED_)
#define AFX_MYDIALOG_H__1E993F12_ADCE_4FAB_B85C_F09E1ABE0435__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyDialog.h : header file
//
/
// CMyDialog dialog
class CMyDialog : public CDialog
{
// Construction
public:
CMyDialog(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CMyDialog)
enum { IDD = IDD_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyDialog)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CMyDialog)
virtual void OnOK();
virtual void OnCancel();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnDestroy();
afx_msg void OnNcPaint();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
virtual BOOL OnInitDialog();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnButton1();
afx_msg void OnNcLButtonDown(UINT nHitTest, CPoint point);
afx_msg void OnNcLButtonUp(UINT nHitTest, CPoint point);
afx_msg void OnNcMouseMove(UINT nHitTest, CPoint point);
afx_msg UINT OnNcHitTest(CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
BOOL m_bFlag;
int m_nHitTest;
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MYDIALOG_H__1E993F12_ADCE_4FAB_B85C_F09E1ABE0435__INCLUDED_)