MFC程式設計(六)

calong發表於2020-11-04
  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程式設計(六)
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章