*自己写的总是不如意,求个自绘边框标题栏的源代码*解决方案
***自己写的总是不如意,求个自绘边框标题栏的源代码*******
我的总是不能平滑绘制,伸缩窗口时总是有点闪烁,弄一天了,又急用,汗一把。
邮箱:liqisong611@yahoo.com.cn 满足伸缩窗口时能平滑绘制立即加分!
最好SDK方式的,MFC也无妨。
------解决方案--------------------
Note:
There are several steps in OnNcEraseBkgnd
1. DefWindowProc
2. Draw what you want
3. Always return TRUE.
------解决方案--------------------
LRESULT CMainFrame::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult=CFrameWnd::DefWindowProc(message, wParam, lParam);
if (!::IsWindow(m_hWnd))
return lResult;
if (message == WM_NCPAINT || message == WM_NCACTIVATE || message==WM_NOTIFY)
{
CDC *pDC=GetWindowDC();
CRect RectWnd, RectTitle, RectButtons;
GetWindowRect(&RectWnd);
//取得整个标题栏的矩形
RectTitle.left=GetSystemMetrics(SM_CXFRAME);
RectTitle.top=GetSystemMetrics(SM_CYFRAME);
RectTitle.right=RectWnd.right-RectWnd.left-
GetSystemMetrics(SM_CXFRAME);
RectTitle.bottom=RectTitle.top+GetSystemMetrics(SM_CYSIZE);
//重画标题栏
CBrush brush;
brush.CreateSolidBrush(RGB(0,128,255));
pDC-> FillRect(RectTitle,&brush);
//重画图标
HICON hIcon=(HICON)::GetClassLong(m_hWnd, GCL_HICON);
m_SaveIcon.left=RectTitle.right-GetSystemMetrics(SM_CYSMICON);
m_SaveIcon.top=RectTitle.top+1;
m_SaveIcon.right=m_SaveIcon.left+GetSystemMetrics(SM_CXSMICON);
m_SaveIcon.bottom=m_SaveIcon.top+GetSystemMetrics(SM_CYSMICON);
::DrawIconEx(pDC-> m_hDC, m_SaveIcon.left, m_SaveIcon.top,hIcon,
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
0, NULL, DI_NORMAL);
//记录图标屏幕位置
m_SaveIcon.OffsetRect(RectWnd.TopLeft());
//重画最小化按钮
int nButtHeight=GetSystemMetrics(SM_CYSMSIZE);
RectButtons.left=RectTitle.left;
RectButtons.top=RectTitle.top+
(GetSystemMetrics(SM_CYSIZE)-nButtHeight)/2;
RectButtons.right=RectButtons.left+GetSystemMetrics(SM_CXSMSIZE);
RectButtons.bottom=RectButtons.top+nButtHeight;
pDC-> DrawFrameControl(&RectButtons, DFC_CAPTION, DFCS_CAPTIONMIN);
m_SaveButtonMin=RectButtons;
//记录最小化按钮屏幕位置
m_SaveButtonMin.OffsetRect(RectWnd.TopLeft());
//重画最大化或恢复按钮
RectButtons.left=RectButtons.right;
RectButtons.right=RectButtons.left+GetSystemMetrics(SM_CXSMSIZE);
pDC-> DrawFrameControl(&RectButtons, DFC_CAPTION, IsZoomed() ?
DFCS_CAPTIONRESTORE : DFCS_CAPTIONMAX);
m_SaveButtonMax=RectButtons;
//记录最大化按钮屏幕位置
m_SaveButtonMax.OffsetRect(RectWnd.TopLeft());
//重画关闭按钮
RectButtons.left=RectButtons.right;
RectButtons.right=RectButtons.left+GetSystemMetrics(SM_CXSMSIZE);
pDC-> DrawFrameControl(&RectButtons, DFC_CAPTION, DFCS_CAPTIONCLOSE);
m_SaveButtonClose=RectButtons;
//记录关闭按钮屏幕位置
m_SaveButtonClose.OffsetRect(RectWnd.TopLeft());
//重画标题栏
int nOldMode=pDC-> SetBkMode(TRANSPARENT);
COLORREF clOldText=pDC-> SetTextColor(RGB(0, 0, 0));
pDC-> SelectStockObject(ANSI_FIXED_FONT);
RectTitle.right-=GetSystemMetrics(SM_CYSMICON);
pDC-> DrawText((LPSTR) "我的自定义标题栏 ",-1, &RectTitle, DT_CENTER);
pDC-> SetBkMode(nOldMode);
pDC-> SetTextColor(clOldText);
}
return lResult;
}
void CMainFrame::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
CFrameWnd::OnNcLButtonDown(nHitTest, point);
//检测最小,最大和关闭按钮是否按到
if (m_SaveButtonClose.PtInRect(point))
SendMessage(WM_CLOSE);
else if (m_SaveButtonMin.PtInRect(point))
SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, MAKELPARAM(point.x, point.y) );
else if (m_SaveButtonMax.PtInRect(point))
我的总是不能平滑绘制,伸缩窗口时总是有点闪烁,弄一天了,又急用,汗一把。
邮箱:liqisong611@yahoo.com.cn 满足伸缩窗口时能平滑绘制立即加分!
最好SDK方式的,MFC也无妨。
------解决方案--------------------
Note:
There are several steps in OnNcEraseBkgnd
1. DefWindowProc
2. Draw what you want
3. Always return TRUE.
------解决方案--------------------
LRESULT CMainFrame::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult=CFrameWnd::DefWindowProc(message, wParam, lParam);
if (!::IsWindow(m_hWnd))
return lResult;
if (message == WM_NCPAINT || message == WM_NCACTIVATE || message==WM_NOTIFY)
{
CDC *pDC=GetWindowDC();
CRect RectWnd, RectTitle, RectButtons;
GetWindowRect(&RectWnd);
//取得整个标题栏的矩形
RectTitle.left=GetSystemMetrics(SM_CXFRAME);
RectTitle.top=GetSystemMetrics(SM_CYFRAME);
RectTitle.right=RectWnd.right-RectWnd.left-
GetSystemMetrics(SM_CXFRAME);
RectTitle.bottom=RectTitle.top+GetSystemMetrics(SM_CYSIZE);
//重画标题栏
CBrush brush;
brush.CreateSolidBrush(RGB(0,128,255));
pDC-> FillRect(RectTitle,&brush);
//重画图标
HICON hIcon=(HICON)::GetClassLong(m_hWnd, GCL_HICON);
m_SaveIcon.left=RectTitle.right-GetSystemMetrics(SM_CYSMICON);
m_SaveIcon.top=RectTitle.top+1;
m_SaveIcon.right=m_SaveIcon.left+GetSystemMetrics(SM_CXSMICON);
m_SaveIcon.bottom=m_SaveIcon.top+GetSystemMetrics(SM_CYSMICON);
::DrawIconEx(pDC-> m_hDC, m_SaveIcon.left, m_SaveIcon.top,hIcon,
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
0, NULL, DI_NORMAL);
//记录图标屏幕位置
m_SaveIcon.OffsetRect(RectWnd.TopLeft());
//重画最小化按钮
int nButtHeight=GetSystemMetrics(SM_CYSMSIZE);
RectButtons.left=RectTitle.left;
RectButtons.top=RectTitle.top+
(GetSystemMetrics(SM_CYSIZE)-nButtHeight)/2;
RectButtons.right=RectButtons.left+GetSystemMetrics(SM_CXSMSIZE);
RectButtons.bottom=RectButtons.top+nButtHeight;
pDC-> DrawFrameControl(&RectButtons, DFC_CAPTION, DFCS_CAPTIONMIN);
m_SaveButtonMin=RectButtons;
//记录最小化按钮屏幕位置
m_SaveButtonMin.OffsetRect(RectWnd.TopLeft());
//重画最大化或恢复按钮
RectButtons.left=RectButtons.right;
RectButtons.right=RectButtons.left+GetSystemMetrics(SM_CXSMSIZE);
pDC-> DrawFrameControl(&RectButtons, DFC_CAPTION, IsZoomed() ?
DFCS_CAPTIONRESTORE : DFCS_CAPTIONMAX);
m_SaveButtonMax=RectButtons;
//记录最大化按钮屏幕位置
m_SaveButtonMax.OffsetRect(RectWnd.TopLeft());
//重画关闭按钮
RectButtons.left=RectButtons.right;
RectButtons.right=RectButtons.left+GetSystemMetrics(SM_CXSMSIZE);
pDC-> DrawFrameControl(&RectButtons, DFC_CAPTION, DFCS_CAPTIONCLOSE);
m_SaveButtonClose=RectButtons;
//记录关闭按钮屏幕位置
m_SaveButtonClose.OffsetRect(RectWnd.TopLeft());
//重画标题栏
int nOldMode=pDC-> SetBkMode(TRANSPARENT);
COLORREF clOldText=pDC-> SetTextColor(RGB(0, 0, 0));
pDC-> SelectStockObject(ANSI_FIXED_FONT);
RectTitle.right-=GetSystemMetrics(SM_CYSMICON);
pDC-> DrawText((LPSTR) "我的自定义标题栏 ",-1, &RectTitle, DT_CENTER);
pDC-> SetBkMode(nOldMode);
pDC-> SetTextColor(clOldText);
}
return lResult;
}
void CMainFrame::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
CFrameWnd::OnNcLButtonDown(nHitTest, point);
//检测最小,最大和关闭按钮是否按到
if (m_SaveButtonClose.PtInRect(point))
SendMessage(WM_CLOSE);
else if (m_SaveButtonMin.PtInRect(point))
SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, MAKELPARAM(point.x, point.y) );
else if (m_SaveButtonMax.PtInRect(point))