- 螢幕佈局
成員變數準備class CDemoDlg : public CDialog
{
public:
CDemoDlg(CWnd* pParent = NULL);
CRect m_example;
CString brushs[4];
int brush_datas[4];
int brush_type;
enum { IDD = IDD_DEMO_DIALOG };
CListBox m_list;
- 為元件修改ID
元件 |
ID |
列表框 |
IDC_BRUSH_LIST |
組合框 |
IDC_STATIC_EXAMPLE |
- 視窗初始化函式
BOOL CDemoDlg::OnInitDialog()
{
...
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;
}
- 列表框選擇函式
void CDemoDlg::OnSelchangeBrushList()
{
int index = m_list.GetCurSel();
if (index != LB_ERR) {
int type = (int)m_list.GetItemData(index);
brush_type = type;
}
InvalidateRect(&m_example);
}
- 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();
}
}
執行結果