初学者,请高手费心指点一下,一个小问题

blackcat242 2004-08-12 08:32:10
如何处理非用户区的消息,列如如果我要在标题栏画上自己的颜色,该怎么办,有人说要处理WM_NCPAINT消息,可是在类向导中找不到这个消息,不知如何处理,能不能给一小段代码的例子。谢谢!
...全文
74 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackcat242 2004-08-12
  • 打赏
  • 举报
回复
这个OnNcPaint()是在类向导中加入的吗?我怎么也找不到WM_NCPAINT这个消息呀
howtotell 2004-08-12
  • 打赏
  • 举报
回复
http://www.vckbase.com/document/viewdoc/?id=317
oyljerry 2004-08-12
  • 打赏
  • 举报
回复
WM_NCPAINT
The WM_NCPAINT message is sent to a window when its frame must be painted.

A window receives this message through its WindowProc function.

LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_NCPAINT
WPARAM wParam, // handle to update region (HRGN)
LPARAM lParam // not used
);
Parameters
wParam
Handle to the update region of the window. The update region is clipped to the window frame. When wParam is 1, the entire window frame needs to be updated.
lParam
This parameter is not used.
Return Values
An application returns zero if it processes this message.

Remarks
The DefWindowProc function paints the window frame.

An application can intercept the WM_NCPAINT message and paint its own custom window frame. The clipping region for a window is always rectangular, even if the shape of the frame is altered.

The wParam value can be passed to GetDCEx as in the following example.

case WM_NCPAINT:
{
HDC hdc;
hdc = GetDCEx(hwnd, (HRGN)wParam, DCX_WINDOW|DCX_INTERSECTRGN);
// Paint into this DC
ReleaseDC(hwnd, hdc);
}
bbol 2004-08-12
  • 打赏
  • 举报
回复
转的高手的代码:

厄,其实画这个不是很难。一般的立体控件,只要你想像左上角有光源照下来就可以了。比如button,左上两个边使用亮色,右下两个边用暗色,这样画出来的一个平面矩形框看起来就是立体的。
至于标题栏,给你个例子吧,在这个例子里,我在标题栏上多添了一个按钮。
void CResizableMiniDockFrameWnd::OnNcPaint()
{
CWindowDC dc(this);

CRect rectClient(0,0,0,0);
GetClientRect(rectClient);

CRect rectWindow(0,0,0,0);
GetWindowRect(rectWindow);
ScreenToClient(rectWindow);

rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
dc.ExcludeClipRect(rectClient);
rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);

CSize sizeFrame(GetSystemMetrics(SM_CXFRAME), GetSystemMetrics(SM_CYFRAME));
CSize sizeBorder(GetSystemMetrics(SM_CXBORDER), GetSystemMetrics(SM_CYBORDER));
CSize sizeSmIcon(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
CMemoryDC dcMemory;
dcMemory.Create(&dc, rectWindow);

//边框
dcMemory.DrawEdge(&rectWindow, EDGE_RAISED, BF_RECT);
CRect rectCaption(rectWindow);
rectCaption.DeflateRect(sizeFrame.cx, sizeFrame.cy);
rectCaption.bottom = rectCaption.top + sizeSmIcon.cy - sizeBorder.cy;

//标题栏底色
BOOL bGradient = FALSE;
::SystemParametersInfo(SPI_GETGRADIENTCAPTIONS, 0, &bGradient, 0);
COLORREF clrLeft = ::GetSysColor(COLOR_ACTIVECAPTION);
COLORREF clrRight = ::GetSysColor(COLOR_GRADIENTACTIVECAPTION);
COLORREF clrGray = ::GetSysColor(COLOR_INACTIVECAPTION);
if(bGradient && m_bActive)
{
int nShift = 6;
int nSteps = 1 << nShift;
for(int i=0; i<nSteps; ++i)
{
int nRed = (GetRValue(clrLeft) * (nSteps - i) + GetRValue(clrRight) * i) >> nShift;
int nGreen = (GetGValue(clrLeft) * (nSteps - i) + GetGValue(clrRight) * i) >> nShift;
int nBlue = (GetBValue(clrLeft) * (nSteps - i) + GetBValue(clrRight) * i) >> nShift;

CRect rect(rectCaption);
rect.left = rectCaption.left +((i * rectCaption.Width()) >> nShift);
rect.right = rectCaption.left +(((i + 1) * rectCaption.Width()) >> nShift);

if(rect.Width() > 0)
{
dcMemory.FillSolidRect(rect, RGB(nRed, nGreen, nBlue));
}
}
}
else
{
dcMemory.FillSolidRect(&rectCaption, m_bActive ? clrLeft:clrGray);
}
LOGFONT fontCaption = {0};
::SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &fontCaption, 0);
fontCaption.lfWeight = FW_BOLD;
HFONT hCaptionFont = CreateFontIndirect(&fontCaption);
HGDIOBJ hOldFont = dcMemory.SelectObject(hCaptionFont);
dcMemory.SetTextColor(::GetSysColor(COLOR_CAPTIONTEXT));
dcMemory.SetBkMode(TRANSPARENT);

CString strCaption(_T(""));
GetWindowText(strCaption);
if(strCaption.IsEmpty())
{
strCaption = _T("ToolBar");
}
CSize sizeText(dcMemory.GetTextExtent(strCaption));

//输出文本(右端扣除Button所占的位置)
CRect rectText(rectCaption);
rectText.left += sizeBorder.cx * 2;
rectText.right -= sizeSmIcon.cx * 2 + sizeBorder.cx;
dcMemory.DrawText(strCaption, &rectText, DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS);
dcMemory.SelectObject(hOldFont);
DeleteObject(hCaptionFont);

DrawButton(&dcMemory, rectCaption);

dcMemory.Flush();
}


void CResizableMiniDockFrameWnd::DrawButton(CDC* pDC, CRect& rectCaption)
{
ASSERT_VALID(pDC);

CRect rectClose = rectCaption;
rectClose.top += 1;
rectClose.bottom = rectClose.top + 12;
rectClose.right -= 1;
rectClose.left = rectClose.right - 12;

CRect rectExpand = rectClose;
rectExpand.OffsetRect(-14,0);

//background
pDC->FillSolidRect(&rectClose, afxData.clrBtnFace);
pDC->FillSolidRect(&rectClose, afxData.clrBtnFace);

//close button
DrawFrameControl(pDC->GetSafeHdc(), rectClose, DFC_BUTTON,
DFCS_BUTTONPUSH|(m_btnClose.IsPushed()?DFCS_PUSHED:0));
CPoint pt = rectClose.TopLeft();
pt.Offset(m_btnClose.IsPushed()?2:1, m_btnClose.IsPushed()?2:1);
m_ImageList.Draw(pDC, m_btnClose.GetIcon(), pt, ILD_TRANSPARENT);

//expand button
DrawFrameControl(pDC->GetSafeHdc(), rectExpand, DFC_BUTTON,
DFCS_BUTTONPUSH|(m_btnExpand.IsPushed()?DFCS_PUSHED:0));
pt = rectExpand.TopLeft();
pt.Offset(1,1);
m_ImageList.Draw(pDC, m_btnExpand.GetIcon(), pt, ILD_TRANSPARENT);
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧