MFC 使用静态文本绘制边框

MFC使用static静态文本控件绘制边框

编译环境 VS2010

1、创建MFC工程,选择基于对话框,为了程序方便,先去除Unicode库

在这里插入图片描述

2、在界面拖出4个静态文本控件,分别用于上、下、左、右四条线

在这里插入图片描述

3、添加CMyStatic类,基于MFC窗体而不是C++,选择基类CStatic

在这里插入图片描述

4、编写CMyStatic类

CMyStatic.h

#pragma once


// CMyStatic

//字体对齐
#define STATIC_ALIGN_LEFT  0
#define STATIC_ALIGN_CENTE 1
#define STATIC_ALIGN_RIGHT 2

class CMyStatic : public CStatic
{
	DECLARE_DYNAMIC(CMyStatic)

public:
	CMyStatic();
	virtual ~CMyStatic();
public:
	afx_msg void OnPaint();

private:
	BOOL m_bBkTransparent;	//透明背景
	BOOL m_bFirst;			
	COLORREF m_BkColor;		//背景颜色
	CString m_strText;		//文本信息
	unsigned int m_uiAlign; //对齐方式

public:
	void SetText(char *ascText);
	void SetBkColor(COLORREF colorBk);
	void SetBkTransparent(BOOL bBkTransparent);

protected:
	DECLARE_MESSAGE_MAP()
};

CMyStatic.cpp

// MyStatic.cpp : 实现文件
//

#include "stdafx.h"
#include "Videoframe_1.h"
#include "MyStatic.h"


// CMyStatic

IMPLEMENT_DYNAMIC(CMyStatic, CStatic)

CMyStatic::CMyStatic()
{
	m_strText = _T("");
	m_bBkTransparent = TRUE;//默认透明
	m_BkColor = RGB(255, 255, 255);//白色背景
	m_uiAlign = STATIC_ALIGN_LEFT;
	m_bFirst = FALSE;;
}

CMyStatic::~CMyStatic()
{
}


BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
	ON_WM_PAINT()
END_MESSAGE_MAP()



// CMyStatic 消息处理程序

void CMyStatic::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: 在此处添加消息处理程序代码
	// 不为绘图消息调用 CStatic::OnPaint()

	// 取得位置
	CRect rect;
	GetClientRect(rect);

	//字体
	CFont *pDlgFont = GetFont();
	CFont *pCurFont = dc.SelectObject(pDlgFont);

	//设置背景颜色
	if(m_bBkTransparent)
	{
		dc.SetBkMode(TRANSPARENT);
	}
	else
	{        
		dc.FillSolidRect(rect, m_BkColor);
	}

	//判断输出文字
	if(m_strText.GetLength() == 0)
	{
		GetWindowText(m_strText);
	}
	//输出文字
	switch(m_uiAlign)
	{
	case STATIC_ALIGN_LEFT:
		dc.DrawText(m_strText, rect, DT_LEFT | DT_SINGLELINE | DT_VCENTER);
		break;

	case STATIC_ALIGN_CENTE:
		dc.DrawText(m_strText, rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
		break;

	case STATIC_ALIGN_RIGHT:
		dc.DrawText(m_strText, rect, DT_RIGHT | DT_SINGLELINE | DT_VCENTER);
		break;

	default:
		dc.DrawText(m_strText, rect, DT_LEFT | DT_SINGLELINE | DT_VCENTER);
		break;
	}
	// 清除字体
	dc.SelectObject(pCurFont);
}

//设置文本内容
void CMyStatic::SetText(char *ascText)
{
	CString strText(ascText);
	m_strText = strText;
	m_bFirst = TRUE;
	Invalidate();
}

//设置背景颜色
void CMyStatic::SetBkColor(COLORREF colorBk)
{
	m_BkColor = colorBk;
	m_bFirst = TRUE;
	Invalidate();
}
//设置背景透明
void CMyStatic::SetBkTransparent(BOOL bBkTransparent)
{
	m_bBkTransparent = bBkTransparent;
}
5、在窗体界面创建Static静态文本关联变量(注:修改Static的控件ID)

在这里插入图片描述

	CMyStatic m_staLineBottom;
	CMyStatic m_staLineLeft;
	CMyStatic m_staLineRight;
	CMyStatic m_staLineTop;
6、在初始化窗体函数InitDialog,初始化值
BOOL CVideoframe_1Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码


	m_staLineLeft.SetBkTransparent(FALSE);
	m_staLineRight.SetBkTransparent(FALSE);
	m_staLineBottom.SetBkTransparent(FALSE);
	m_staLineTop.SetBkTransparent(FALSE);

	m_staLineLeft.SetBkColor(RGB(128, 0, 0));
	m_staLineRight.SetBkColor(RGB(128, 0, 0));
	m_staLineTop.SetBkColor(RGB(128, 0, 0));
	m_staLineBottom.SetBkColor(RGB(128, 0, 0));

	m_staLineLeft.SetText("");
	m_staLineRight.SetText("");
	m_staLineTop.SetText("");
	m_staLineBottom.SetText("");
	//在类中创建此函数
	MoveFrame();
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CVideoframe_1Dlg::MoveFrame()
{
	//横杠	大小
	_SI siPenWidth = 2;

	//先判断是第几行第几列
	_UI uiWndIndex = 0;		//窗口索引
	_UI uiColIndex = 0;		//纵向
	_UI uiRowIndex = 0;		//横向
	_SI siWndWidth = 160;	//窗体宽
	_SI siWndHeight = 90;	//窗体高度
	_SI siWndLeft = 100;	//X坐标
	_SI siWndTop = 100;		//Y坐标

	m_staLineLeft.MoveWindow(siWndLeft - siPenWidth, 
		siWndTop,  
		siPenWidth, 
		siWndHeight + (siPenWidth*2));

	m_staLineTop.MoveWindow(siWndLeft - siPenWidth,
		siWndTop - siPenWidth,
		siWndWidth + siPenWidth,
		siPenWidth);

	m_staLineRight.MoveWindow(siWndLeft + siWndWidth - siPenWidth ,
		siWndTop,
		siPenWidth,
		siWndHeight + (siPenWidth * 2));

	m_staLineBottom.MoveWindow(siWndLeft - siPenWidth,
		siWndTop + siWndHeight + (siPenWidth*2),
		siWndWidth + siPenWidth,
		siPenWidth);

}
7、最终效果

在这里插入图片描述

源代码在码云Gitee:
https://gitee.com/hi_feng/Videoframe_1

猜你喜欢

转载自blog.csdn.net/qq_36351159/article/details/107676153