The study of Programming Windows with MFC--Ip Address control and Date control

本文详细介绍了几种常用控件的设置与获取方法,包括IP地址控件的字段范围设定及值的读取、热键控件的设置与获取、月历控件的日期选择与范围设置以及日期时间选择控件的时间设定与获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Ip Address Control

   CIPAddressCtrl  m_wndIPAddress;

   

// In CMyDialog's class declaration
BYTE m_nField1, m_nField2, m_nField3, m_nField4;
   
BOOL CMyDialog::OnInitDialog ()
{
    CDialog::OnInitDialog ();
    m_wndIPAddress.SetAddress (m_nField1, m_nField2,
        m_nField3, m_nField4);
    return TRUE;
}

void CMyDialog::OnOK ()
{
    m_wndIPAddress.GetAddress (m_nField1, m_nField2,
        m_nField3, m_nField4);
    CDialog::OnOK ();
}

 

m_wndIPAddress.SetFieldRange (0,  10, 100); // Field 1
m_wndIPAddress.SetFieldRange (3, 100, 155); // Field 4

 

2.HotKey

CHotKeyCtrl  m_wndHotkey;

m_wndHotkey.SetHotKey (_T (`P'), HOTKEYF_CONTROL ¦ HOTKEYF_ALT);


WORD wKeyCode, wModifiers;
m_wndHotkey.GetHotKey (wKeyCode, wModifiers);

 

m_wndHotkey.SetRules (HKCOMB_A ¦ HKCOMB_CA ¦ HKCOMB_SA ¦ HKCOMB_SCA, 0);

 

2.Month Calendar Controls

CMonthCalCtrl m_wndCal;

m_wndCal.SetCurSel (CTime (1999, 9, 30, 0, 0, 0));

CTime date;
m_wndCal.GetCurSel (date);


Contrary to what the documentation says, a calendar control sometimes returns random data in the hours, minutes, seconds, and milliseconds fields of the SYSTEMTIME structure it uses to divulge dates in response to MCM_GETCURSEL messages. Because CTime factors the time into the dates it obtains from SYSTEMTIME structures, incrementing the day by 1, for example, if hours equals 25, CTime objects initialized by CMonthCalCtrl::GetCurSel can't be trusted. The solution is to retrieve the current date by sending the control an MCM_GETCURSEL message and zeroing the time fields of the SYSTEMTIME structure before converting it into a CTime, as demonstrated here:

SYSTEMTIME st;
m_wndCal.SendMessage (MCM_GETCURSEL, 0, (LPARAM) &st);
st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
CTime date (st);
m_wndCal.SetMaxSelCount (14);
m_wndCal.SetSelRange (CTime (1999, 9, 30, 0, 0, 0),(1999, 9, 30, 0, 0, 0));

 

 

CTime dateStart, dateEnd;
m_wndCal.GetSelRange (dateStart, dateEnd);

This example sets dateStart equal to the selection's start date and dateEnd to the end date. If just one day is selected, dateStart will equal dateEnd. Fortunately, GetSelRange doesn't suffer from the randomness problems that GetCurSel does.

 

4.Date-Time Picker Controls  (DTP controls)

CDateTimeCtrl m_wndDTP;

// In CMyDialog's class declaration
CTime m_time;
    
BOOL CMyDialog::OnInitDialog ()
{
    CDialog::OnInitDialog ();
    m_wndDTP.SetTime (&m_time);
    return TRUE;
}

void CMyDialog::OnOK ()
{
    m_wndDTP.GetTime (m_time);
    CDialog::OnOK ();
}
DDX_DateTimeCtrl (pDX, IDC_DTP, m_time);
m_wndDTP.SetFormat (_T ("H/':/'mm/':/'ss"));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风水月

从心底相信自己是成功的第一步

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值