Mfc-managing-updown-control
提供:Dev Guides
MFC-アップダウンコントロールの管理
ステップ1 *-次のスナップショットに示すような設定でスピン制御用の制御変数 *m_spinControl を追加します。
- ステップ2 *-編集コントロールにコントロール変数m_editControlを追加します。
- ステップ3 *-スピンボタンのUDN_DELTAPOSイベントのイベントハンドラーを追加します。
- ステップ4 *-次のコードに示すように、OnInitDialog()を更新します。
BOOL CMFCSpinButtonDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
//Set the icon for this dialog. The framework does this automatically
//when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); //Set big icon
SetIcon(m_hIcon, FALSE); //Set small icon
//TODO: Add extra initialization here
m_spinControl.SetRange(0, 100);
m_spinControl.SetPos(50);
m_editControl.SetWindowText(L"50");
return TRUE;//return TRUE unless you set the focus to a control
}
- ステップ5 *-スピン制御イベントの実装です。
void CMFCSpinButtonDlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult) {
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
//TODO: Add your control notification handler code here
//Declare a pointer to a CSpinButtonCtrl;
CSpinButtonCtrl *Spinner;
//Get a pointer to our spin button
Spinner = reinterpret_cast<CSpinButtonCtrl *>(GetDlgItem(IDC_SPIN1));
//Found out if it is our spin button that sent the message
//This conditional statement appears useless but so what?
if (pNMHDR -> hwndFrom == Spinner -> m_hWnd) {
//Get the current value of the spin button
int CurPos = pNMUpDown→iPos;
//Convert the value to a string
CString str;
str.Format(L"%d", CurPos);
//Display the value into the accompanying edit box
m_editControl.SetWindowText(str);
}
*pResult = 0;
}
- ステップ6 *-上記のコードをコンパイルして実行すると、次の出力が表示されます。