오후 2:32 99-12-31
탭 & 프로퍼티 쉬트 만들기
-----------------------------------------------------------

초간단 프로퍼티 쉬트 만들기
먼저 페이지를 만들고 쉬트에다가 AddPage하고 쉬트.DoModal하면 된다.

페이지 만들기

일딴 페이지에 쓰일 리소스 다이얼로그를 하나 만들고 거기에다가

페이지 치장을 한다. ( 아이디는 IDD_MYPAGE1 ) 속성을 Child로 하고

보더를 Thin으로 한다. 타이틀은 그대로 갖고 있는다 타이틀이 페이지명

클래스 위자드로 CMyPage1을 만든다. ( 선조클래스 CPropertyPage )
또한 아까 만든 IDD_MYPAGE1을 리소스 다이얼로그로 쓴다.

버튼을 눌렀을때 코드를 다음 처럼 짜본다.

        CPropertySheet x("쉬트정보");;
        CMyPage1 p1;

        x.AddPage( &p1);

        if( x.DoModal() == IDOK )
        {
                // Apply 적용시 코드를 넣어야 할 곳
        }




나만의 프러퍼티 시트 만들기  
---------------------------
일딴 CPropertySheet를 상속받은 MySheet를 만든다. 여기에서
이걸로 예쁘게 치장을 한다.

dlg.h
MySheet m_Sheet;

dlg.cpp 에서
생성자에서 초기와로

CTestSheetDlg::CTestSheetDlg(CWnd* pParent /*=NULL*/)
        : CDialog(CTestSheetDlg::IDD, pParent),m_Sheet("My Sheet")  <- 해주고

dlg 버튼 클릭시를

MyPage1 p1;
m_Sheet.AddPage(new MyPage1);
m_Sheet.AddPage(&p1);
m_Sheet.DoModal() 해준다.


탭 컨트롤 쓰기
===========================================

프로퍼티 쉬트 같지만 다이얼로그에 직접 붙이는 것으로

CEZTabCtrl을 쓰면된다.

먼저 탭에 쓸 페이지를 만들어야 하는데

다이얼로그를 하나만든다. 폼을 갖은 MyTab1이라는 탭페이지 다이얼로그를
만든다.  ( Child이면서 Title은 없어야 한다. )


먼저 탭 컨트롤을 원하는 다이얼로그 폼에 붙인다.

그리고

dlg.h에서
CEzTabCtrl m_Tab; 을 한후

dlg.cpp 에서

void CTestSheetDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CTestSheetDlg)
                // NOTE: the ClassWizard will add DDX and DDV calls here
        //}}AFX_DATA_MAP
        DDX_Control(pDX, IDC_TAB1, m_Tab);
}
하고

BOOL CTestSheetDlg::OnInitDialog()
{
:
        CDialog* pTab = new MyTab1;
        pTab->Create( MyTab1::IDD, &m_Tab);
        m_Tab.AddTab( pTab, "캡션" );
}
하면 된다.


탭 제목에 이미지 넣기

이미지 리스트 CImageListCtrl m_Image;

BOOL CTestSheetDlg::OnInitDialog()
{
       :
   m_Image.Create(IDB_TABIMAGE, 11, 0 ,RGB(0,255,0));
   m_Tab.SetImageList(&m_Image);
   CDialog* pTab = new MyTab1;
   pTab->Create( MyTab1::IDD, &m_Tab);
   m_Tab.AddTab( pTab, "캡션" ,0 ); //0 번 이미지를 붙인다.
        :
}

=-=========================================================================
CExTabCtrl의 소스 코드

h 헤더 파일
#if !defined(AFX_XTABCTRL_H__A11951B3_2F95_11D3_A896_00A0C9B6FB28__INCLUDED_)
#define AFX_XTABCTRL_H__A11951B3_2F95_11D3_A896_00A0C9B6FB28__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// XTabCtrl.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CEZTabCtrl window
#include <afxtempl.h>

class CEZTabCtrl : public CTabCtrl
{
// Construction
public:
        CEZTabCtrl();

// Attributes
public:

// Operations
public:
        void AddTab(CWnd* pWnd, LPTSTR lpszCaption, int iImage =0);
        void EnableTab(int iIndex, BOOL bEnable = TRUE);
        BOOL SelectTab(int iIndex);
        void DeleteAllTabs();
        void DeleteTab(int iIndex);
        void SetTopLeftCorner(CPoint pt);
        BOOL IsTabEnabled(int iIndex);

        void SetDisabledColor(COLORREF cr);
        void SetSelectedColor(COLORREF cr);
        void SetNormalColor(COLORREF cr);
        void SetMouseOverColor(COLORREF cr);

        //void FireKey(
// Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CEZTabCtrl)
        protected:
        virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
        virtual void PreSubclassWindow();
        //}}AFX_VIRTUAL

// Implementation
public:
        virtual ~CEZTabCtrl();

        // Generated message map functions
protected:
        CArray<BOOL, BOOL> m_arrayStatusTab; //** enabled Y\N
        int m_iSelectedTab;
        POINT m_ptTabs;
        COLORREF m_crSelected;
        COLORREF m_crDisabled;
        COLORREF m_crNormal;
        COLORREF m_crMouseOver;

        int m_iIndexMouseOver;
        
        bool m_bMouseOver;
        bool m_bColorMouseOver;
        bool m_bColorNormal;
        bool m_bColorDisabled;
        bool m_bColorSelected;
        
        //{{AFX_MSG(CEZTabCtrl)
        afx_msg void OnSelchange(NMHDR* pNMHDR, LRESULT* pResult);
        afx_msg void OnSelchanging(NMHDR* pNMHDR, LRESULT* pResult);
        afx_msg void OnMouseMove(UINT nFlags, CPoint point);
        afx_msg void OnTimer(UINT nIDEvent);
        //}}AFX_MSG

        DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_XTABCTRL_H__A11951B3_2F95_11D3_A896_00A0C9B6FB28__INCLUDED_)


ExTabCtrl.cpp 이다.
// XTabCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "TestSheet.h"
#include "XTabCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEZTabCtrl

CEZTabCtrl::CEZTabCtrl()
{
        m_iSelectedTab = 0;
        m_ptTabs.x = 4;
        m_ptTabs.y = 28;

        m_crSelected = 0;
        m_crDisabled = 0;
        m_crNormal = 0;
        m_crMouseOver = 0;

        m_bColorSelected  = false;
        m_bColorDisabled  = false;
        m_bColorNormal    = false;
        m_bColorMouseOver = false;


        m_iIndexMouseOver = -1;

        m_bMouseOver = false;
}

CEZTabCtrl::~CEZTabCtrl()
{
        m_arrayStatusTab.RemoveAll();
}


BEGIN_MESSAGE_MAP(CEZTabCtrl, CTabCtrl)
        //{{AFX_MSG_MAP(CEZTabCtrl)
        ON_NOTIFY_REFLECT(TCN_SELCHANGE, OnSelchange)
        ON_NOTIFY_REFLECT(TCN_SELCHANGING, OnSelchanging)
        ON_WM_MOUSEMOVE()
        ON_WM_TIMER()
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEZTabCtrl message handlers

void CEZTabCtrl::SetTopLeftCorner(CPoint pt)
{
        m_ptTabs.x = pt.x;
        m_ptTabs.y = pt.y;
}

void CEZTabCtrl::SetMouseOverColor(COLORREF cr)
{
        m_bColorMouseOver = true;
        m_crMouseOver = cr;
}


void CEZTabCtrl::SetDisabledColor(COLORREF cr)
{
        m_bColorDisabled = true;
        m_crDisabled = cr;
}

void CEZTabCtrl::SetSelectedColor(COLORREF cr)
{
        m_bColorSelected = true;
        m_crSelected = cr;
}


void CEZTabCtrl::SetNormalColor(COLORREF cr)
{
        m_bColorNormal = true;
        m_crNormal = cr;
}



void CEZTabCtrl::AddTab(CWnd* pWnd, LPTSTR lpszCaption, int iImage)
{
        ASSERT_VALID(pWnd);

        TCITEM item;
        item.mask = TCIF_TEXT|TCIF_PARAM|TCIF_IMAGE;
        item.lParam = (LPARAM) pWnd;
        item.pszText = lpszCaption;
        item.iImage = iImage;

        int iIndex = m_arrayStatusTab.GetSize();
        InsertItem(iIndex, &item);

        pWnd->SetWindowPos(NULL, m_ptTabs.x, m_ptTabs.y , 0, 0,
                                                SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOZORDER);
        
        pWnd->ShowWindow(iIndex ? SW_HIDE : SW_SHOW);

        //** the initial status is enabled
        m_arrayStatusTab.Add(TRUE);

}

void CEZTabCtrl::EnableTab(int iIndex, BOOL bEnable)
{
        ASSERT(iIndex < m_arrayStatusTab.GetSize());

        //** if it should change the status ----
        if (m_arrayStatusTab[iIndex] != bEnable)
        {
                m_arrayStatusTab[iIndex] = bEnable;

                //** redraw the item -------
                CRect rect;

                GetItemRect(iIndex, &rect);
                InvalidateRect(rect);
        }
}

void CEZTabCtrl::DeleteAllTabs()
{
        m_arrayStatusTab.RemoveAll();
        
        DeleteAllItems();

}

void CEZTabCtrl::DeleteTab(int iIndex)
{
        ASSERT(iIndex < m_arrayStatusTab.GetSize());

        m_arrayStatusTab.RemoveAt(iIndex);

        DeleteItem(iIndex);

}


BOOL CEZTabCtrl::IsTabEnabled(int iIndex)
{
        ASSERT(iIndex < m_arrayStatusTab.GetSize());
        
        return m_arrayStatusTab[iIndex];
}


BOOL CEZTabCtrl::SelectTab(int iIndex)
{
        ASSERT(iIndex < m_arrayStatusTab.GetSize());

        if (GetCurSel() == iIndex)
                return TRUE;

        //** check if the tab is enabled --------
        if (m_arrayStatusTab[iIndex])
        {
                TCITEM item;
                CWnd* pWnd;
                item.mask = TCIF_PARAM;

                GetItem(GetCurSel(), &item);
                pWnd = reinterpret_cast<CWnd*> (item.lParam);
                ASSERT_VALID(pWnd);
                pWnd->ShowWindow(SW_HIDE);

                SetCurSel(iIndex);
                GetItem(iIndex, &item);
                pWnd = reinterpret_cast<CWnd*> (item.lParam);
                ASSERT_VALID(pWnd);
                pWnd->ShowWindow(SW_SHOW);

                return TRUE;
        }

        return FALSE;
}

void CEZTabCtrl::OnSelchange(NMHDR* pNMHDR, LRESULT* pResult)
{
        // TODO: Add your control notification handler code here
        int iNewTab = GetCurSel();

        if (!IsTabEnabled(iNewTab))
        {
                SetCurSel(m_iSelectedTab);
        }
        else
        {
                TCITEM item;
                CWnd* pWnd;

                item.mask = TCIF_PARAM;
                
                //** hide the current tab ---------
                GetItem(m_iSelectedTab, &item);
                pWnd = reinterpret_cast<CWnd*> (item.lParam);
                ASSERT_VALID(pWnd);
                pWnd->ShowWindow(SW_HIDE);

                //** show the selected tab --------
                GetItem(iNewTab, &item);
                pWnd = reinterpret_cast<CWnd*> (item.lParam);
                ASSERT_VALID(pWnd);
                pWnd->ShowWindow(SW_SHOW);

        }

        *pResult = 0;
}

void CEZTabCtrl::OnSelchanging(NMHDR* pNMHDR, LRESULT* pResult)
{
        // TODO: Add your control notification handler code here
        m_iSelectedTab = GetCurSel();

        *pResult = 0;
}



void CEZTabCtrl::PreSubclassWindow()
{
        // TODO: Add your specialized code here and/or call the base class
        CTabCtrl::PreSubclassWindow();
        ModifyStyle(0, TCS_OWNERDRAWFIXED);
}


void CEZTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{        
        CRect rect = lpDrawItemStruct->rcItem;
        rect.top += ::GetSystemMetrics(SM_CYEDGE);

        int nTabIndex = lpDrawItemStruct->itemID;
        
        if (nTabIndex < 0) return;

        BOOL bSelected = (nTabIndex == GetCurSel());

        COLORREF crSelected = m_bColorSelected ? m_crSelected : GetSysColor(COLOR_BTNTEXT);
        COLORREF crNormal  = m_bColorNormal   ? m_crNormal   : GetSysColor(COLOR_BTNTEXT);
        COLORREF crDisabled = m_bColorDisabled ? m_crDisabled : GetSysColor(COLOR_GRAYTEXT);


        char label[64];
        TC_ITEM item;
        item.mask = TCIF_TEXT|TCIF_IMAGE;
        item.pszText = label;    
        item.cchTextMax = 63;            
        if (!GetItem(nTabIndex, &item))
                return;

        CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
        if (!pDC)
                return;

        int nSavedDC = pDC->SaveDC();

        CRect rectItem;
        POINT pt;

        GetItemRect(nTabIndex, &rectItem);
        GetCursorPos(&pt);
        ScreenToClient(&pt);

        if (rectItem.PtInRect(pt))
                m_iIndexMouseOver = nTabIndex;



        pDC->SetBkMode(TRANSPARENT);
        pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE));

        //**  Draw the image
        CImageList* pImageList = GetImageList();
        if (pImageList && item.iImage >= 0)
        {

                rect.left += pDC->GetTextExtent(_T(" ")).cx;

                IMAGEINFO info;
                pImageList->GetImageInfo(item.iImage, &info);
                CRect ImageRect(info.rcImage);
                int nYpos = rect.top;

                pImageList->Draw(pDC, item.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT);
                rect.left += ImageRect.Width();
        }

        if (!IsTabEnabled(nTabIndex))
        {
                pDC->SetTextColor(crDisabled);
                rect.top -= ::GetSystemMetrics(SM_CYEDGE);
                pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
        }
        else
        {
                //** selected item -----
                if (bSelected)
                        pDC->SetTextColor(crSelected);
                else //** other item ---
                {
                        if (m_bColorMouseOver && nTabIndex == m_iIndexMouseOver)
                        {
                                pDC->SetTextColor(m_crMouseOver);        
                        }
                        else
                        {
                                pDC->SetTextColor(crNormal);
                        }
                }

                rect.top -= ::GetSystemMetrics(SM_CYEDGE);
                pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);

        }

        pDC->RestoreDC(nSavedDC);
}

void CEZTabCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
        // TODO: Add your message handler code here and/or call default
        
        //** if we should change the color of the tab ctrl ---
        if (m_bColorMouseOver)
        {
                SetTimer(1,10,NULL);

                if (m_iIndexMouseOver != -1)
                {
                        CRect rectItem;
                        GetItemRect(m_iIndexMouseOver, rectItem);
                        if (!rectItem.PtInRect(point))
                        {
                                CRect rectOldItem;
                                GetItemRect(m_iIndexMouseOver, rectOldItem);
                                m_iIndexMouseOver = -1;
                                InvalidateRect(rectOldItem);
                                return;
                        }
                }
        

                if (!m_bMouseOver)
                {
                        TCHITTESTINFO hitTest;

                        m_bMouseOver = true;
                        hitTest.pt = point;

                        int iItem = HitTest(&hitTest);
                        if (iItem != -1 && m_arrayStatusTab[iItem])
                        {
                                RECT rectItem;
                                GetItemRect(iItem, &rectItem);

                                InvalidateRect(&rectItem);
                        }
                }
        }
        
        CTabCtrl::OnMouseMove(nFlags, point);
}

void CEZTabCtrl::OnTimer(UINT nIDEvent)
{
        // TODO: Add your message handler code here and/or call default
        POINT pt;
        GetCursorPos(&pt);
        CRect rectItem, rectScreen;

        GetItemRect(m_iIndexMouseOver, rectItem);
        rectScreen = rectItem;
        ClientToScreen(rectScreen);
        
        // If mouse leaves, show normal
        if (!rectScreen.PtInRect(pt))
        {
                KillTimer (1);
                m_bMouseOver = false;
                m_iIndexMouseOver = -1;
                InvalidateRect(rectItem);
        }


        CTabCtrl::OnTimer(nIDEvent);
}

'KB > MFC/Win32' 카테고리의 다른 글

ATL 객체포인터 참조하기  (0) 2004.03.19
argv 얻어오기  (0) 2004.03.19
콤보 박스  (0) 2004.03.19
자기 exe 풀 패스 얻어오기  (0) 2004.03.19
메세지 큐를통해서 사용자 메세지 전달하기  (0) 2004.03.19

+ Recent posts