MFC编程(三)

画笔和单选框/复选框

页面布局如下

MFC编程(三)

  • 使用类:
    • CButton:按钮(包括单选和复选按钮)
      // 获取某个单选框按钮是否被选中
      int checked = ((CButton *)GetDlgItem(IDC_RADIO_NAME))->GetCheck();
      // 设置选中或者非选中
      ((CButton *)GetDlgItem(IDC_RADIO_NAME))->SetCheck(TRUE); // 非选中为FALSE
    • COLORREF:颜色
      // 定义
      COLORREF Color = RGB(r int, g int, b int);
  • 使用方法:
    • CWnd::UpdateWindow:更新窗口显示
    • CWnd::ScreenToClient(<CRect>):将矩形区域转化为客户端坐标
    • CWnd::GetDlgItem(IDC_xxx):根据ID获取某个组件
    • CWnd::GetWindowRect(<CRect>):获取当前组件的矩形区域

      定义实例框内的Paint区域

      // 窗口类
      class CDemoDlg : public CDialog
      {
      public:
      CRect m_example;
      }
      // 窗口初始化函数
      BOOL CDemoDlg::OnInitDialog()
      {
      CDialog::OnInitDialog();
      // 获取实例框大小并赋值给 m_ example
      this->GetDlgItem(IDC_SECTION_EXAMPLE)->GetWindowRect(&this->m_example);
      // 将显示的矩形的坐标转化为客户端坐标
      this->ScreenToClient(&this->m_example);
      // 计算并显示矩形区域
      int border = (m_example.right - m_example.left) / 7;
      m_example.InflateRect(-border, -border);
      }

      为空间添加变量
      MFC编程(三)

为单选框和复选框添加事件

// 红色复选框事件
void CDemoDlg::OnCheckRed() 
{
    // TODO: Add your control notification handler code here
    this->m_red = this->IsDlgButtonChecked(IDC_CHECK_RED);
    this->InvalidateRect(&this->m_example);
    this->UpdateWindow();
}
// 绿色复选框事件
void CDemoDlg::OnCheckGreen()
{
    // TODO: Add your control notification handler code here
    this->m_green = this->IsDlgButtonChecked(IDC_CHECK_GREEN);
    this->InvalidateRect(&this->m_example);
    this->UpdateWindow();
}
// 蓝色复选框事件
void CDemoDlg::OnCheckBlue()
{
    // TODO: Add your control notification handler code here
    this->m_blue = this->IsDlgButtonChecked(IDC_CHECK_BLUE);
    this->InvalidateRect(&this->m_example);
    this->UpdateWindow();
}
// 亮色单选框事件
void CDemoDlg::OnRadioLight1()
{
    // TODO: Add your control notification handler code here
    if (this->IsDlgButtonChecked(IDC_RADIO_LIGHT1)) {
        this->m_light = TRUE;
        this->InvalidateRect(&this->m_example);
        this->UpdateWindow();
    }
}
// 暗色单选框事件
void CDemoDlg::OnRadioLight2()
{
    // TODO: Add your control notification handler code here
    if (this->IsDlgButtonChecked(IDC_RADIO_LIGHT2)) {
        this->m_light = FALSE;
        this->InvalidateRect(&this->m_example);
        this->UpdateWindow();
    }
}

窗口绘画事件

void CDemoDlg::OnPaint() 
{
    if (IsIconic())
    {
        ...
    }
    else
    {
        // 绘制矩形区域
        if (this->IsDlgButtonChecked(IDC_RADIO_LIGHT1)) 
            this->m_light = TRUE;
        COLORREF Color = RGB(
            this->m_red ? (this->m_light == TRUE ? 255 : 128) : 0,
            this->m_green ? (this->m_light == TRUE ? 255 : 128) : 0,
            this->m_blue ? (this->m_light == TRUE ? 255 : 128) : 0
        );
        CBrush Brush(Color);
        CPaintDC dc(this);
        dc.FillRect(&this->m_example, &Brush);
        //调用父类方法
        CDialog::OnPaint();
    }
}
MFC
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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