MFC编程(六)

画刷更新

  1. 屏幕布局
    MFC编程(六)
    成员变量准备
    class CDemoDlg : public CDialog
    {
    // Construction
    public:
     CDemoDlg(CWnd* pParent = NULL);    // standard constructor
     CRect m_example;
     CString brushs[4];
     int brush_datas[4];
     int brush_type;
    // Dialog Data
     //{{AFX_DATA(CDemoDlg)
     enum { IDD = IDD_DEMO_DIALOG };
     CListBox    m_list;
  2. 为组件修改ID
    组件 ID
    列表框 IDC_BRUSH_LIST
    组合框 IDC_STATIC_EXAMPLE
  3. 窗口初始化函数
    BOOL CDemoDlg::OnInitDialog()
    {
     ...
     // TODO: Add extra initialization here
     brushs[0] = "水平线";
     brush_datas[0] = HS_HORIZONTAL;
     brushs[1] = "竖直线";
     brush_datas[1] = HS_VERTICAL;
     brushs[2] = "向下斜线";
     brush_datas[2] = HS_DIAGCROSS;
     brushs[3] = "十字线";
     brush_datas[3] = HS_CROSS;
     // 添加
     for (int i = 0; i < 4; i++) {
         int index = m_list.AddString(brushs[i]);
         m_list.SetItemData(index, brush_datas[i]);
         if (i == 3) {
             m_list.SetCurSel(index);
             brush_type = m_list.GetItemData(m_list.GetCurSel());
         }
     }
     return TRUE;  // return TRUE  unless you set the focus to a control
    }
  4. 列表框选择函数
    void CDemoDlg::OnSelchangeBrushList() 
    {
     // TODO: Add your control notification handler code here
     int index = m_list.GetCurSel();
     if (index != LB_ERR) {
         int type = (int)m_list.GetItemData(index);
         brush_type = type;
     }
     InvalidateRect(&m_example);
    }
  5. Paint函数
    void CDemoDlg::OnPaint() 
    {
     if (IsIconic())
     {
         ...
     }
     else
     {
         COLORREF Color = RGB(255, 20, 30);
         GetDlgItem(IDC_STATIC_EXAMPLE)->GetWindowRect(&m_example);
         ScreenToClient(&m_example);
         int border = (m_example.right - m_example.left) / 6;
         m_example.InflateRect(-border, -30);
         CBrush brush(brush_type, Color);
         CPaintDC dc(this);
         dc.FillRect(&m_example, &brush);
         CDialog::OnPaint();
     }
    }
    运行结果
    MFC编程(六)
MFC
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 1
Dennis_Ritchie

不建议学这个东西, :joy:有点老咯

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!