首页 > 代码库 > VC创建圆角窗体

VC创建圆角窗体

有时候根据具体需求,我们可能要改变窗口的形状。使用API函数SetWindowRgn可以改变一个窗体的可见范围。

使用API函数CreateRoundRectRgn()函数可以创建一个圆角矩形区域。


图片ID为:IDB_BACKBMP

BOOL CDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	// when the application's 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
	m_back.LoadBitmap(IDB_BACKBMP);
	CRgn rgn;
	rgn.CreateRoundRectRgn(3,3,700,650,20,20);
	SetWindowRgn(rgn,TRUE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}
void CDemoDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CDC picDC;
	picDC.CreateCompatibleDC (&dc);

	CBitmap *pOldBmp;
	pOldBmp = picDC.SelectObject (&m_back);
	BITMAP bm;
	m_back.GetBitmap(&bm);
	dc.BitBlt (0,0,bm.bmWidth ,bm.bmHeight,&picDC,0,0,SRCCOPY);
	dc.SelectObject(pOldBmp); 
}

void OnSize(UINT nType, int cx, int cy) 
{ 
	CDialog::OnSize(nType, cx, cy); 
	
	// TODO: 在此处添加消息处理程序代码 
	CRgn MyRgn; 
	CRect rect; 
	GetWindowRect(&rect); 
	rect -= rect.TopLeft(); 
	MyRgn.CreateRoundRectRgn(rect.left,rect.top, rect.right, rect.bottom, 3,3); 
	SetWindowRgn(MyRgn,TRUE); 
} 



VC创建圆角窗体