我对ATL没研究过,请问MFC可以制作复合控件吗?又如何操作才行呢?谢谢!

解决方案 »

  1.   

    MFC功能相当强大,只要你想得到的,就能做得到。没研究过就去研究吧
      

  2.   

    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,"-时-分-秒",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 if(m sprintf(str,"-时-分-秒",h-H,m-M,60-s);
    SetDlgItemText(IDC_STIME,str);
    }  11、测试很简单,首先按F7键编译,之后打开源文件所在目录,双击ShutDownCtrl.Htm文件就可以测试了。