zoukankan      html  css  js  c++  java
  • MFC DrawText如何使多行文字居中显示 Demo

    最近忙于一个小项目,用MFC做一个对话框:1.显示自定义文字 2.多行文本居中显示 3.文字颜色支持自定义 4.窗口透明度支持自定义 5.窗口自动隐藏

    一、新建一个基于对对话框的MFC程序

     二、添加子窗口来动态显示文本,对应的类是CDlgShowMsg,子窗口的属性 Title bar 属性设为FALSE,不显示标题栏。

      

     

     三、CDlgShowMsg类的实现

        .h实现

    class CDlgShowMsg : public CDialog
    {
        DECLARE_DYNAMIC(CDlgShowMsg)
    
    public:
        CDlgShowMsg(CWnd* pParent = NULL);   // 标准构造函数
        virtual ~CDlgShowMsg();
    
    // 对话框数据
    #ifdef AFX_DESIGN_TIME
        enum { IDD = IDD_DLGDRAW };
    #endif
    
    protected:
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持
        virtual BOOL OnInitDialog();
        DECLARE_MESSAGE_MAP()
    public:
        afx_msg void OnPaint();
    public:
        void ShowMsgInfo(CString strMsg);
        void SetWindowWidthAndHeight(int iWidth,int iHeight);//窗口的宽和高
        void SetWindowTransparency(double dAlpha);//(0~1.0) 窗口透明度
        void SetPosWindow(int iXPos,int iYPos,int iWidth,int iHeigth);//窗口的位置和大小
        void SetTextColor(int iRed,int iGreen, int iBlue);//可以修改文本颜色
    private:
        CString m_strMsg;//显示文本
        int m_iWidth;//窗口宽
        int m_iHeight;//窗口高
        int m_iRed;//RGB的R
        int m_iGreen;//RGB的G
        int m_iBlue;//RGB的B
        UINT m_uTimeID;//定时器ID
    public:
        afx_msg void OnTimer(UINT_PTR nIDEvent);
    };

    .cpp实现

    CDlgShowMsg::CDlgShowMsg(CWnd* pParent /*=NULL*/)
        : CDialog(IDD_DLGDRAW, pParent)
        , m_iWidth(200)
        , m_iHeight(100)
        , m_iRed(255)
        , m_iGreen(255)
        , m_iBlue(255)
    {
    
    }
    
    CDlgShowMsg::~CDlgShowMsg()
    {
    
    }
    
    void CDlgShowMsg::DoDataExchange(CDataExchange* pDX)
    {
        CDialog::DoDataExchange(pDX);
    }
    
    
    BOOL CDlgShowMsg::OnInitDialog()
    {
        CDialog::OnInitDialog();
    
        return TRUE;
    }
    
    BEGIN_MESSAGE_MAP(CDlgShowMsg, CDialog)
        ON_WM_PAINT()
        ON_WM_TIMER()
    END_MESSAGE_MAP()
    
    
    // CDlgShowMsg 消息处理程序
    
    
    void CDlgShowMsg::OnPaint()
    {
        CPaintDC dc(this); // device context for painting
                           // TODO: 在此处添加消息处理程序代码
        CBrush brush;
        CRect rc;
        GetWindowRect(&rc); //获得窗口矩形
        brush.CreateSolidBrush(RGB(255, 255, 255));
        dc.SelectObject(brush);
        dc.PatBlt(0, 0, rc.Width(), rc.Height(), PATCOPY);
    
        CRect rect;
    
        GetClientRect(&rect);
        CDC* pDc = GetDC();
    
        CFont new_font;
    
        //创建字体宋体格式  100为字高
        VERIFY(new_font.CreatePointFont(250, _T("宋体"), pDc));
    
        //选择该字体进入PDC
        CFont* default_font = pDc->SelectObject(&new_font);
    
        //设置字体背景为透明
        pDc->SetBkMode(TRANSPARENT);
    
        //设置字体颜色
        pDc->SetTextColor(RGB(m_iRed,m_iGreen,m_iBlue));
    
        CRect boundary(0,0,m_iWidth,m_iHeight);//设置文本要显示矩形区域
        CRect rect1(0,0,boundary.Width(),boundary.Height());//设置文字显示临时区域
        int height = pDc->DrawText(m_strMsg, rect, DT_CALCRECT | DT_CENTER | DT_EDITCONTROL | DT_WORDBREAK);//计算完成后将原来的区域赋回rect
        rect1 = boundary;
        if (boundary.Height()>height)
        {
            rect1.top += (boundary.Height() - height)/2;//计算空白高度的一半
        }
    
    
        //显示文本,居中显示
        //pDc->DrawText(m_strMsg, rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);//单行 居中显示
        pDc->DrawText(m_strMsg, rect1, DT_VCENTER |  DT_CENTER |DT_EDITCONTROL | DT_WORDBREAK);//多行 居中显示  垂直巨宗  左右居中  自动换行
    
        //恢复PDC的缺省字体
        pDc->SelectObject(default_font);
    
        //释放font对象
        new_font.DeleteObject();
                           // 不为绘图消息调用 CDialog::OnPaint()
    }
    
    void CDlgShowMsg::ShowMsgInfo(CString strMsg)
    {
        m_strMsg = strMsg;
        ShowWindow(SW_SHOW);
        m_uTimeID = SetTimer(1, 5000, NULL);
        CRect rc;
        GetClientRect(&rc);
        InvalidateRect(rc, FALSE);
    }
    
    
    void CDlgShowMsg::SetWindowWidthAndHeight(int iWidth, int iHeight)
    {
        m_iWidth = iWidth;
        m_iHeight = iHeight;
    }
    
    void CDlgShowMsg::SetWindowTransparency(double dAlpha)
    {
        // 设置分层属性   
        SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE) | WS_EX_LAYERED);
        SetLayeredWindowAttributes( 0, 255* dAlpha , LWA_ALPHA);
    }
    
    void CDlgShowMsg::SetPosWindow(int iXPos, int iYPos, int iWidth, int iHeigth)
    {
        SetWindowPos(NULL, iXPos, iYPos, iWidth, iHeigth, SWP_SHOWWINDOW);
    }
    
    void CDlgShowMsg::SetTextColor(int iRed, int iGreen, int iBlue)
    {
        m_iRed = iRed;
        m_iGreen = iGreen;
        m_iBlue = iBlue;
    }
    
    void CDlgShowMsg::OnTimer(UINT_PTR nIDEvent)
    {
        // TODO: 在此添加消息处理程序代码和/或调用默认值
        KillTimer(m_uTimeID);
        ShowWindow(SW_HIDE);
        CDialog::OnTimer(nIDEvent);
    }

    四、使用

    在CtestDrawTextDlg中定义一个私有的 CDlgShowMsg* m_pShowMsg变量,在OnInitDialog函数中创建子窗口

    if (m_pShowMsg == NULL)
    {
        m_pShowMsg = new CDlgShowMsg;
        m_pShowMsg->Create(IDD_DLGDRAW, this);
        m_pShowMsg->ShowWindow(SW_HIDE);//先隐藏
    }
    void CtestDrawTextDlg::OnBnTestClicked()
    {
        // TODO: 在此添加控件通知处理程序代码
    
        if (m_pShowMsg)
        {
            m_pShowMsg->SetTextColor(255, 255, 0);//黄色的文本颜色
            m_pShowMsg->ShowMsgInfo(_T("白雪公和七个小矮人哈哈哈哈哈1111"));
            m_pShowMsg->SetWindowTransparency(0.9);//窗口透明度
            m_pShowMsg->SetPosWindow(100, 150, 400, 100);//窗口位置和大小
            m_pShowMsg->SetWindowWidthAndHeight(400, 100);//窗宽和高
        }
    }
  • 相关阅读:
    windows 系列机器查看tcp 配置
    test windows live writer
    win7 mysql 启动 问题
    用enum枚举量做下标初始化数组
    二层VXLAN静态配置
    pip install scrapy报错原文
    python练习2
    pycharm字体大小设置快捷键的方法
    python练习1
    centos 7下修改IP地址为静态
  • 原文地址:https://www.cnblogs.com/chechen/p/14027549.html
Copyright © 2011-2022 走看看