winform GDI+ 画矩形
我想实现沿着一个Form的边框画一个矩形矩形边框宽4px,求大神指导需要注意哪些给代码最好。...
我想实现沿着一个Form的边框画一个矩形 矩形边框宽4px,求大神指导 需要注意哪些 给代码最好。
展开
1个回答
展开全部
这是GDI+一个根据窗口大小尺寸,画宽度为3px的椭圆边框,希望对你有帮助。
void DrawBackgroundImage()
{
EnterCriticalSection(&m_Lock);
// Create canvas.
if (m_bmpCanvas != NULL)
{
delete m_bmpCanvas;
m_bmpCanvas = NULL;
}
if (m_imgBKG.m_pImage == NULL)
{
m_imgBKG.Load_Image(IDB_BKG_BLUE, _T("PNG"));
}
CRect rcClient;
GetClientRect(&rcClient);
Rect rc(m_rcBKG.left, m_rcBKG.top, m_rcBKG.right, m_rcBKG.bottom);
m_bmpCanvas = new Bitmap(m_rcBKG.Width(), m_rcBKG.Height());
Graphics g(m_bmpCanvas);
g.DrawImage(m_imgBKG.m_pImage, 0, 0);
Rect rcBorder(m_rcBKG.left, m_rcBKG.top, m_rcBKG.right - 2, m_rcBKG.bottom -2);
m_pathFrame.CreateRoundRect(rcBorder, 3);
Pen WhitePen(Color(255, 200, 200, 200), 3);
g.DrawPath(&WhitePen, m_pathFrame.m_pPath);
LeaveCriticalSection(&m_Lock);
}
这是创建椭圆边框的代码:
/////////////////////////////////////////////////////////////////////
// Defined class for GDI Plus Path.
class CPathHelper
{
// Constructor and destructor.
public:
CPathHelper() : m_pPath(NULL)
{
}
~CPathHelper()
{
DeleteOldPath();
}
// Data members.
public:
GraphicsPath* m_pPath;
// Operations.
public:
// Delete old path.
void DeleteOldPath()
{
if (m_pPath != NULL)
{
delete m_pPath;
m_pPath = NULL;
}
}
// Create round rect path.
GraphicsPath* CreateRoundRect(Rect rect, int cornerRadius)
{
DeleteOldPath();
m_pPath = new GraphicsPath();
m_pPath->AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
m_pPath->AddLine(rect.X + cornerRadius, rect.Y, rect.GetRight() - cornerRadius * 2, rect.Y);
m_pPath->AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
m_pPath->AddLine(rect.GetRight(), rect.Y + cornerRadius * 2, rect.GetRight(), rect.Y + rect.Height - cornerRadius * 2);
m_pPath->AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
m_pPath->AddLine(rect.GetRight() - cornerRadius * 2, rect.GetBottom(), rect.X + cornerRadius * 2, rect.GetBottom());
m_pPath->AddArc(rect.X, rect.GetBottom() - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
m_pPath->AddLine(rect.X, rect.GetBottom() - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
m_pPath->CloseFigure();
return m_pPath;
}
};
void DrawBackgroundImage()
{
EnterCriticalSection(&m_Lock);
// Create canvas.
if (m_bmpCanvas != NULL)
{
delete m_bmpCanvas;
m_bmpCanvas = NULL;
}
if (m_imgBKG.m_pImage == NULL)
{
m_imgBKG.Load_Image(IDB_BKG_BLUE, _T("PNG"));
}
CRect rcClient;
GetClientRect(&rcClient);
Rect rc(m_rcBKG.left, m_rcBKG.top, m_rcBKG.right, m_rcBKG.bottom);
m_bmpCanvas = new Bitmap(m_rcBKG.Width(), m_rcBKG.Height());
Graphics g(m_bmpCanvas);
g.DrawImage(m_imgBKG.m_pImage, 0, 0);
Rect rcBorder(m_rcBKG.left, m_rcBKG.top, m_rcBKG.right - 2, m_rcBKG.bottom -2);
m_pathFrame.CreateRoundRect(rcBorder, 3);
Pen WhitePen(Color(255, 200, 200, 200), 3);
g.DrawPath(&WhitePen, m_pathFrame.m_pPath);
LeaveCriticalSection(&m_Lock);
}
这是创建椭圆边框的代码:
/////////////////////////////////////////////////////////////////////
// Defined class for GDI Plus Path.
class CPathHelper
{
// Constructor and destructor.
public:
CPathHelper() : m_pPath(NULL)
{
}
~CPathHelper()
{
DeleteOldPath();
}
// Data members.
public:
GraphicsPath* m_pPath;
// Operations.
public:
// Delete old path.
void DeleteOldPath()
{
if (m_pPath != NULL)
{
delete m_pPath;
m_pPath = NULL;
}
}
// Create round rect path.
GraphicsPath* CreateRoundRect(Rect rect, int cornerRadius)
{
DeleteOldPath();
m_pPath = new GraphicsPath();
m_pPath->AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
m_pPath->AddLine(rect.X + cornerRadius, rect.Y, rect.GetRight() - cornerRadius * 2, rect.Y);
m_pPath->AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
m_pPath->AddLine(rect.GetRight(), rect.Y + cornerRadius * 2, rect.GetRight(), rect.Y + rect.Height - cornerRadius * 2);
m_pPath->AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
m_pPath->AddLine(rect.GetRight() - cornerRadius * 2, rect.GetBottom(), rect.X + cornerRadius * 2, rect.GetBottom());
m_pPath->AddArc(rect.X, rect.GetBottom() - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
m_pPath->AddLine(rect.X, rect.GetBottom() - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
m_pPath->CloseFigure();
return m_pPath;
}
};
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询