mfc 画圆角矩形

清泛原创
先看效果:

画圆角矩形的函数:

BOOL DrawRoundRect( CDC* pDC, LPCRECT lprcRect, SIZE sizeRound, DWORD dwColorFrame = COLOR_TRANSPARENT, DWORD dwColorFill = COLOR_TRANSPARENT )
{
	ASSERT_RESULT( NULL != pDC );

	COLORREF crBackground;
	crBackground = pDC->GetBkColor();

	HRGN hrgnFrame = ::CreateRoundRectRgn( lprcRect->left, lprcRect->top, lprcRect->right + 1, lprcRect->bottom + 1, sizeRound.cx, sizeRound.cy );

	if ( COLOR_TRANSPARENT == dwColorFrame && COLOR_TRANSPARENT != dwColorFill )
	{
		// fill it only			
		CBrush brushFill( dwColorFill );		
		FillRgn(  pDC->GetSafeHdc(), hrgnFrame, brushFill );
	}

	else
	{
		HRGN hrgnFill = ::CreateRoundRectRgn( lprcRect->left + 1, lprcRect->top + 1, lprcRect->right, lprcRect->bottom, sizeRound.cx, sizeRound.cy );
		if ( COLOR_TRANSPARENT != dwColorFrame && COLOR_TRANSPARENT != dwColorFill )
		{
			// fill and frame
			CBrush brushFillFrame( dwColorFrame );	 
			FillRgn(   pDC->GetSafeHdc(),  hrgnFrame, brushFillFrame );

			CBrush brushFill( dwColorFill );
			FillRgn(   pDC->GetSafeHdc(), hrgnFill, brushFill );

		}
		else
		{
			// frame only
			::CombineRgn( hrgnFrame, hrgnFrame, hrgnFill, RGN_DIFF );

			CBrush brushFillFrame( dwColorFrame );	
			FillRgn(   pDC->GetSafeHdc(), hrgnFrame, brushFillFrame );

		}

		::DeleteObject( (HGDIOBJ)hrgnFill );
	}

	::DeleteObject( (HGDIOBJ)hrgnFrame );

	pDC->SetBkColor( crBackground );

	return TRUE;
}

调用方法:
void CxxxView::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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;
	}
	else
	{		
		CRect rc(10, 10, 100, 80);
		//画圆角矩形
		SIZE sizeRect = {3, 3};
		DrawRoundRect(this->GetDC(), rc, sizeRect);
}

这里直接使用的窗口DC,实际应用中为了防止出现闪屏现象,需要使用MemDC,调用方法类似,大家自行调整下。


推荐:
MFC采用双缓存解决闪屏问题(使用了双缓存还是闪屏的)

mfc 画图 圆角矩形

分享到:
评论加载中,请稍后...
创APP如搭积木 - 创意无限,梦想即时!
回到顶部