<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    在VC++中使用Tab Control控件

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    系統環境:Windows 7
    軟件環境:Visual Studio 2008 SP1
    本次目的:在模態或非模態對話框中使用Tab Control控件,及引申在單/多文檔中使用

     

    查閱MSDN文檔,對于創建Tab Control控件,MSDN上說明如下:

    To use CTabCtrl directly in a dialog box

    1.     In the dialog editor, add a Tab Control to your dialog template resource. Specify its control ID.

    2.     Use the Add Member Variable Wizard to add a member variable of type CTabCtrl with the Control property. You can use this member to call CTabCtrl member functions.

    3.     Map handler functions in the dialog class for any tab control notification messages you need to handle. For more information, see Mapping Messages to Functions.

    4.     In OnInitDialog, set the styles for the CTabCtrl .

    To use CTabCtrl in a nondialog window

    1.     Define the control in the view or window class.

    2.     Call the control's Create member function, possibly in OnInitialUpdate, possibly as early as the parent window's OnCreate handler function (if you're subclassing the control). Set the styles for the control.

    對于直接使用 CTabCtrl 在對話框內

    1.          在對話框編輯區內,添加一個 Tab Control 控件到資源模板里面,設置它的控制 ID

    2.          使用添加成員變量向導,為控件添加一個 CTabCtrl 類型的成員變量,你可以使用這個變量調用 CTabCtrl 的成員函數

    3.          對話框類的映射處理功能可以處理任何你需要處理的標簽控件消息。有關更多信息,請參閱消息映射函數。

    4.           OnInitDialog() 函數里面,設置 CTabCtrl 的風格。

    對于在非對話框窗口使用 CTabCtrl

    1.          定義在視圖或窗口類的控件。

    2.          調用控件的創建成員函數,可能在 OnInitialUpdate 中,可能在父窗口的 OnCreate 處理函數早期(如果你是子類的控件)。設置控件的風格。

     

    下面介紹在對話框中添加 Tab Control 控件,工程不一定是要 MFC 基于對話框形式,單文檔視圖類派生自 CFormView 的工程也行,或是任何工程彈出的對話框 ( 如登錄界面等等 ) 都行 ( 個人沒有都去實驗,但理論上應該可以,看了下面就知道。 )

    1.        首先在對話框資源上添加一個 Tab Control 控件 ID  IDC_LOGIN_TAB ,根據需要修改其屬性,然后為其添加成員變量 m_tab ,類型為 CTabCtrl

    2.        需要幾個選項卡,則在對話框資源添加幾個對話框,在這里我添加兩個選項卡,則要添加兩個對話框,其 ID 分別為 IDD_TAB1_DIALOG  IDD_TAB2_DIALOG 它們的屬性 style  Child, Border  None ,其他的再根據自己調整。然后分別為其添加對應的基于 CDialog  CLoginTab1  CLoginTab2

    3.        在主對話框添加子對話框頭文件,然后再添加三個成員變量,為子對話框添加實例。在 OnInitDialog() 函數初始化 Tab Control 控件顯示,若對話框沒有 OnInitDialog() 函數,則重載這個函數,具體如下:

    [cpp] view plaincopy
    1. //主對話框頭文件  
    2. class LoginUser : public CDialog  
    3. {•••  
    4. protected:  
    5.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持  
    6.     virtual BOOL OnInitDialog();            //沒有這個,則在這里重載  
    7.     DECLARE_MESSAGE_MAP()  
    8. public:  
    9.     CTabCtrl m_tab;  
    10.     CLoginTab1 m_login_tab1;  
    11.     CLoginTab2 m_login_tab2;  
    12. •••  
    13. }  

    [cpp] view plaincopy
    1. //主對話框實現文件  
    2. BOOL LoginUser::OnInitDialog()  
    3. {  
    4.     CDialog::OnInitDialog();  
    5.     m_tab.InsertItem(0,_T("系統登錄"));  
    6.     m_tab.InsertItem(1,_T("服務器設置"));  
    7.     m_login_tab1.Create(IDD_TAB1_DIALOG,GetDlgItem(IDC_LOGIN_TAB));  
    8.     m_login_tab2.Create(IDD_TAB2_DIALOG,GetDlgItem(IDC_LOGIN_TAB));  
    9.     //獲得IDC_TABTEST客戶區大小  
    10. CRect rs;  
    11. m_tab.GetClientRect(&rs);  
    12. //調整子對話框在父窗口中的位置,根據實際修改  
    13. rs.top+=25;  
    14. rs.bottom-=60;  
    15. rs.left+=1;  
    16. rs.right-=10;  
    17.       
    18. //設置子對話框尺寸并移動到指定位置  
    19. m_login_tab1.MoveWindow(&rs);  
    20. m_login_tab2.MoveWindow(&rs);  
    21. //分別設置隱藏和顯示  
    22. m_login_tab1.ShowWindow(true);  
    23. m_login_tab2.ShowWindow(false);  
    24.       
    25. //設置默認的選項卡  
    26. m_tab.SetCurSel(0);  
    27. return TRUE;  
    28. }  

     

    4.        響應選項卡切換事件消息,右鍵 Tab Control 控件,添加事件處理程序,選擇 TCN_SELCHANGE 事件,在彈出的編輯區域,填入以下代碼:

    [cpp] view plaincopy
    1. void LoginUser::OnTcnSelchangeLoginTab(NMHDR *pNMHDR, LRESULT *pResult)  
    2. {  
    3.     // TODO: 在此添加控件通知處理程序代碼  
    4.     int CurSel = m_tab.GetCurSel();  
    5.     switch(CurSel)  
    6.     {  
    7.     case 0:  
    8.         m_login_tab1.ShowWindow(true);  
    9.         m_login_tab2.ShowWindow(false);  
    10.         break;  
    11.     case 1:  
    12.         m_login_tab1.ShowWindow(false);  
    13.         m_login_tab2.ShowWindow(true);  
    14.         break;  
    15.     default: ;  
    16.     }  
    17.     *pResult = 0;  
    18. }  

     

     

     

     5.   編譯運行。
    RFID設備管理軟件

    接下來,來看看非模態對話框的實現,因為Tab Control控件要在對話框的OnInitDialog () 函數初始化,而MSDN上說:

    After the dialog box and all of its controls are created but just before the dialog box (of either type) appears on the screen, the dialog object's OnInitDialog member function is called. For a modal dialog box, this occurs during the DoModal call. For a modeless dialog box, OnInitDialog is called when Create is called. You typically override OnInitDialog to initialize the dialog box's controls, such as setting the initial text of an edit box. You must call the OnInitDialog member function of the base class, CDialog , from your OnInitDialog override.

     非模態對話框是在Create的時候一起調用初始化函數的,OnInitDialog()我們仍可以重載這個函數,來實現以上功能。對于單文檔/多文檔可以看前面的MSDN說明,這里不再詳細寫。本文若有錯誤,請指出。

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全