分类: C/C++
2008-03-18 15:45:46
| 平时在编程时,常想自己编一些适用的控件以利于程序开发,最近编了一个定时关机的控件。现把它提供出来,为和我类似的朋友提供小小的帮助。由于我也是初学乍练,有不当的地方请各位高手给予指出。 1、利用ATL COM Wizard新建一个ATL DLL工程,工程名定为TrueShutDown,其余所有设置保持为默认。 2、利用插入菜单中的"ATL Object Wizard"插入对象,选择复合控件(Composite Control),如图1所示。然后单击"下一步",输入短名称:ShutDownCtrl,然后单击"确定"。 |
![]() |
| 3、我们选择的是复合控件,所以程序中生成了一个对话框模板,在这里我们可以将多个控件组合起来形成一个复合控件。 |
![]() |
| 4、在ClassView中右击CShutDownCtrl,然后选择Add Window Message Handle,在接一下来的对话框中选择IDC_OK,并为它选择消息BN_CLICKED。点击"Add and Edit"退出。 5、OnClickedOk函数的实现如下: |
LRESULT OnClickedOk(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
if(IsOK==0)
{
HWND h=::GetDlgItem(m_hWnd,IDC_HOUR);
::EnableWindow(h,false);
h=::GetDlgItem(m_hWnd,IDC_MINUTE);
::EnableWindow(h,false);
SetDlgItemText(IDC_OK,"重设");
IsOK=1;
}
else
{
HWND h=::GetDlgItem(m_hWnd,IDC_HOUR);
::EnableWindow(h,true);
h=::GetDlgItem(m_hWnd,IDC_MINUTE);
::EnableWindow(h,true);
SetDlgItemText(IDC_OK,"确定");
SetDlgItemText(IDC_STIME,"");
IsOK=0;
}
return 0;
} |
| 其中的IsOK用来判断是否点击了确定按钮。定义如下: protected: int IsOK; 初始值在构造函数CShutDownCtrl()中 IsOK=0; 6。在ClassView中右击CShutDownCtrl,然后选择Add Window Message Handle,在接一下来的对话框中选择WM_TIMER,为它增加句柄(点击"Add Handle"),并为WM_INITDIALOG也增加一个句柄。点击"Add and Edit"退出。 7、往新增加的函数中添加代码: |
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// TODO : Add Code for message handler. Call DefWindowProc if necessary.
::SetTimer(m_hWnd,0,5000,NULL);//用来定时检查关机时间是否已到
::SetTimer(m_hWnd,1,1000,NULL);//用来显示当前时间
return 0;
}
LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
SYSTEMTIME str;
CHAR stall DefWindowProc if necessary.
::SetTimer(m_hWnd,0,5000,NULL);//用来定时检查关机时间是否已到
::SetTimer(m_hWnd,1,1000,NULL);//用来显示当前时间
return 0;
}
LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
SYSTEMTIME str;
CHAR str1[20];
CHAR str_h[10],str_m[10];
::GetLocalTime(&str);//得到当前时间
sprintf(str1,"%2d时%2d分%2d秒",str.wHour,str.wMinute,str.wSecond);
SetDlgItemText(IDC_TIME,str1);
GetDlgItemText(IDC_HOUR,str_h,sizeof(str_h));//得到关机小时
GetDlgItemText(IDC_MINUTE,str_m,sizeof(str_m));//得到关机分钟
if(IsOK==1)GetSTime(str.wHour,str.wMinute,atoi(str_h),atoi(str_m),str.wSecond);
if(wParam==0&&IsOK==1&&str.wHour==atoi(str_h)&&str.wMinute==atoi(str_m))
{
switch(m_nType)
{
case 1:
ExitWindowsEx(EWX_SHUTDOWN,0);
break;
case 2:
ExitWindowsEx(EWX_REBOOT,0);
break;
case 3:
ExitWindowsEx(EWX_FORCE,0);
break;
}
}
return 0;
} |
| 上面代码中的函数GetSTime(str.wHour,str.wMinute,atoi(str_h),atoi(str_m),str.wSecond)是我自定义的,目的是用来计算剩余时间,具体方法见后。 8、为程序添加一个变量,此变量用来反映用户选择的是哪一个单选按钮。 protected: int m_nType; 并初使变量,在构造函数CShutDownCtrl()中加入代码m_nType=0; 9、为三个单选按钮添加函数,方法和上面为IDC_OK添加单击函数相同 |
LRESULT OnClickedRadio1(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
m_nType=1;
return 0;
}
LRESULT OnClickedRadio2(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
m_nType=2;
return 0;
}
LRESULT OnClickedRadio3(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
m_nType=3;
return 0;
} |
| 10、在ClassView中右击CShutDownCtrl,然后选择Add Member Function,在接一下来的对话框中输入函数类型为void,函数声明为GetSTime(int H,int M,int h,int m,int s) 然后在ShutDownCtrl.cpp中加入代码: |
void CShutDownCtrl::GetSTime(int H, int M, int h, int m, int s)
{
CHAR str[20];
m-=1;
if(h |
| 11、测试很简单,首先按F7键编译,之后打开源文件所在目录,双击ShutDownCtrl.Htm文件就可以测试了。 总结:其实编制复合控件和用MFC编一般的程序区别不大,只要搞清步骤就可以了。在此希望所有程序爱好者多多练习,学有所成。我坚信一分耕耘,就一定会有一分收获的。 |