代码里调用了3次roundrect,结果当点击按钮有焦点的时候好像就有2个矩形, 应该有3个才对
// A simple function for drawing the rounded button.
void DrawRoundedButton( HDC dc, // Device context to draw on.
RECT rect, // The bounding box of the button.
int pressed, // 1 if the button is pressed.
int focused ) // 1 if the button has the focus.
{
// The interior of the button is whatever color the system tells us.
HBRUSH brush = GetSysColorBrush( COLOR_BTNFACE );
HBRUSH oldBrush = (HBRUSH)SelectObject( dc, brush ); HPEN pen, oldPen; // If this is a default button or focused, then it needs a black border.
if( focused )
{
pen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
oldPen = (HPEN)SelectObject( dc, pen );
RoundRect( dc, rect.left, rect.top, rect.right, rect.bottom, 10, 10 );
SelectObject( dc, oldPen );
DeleteObject( pen );
} // Otherwise, clear the area completely.
else
{
pen = CreatePen( PS_NULL, 1, 0 );
oldPen = (HPEN)SelectObject( dc, pen );
FillRect( dc, &rect, brush );
SelectObject( dc, oldPen );
DeleteObject( pen );
} // Draw the top and left.
if( pressed )
{
pen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
} else
{
pen = CreatePen( PS_SOLID, 1, RGB( 255, 255, 255 ) );
} oldPen = (HPEN)SelectObject( dc, pen );
RoundRect( dc, rect.left + 2, rect.top + 2, rect.right - 2, rect.bottom - 2, 7, 7 );
SelectObject( dc, oldPen );
DeleteObject( pen ); // Draw the bottom and right.
if( pressed )
{
pen = CreatePen( PS_SOLID, 1, RGB( 255, 255, 255 ) );
} else
{
pen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
} SelectObject( dc, pen );
RoundRect( dc, rect.left + 4, rect.top + 4, rect.right - 4, rect.bottom - 4, 7, 7 );
DeleteObject( pen );
// Put the original brush back.
SelectObject( dc, oldBrush );
SelectObject( dc, oldPen );
}
// A simple function for drawing the rounded button.
void DrawRoundedButton( HDC dc, // Device context to draw on.
RECT rect, // The bounding box of the button.
int pressed, // 1 if the button is pressed.
int focused ) // 1 if the button has the focus.
{
// The interior of the button is whatever color the system tells us.
HBRUSH brush = GetSysColorBrush( COLOR_BTNFACE );
HBRUSH oldBrush = (HBRUSH)SelectObject( dc, brush ); HPEN pen, oldPen; // If this is a default button or focused, then it needs a black border.
if( focused )
{
pen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
oldPen = (HPEN)SelectObject( dc, pen );
RoundRect( dc, rect.left, rect.top, rect.right, rect.bottom, 10, 10 );
SelectObject( dc, oldPen );
DeleteObject( pen );
} // Otherwise, clear the area completely.
else
{
pen = CreatePen( PS_NULL, 1, 0 );
oldPen = (HPEN)SelectObject( dc, pen );
FillRect( dc, &rect, brush );
SelectObject( dc, oldPen );
DeleteObject( pen );
} // Draw the top and left.
if( pressed )
{
pen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
} else
{
pen = CreatePen( PS_SOLID, 1, RGB( 255, 255, 255 ) );
} oldPen = (HPEN)SelectObject( dc, pen );
RoundRect( dc, rect.left + 2, rect.top + 2, rect.right - 2, rect.bottom - 2, 7, 7 );
SelectObject( dc, oldPen );
DeleteObject( pen ); // Draw the bottom and right.
if( pressed )
{
pen = CreatePen( PS_SOLID, 1, RGB( 255, 255, 255 ) );
} else
{
pen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
} SelectObject( dc, pen );
RoundRect( dc, rect.left + 4, rect.top + 4, rect.right - 4, rect.bottom - 4, 7, 7 );
DeleteObject( pen );
// Put the original brush back.
SelectObject( dc, oldBrush );
SelectObject( dc, oldPen );
}
{
// Draw the button.
DrawRoundedButton( item->hDC,
item->rcItem,
item->itemState & ODS_SELECTED,
item->itemState & ODS_FOCUS );
最外边 要 windowDC 吧?
前辈,你搞错了, 我也糊涂了,应该是画出来了, 我特意修改颜色,而且修改 roundrect的参数成功了。
// A simple function for drawing the rounded button.
void DrawRoundedButton( HDC dc, // Device context to draw on.
RECT rect, // The bounding box of the button.
int pressed, // 1 if the button is pressed.
int focused ) // 1 if the button has the focus.
{
// The interior of the button is whatever color the system tells us.
HBRUSH brush = GetSysColorBrush( COLOR_BTNFACE );
HBRUSH oldBrush = (HBRUSH)SelectObject( dc, brush ); HPEN pen, oldPen; // If this is a default button or focused, then it needs a black border.
if( focused )
{
// pen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
pen = CreatePen( PS_SOLID, 1, RGB( 255, 0, 0 ) ); //red oldPen = (HPEN)SelectObject( dc, pen );
RoundRect( dc, rect.left, rect.top, rect.right, rect.bottom, 10, 10 );
SelectObject( dc, oldPen );
DeleteObject( pen );
} // Otherwise, clear the area completely.
else
{
/*pen = CreatePen( PS_NULL, 1, 0 );*/
pen = CreatePen( PS_SOLID, 1, RGB( 255, 255, 0 ) ); //yellow
oldPen = (HPEN)SelectObject( dc, pen );
FillRect( dc, &rect, brush );
SelectObject( dc, oldPen );
DeleteObject( pen );
} // Draw the top and left.
if( pressed )
{
pen = CreatePen( PS_SOLID, 1, RGB( 65, 105, 255 ) ); //品蓝
} else
{
pen = CreatePen( PS_SOLID, 1, RGB( 0, 255, 255 ) ); //青色
} //第二次画
oldPen = (HPEN)SelectObject( dc, pen );
RoundRect( dc, rect.left + 3, rect.top + 3, rect.right - 3, rect.bottom - 3, 7, 7 );
SelectObject( dc, oldPen );
DeleteObject( pen ); // Draw the bottom and right.
if( pressed )
{
pen = CreatePen( PS_SOLID, 1, RGB( 160, 32, 240 ) ); //紫色
} else
{
pen = CreatePen( PS_SOLID, 1, RGB( 255, 127, 80 ) ); //珊瑚
} //第三次画
SelectObject( dc, pen );
RoundRect( dc, rect.left + 6, rect.top + 6, rect.right - 6, rect.bottom - 6, 7, 7 );
DeleteObject( pen );
// Put the original brush back.
SelectObject( dc, oldBrush );
SelectObject( dc, oldPen );
}
void DrawRoundedButton( HDC dc, // Device context to draw on.
RECT rect, // The bounding box of the button.
int pressed, // 1 if the button is pressed.
int focused ) // 1 if the button has the focus.
{
// The interior of the button is whatever color the system tells us.
HBRUSH brush = GetSysColorBrush( COLOR_BTNFACE );
HBRUSH oldBrush = (HBRUSH)SelectObject( dc, brush ); HPEN pen, oldPen; // If this is a default button or focused, then it needs a black border.
if( focused )
{
// pen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
pen = CreatePen( PS_SOLID, 1, RGB( 255, 0, 0 ) ); //red oldPen = (HPEN)SelectObject( dc, pen );
// 这是第一次RoundRect,但是必须要focused为真,也就是按钮当前是FOCUS状态的,才会RoundRect,所以第一次的RoundRect没有画就是因为按钮根本就没有获得焦点,就没有进入到这个地方来。
RoundRect( dc, rect.left, rect.top, rect.right, rect.bottom, 10, 10 );
SelectObject( dc, oldPen );
DeleteObject( pen );
} // Otherwise, clear the area completely.
else
{
/*pen = CreatePen( PS_NULL, 1, 0 );*/
pen = CreatePen( PS_SOLID, 1, RGB( 255, 255, 0 ) ); //yellow
oldPen = (HPEN)SelectObject( dc, pen );
FillRect( dc, &rect, brush );
SelectObject( dc, oldPen );
DeleteObject( pen );
} // Draw the top and left.
if( pressed )
{
pen = CreatePen( PS_SOLID, 1, RGB( 65, 105, 255 ) ); //品蓝
} else
{
pen = CreatePen( PS_SOLID, 1, RGB( 0, 255, 255 ) ); //青色
} //第二次画
oldPen = (HPEN)SelectObject( dc, pen );
RoundRect( dc, rect.left + 3, rect.top + 3, rect.right - 3, rect.bottom - 3, 7, 7 );
SelectObject( dc, oldPen );
DeleteObject( pen ); // Draw the bottom and right.
if( pressed )
{
pen = CreatePen( PS_SOLID, 1, RGB( 160, 32, 240 ) ); //紫色
} else
{
pen = CreatePen( PS_SOLID, 1, RGB( 255, 127, 80 ) ); //珊瑚
} //第三次画
SelectObject( dc, pen );
RoundRect( dc, rect.left + 6, rect.top + 6, rect.right - 6, rect.bottom - 6, 7, 7 );
DeleteObject( pen );
// Put the original brush back.
SelectObject( dc, oldBrush );
SelectObject( dc, oldPen );
}
给按钮绘制边框,必须 修改其再付窗口中的区域大小,比如:rect2.left=point1.x;
rect2.top=point1.y;
rect2.right=point2.x;
rect2.bottom=point2.y; //rect2.left=point1.x-5;
//rect2.top=point1.y-5;
//rect2.right=point2.x+5;
//rect2.bottom=point2.y+5; FrameRect(hdc,&rect2,(HBRUSH)GetStockObject(BLACK_BRUSH));被我注释掉的区域是可以的,没注释掉的是不行的。说白了就是通过一种假象,让人看起来有客户区域
如果是在 wm_ctlcolorbtn里可以直接使用,不需要修正区域大小。
这是windows的迷惑人的地方。非常古怪,而且毫无规律可言。
wm_paint里修改区域绘制的, 其实是可客户区域中绘制的,所以看起来成功而来。wm_ctlcolor绘制边框,估计也是假象。父窗口的
wm_drawitem能够绘制的。