⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertylist.h

📁 这是一本学习 window编程的很好的参考教材
💻 H
📖 第 1 页 / 共 3 页
字号:
#ifndef __PROPERTYLIST__H
#define __PROPERTYLIST__H

#pragma once

/////////////////////////////////////////////////////////////////////////////
// CPropertyList - A Property List control
//
// Written by Bjarke Viksoe (bjarke@viksoe.dk)
// Copyright (c) 2001-2003 Bjarke Viksoe.
//   Thanks to Pascal Binggeli for fixing the disabled items.
//   Column resize supplied by Remco Verhoef, thanks.
//   Also thanks to Daniel Bowen, Alex Kamenev and others for fixes.
//
// Add the following macro to the parent's message map:
//   REFLECT_NOTIFICATIONS()
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. 
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Beware of bugs.
//

#ifndef __cplusplus
  #error WTL requires C++ compilation (use a .cpp suffix)
#endif

#ifndef __ATLAPP_H__
  #error PropertyList.h requires atlapp.h to be included first
#endif

#ifndef __ATLCTRLS_H__
  #error PropertyList.h requires atlctrls.h to be included first
#endif

#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
  #include <zmouse.h>
#endif //!((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))


// Extended List styles
#define PLS_EX_CATEGORIZED     0x00000001
#define PLS_EX_SORTED          0x00000002
#define PLS_EX_XPLOOK          0x00000004
#define PLS_EX_SHOWSELALWAYS   0x00000008
#define PLS_EX_SINGLECLICKEDIT 0x00000010
#define PLS_EX_NOCOLUMNRESIZE  0x00000020

// Include property base class
#include "PropertyItem.h"

// Include property implementations
#include "PropertyItemEditors.h"
#include "PropertyItemImpl.h"



/////////////////////////////////////////////////////////////////////////////
// Category property

class CCategoryProperty : public CProperty
{
public:
   CSimpleArray<IProperty*> m_arrItems;
   bool m_fExpanded;

   CCategoryProperty(LPCTSTR pstrName, LPARAM lParam) : CProperty(pstrName, lParam), m_fExpanded(true)
   {
   }
   virtual ~CCategoryProperty()
   {
      // Need to delete hidden items too
      for( int i = 0; i < m_arrItems.GetSize(); i++ ) delete m_arrItems[i];
   }

   BYTE GetKind() const 
   { 
      return PROPKIND_CATEGORY; 
   }
   void DrawName(PROPERTYDRAWINFO& di)
   {
      CDCHandle dc(di.hDC);
      HFONT hOldFont = dc.SelectFont(di.CategoryFont);
      if( di.dwExtStyle & PLS_EX_XPLOOK ) {
         di.clrSelText = di.clrText;
         di.clrBack = ::GetSysColor(COLOR_3DFACE);
         di.clrSelBack = ::GetSysColor(COLOR_3DFACE);
         CProperty::DrawName(di);
         if( di.state & ODS_SELECTED ) {
            RECT rcFocus = di.rcItem;
            ::InflateRect(&rcFocus, -1, -1);
            SIZE size;
            dc.GetTextExtent(m_pszName, ::lstrlen(m_pszName), &size);
            rcFocus.right = rcFocus.left + size.cx + 3;
            rcFocus.bottom = rcFocus.top + size.cy;
            dc.DrawFocusRect(&rcFocus);
         }
      }
      else {
         CProperty::DrawName(di);
      }
      dc.SelectFont(hOldFont);
   }
   void DrawValue(PROPERTYDRAWINFO& di) 
   { 
      if( di.dwExtStyle & PLS_EX_XPLOOK ) {
         ::FillRect(di.hDC,  &di.rcItem, ::GetSysColorBrush(COLOR_3DFACE));
      }
   }
   BOOL Activate(UINT action, LPARAM /*lParam*/)
   {
      switch( action ) {
      case PACT_SPACE:
      case PACT_EXPAND:
      case PACT_DBLCLICK:
         ::SendMessage(m_hWndOwner, IsExpanded() ? WM_USER_PROP_COLLAPSE : WM_USER_PROP_EXPAND, 0, (LPARAM) this );
         break;
      }
      return TRUE;
   }

   BOOL IsExpanded() const
   {
      return m_fExpanded == true;
   }
   BOOL Expand(int idx)
   {
      ATLASSERT(::IsWindow(m_hWndOwner));
      CListBox ctrl(m_hWndOwner);
      ctrl.SetRedraw(FALSE);
      idx++;
      while( m_arrItems.GetSize() > 0 ) {
         IProperty* prop = m_arrItems[0];
         ATLASSERT(prop);
         m_arrItems.RemoveAt(0);
         int item = ctrl.InsertString(idx++, prop->GetName());
         ctrl.SetItemData(item, (DWORD) prop);
      }
      m_fExpanded = true;
      ctrl.SetRedraw(TRUE);
      ctrl.Invalidate();
      return TRUE;
   }
   BOOL Collapse(int idx)
   {
      ATLASSERT(::IsWindow(m_hWndOwner));
      CListBox ctrl(m_hWndOwner);
      ctrl.SetRedraw(FALSE);
      idx++;
      while( idx < ctrl.GetCount() ) {
         IProperty* prop = reinterpret_cast<IProperty*>(ctrl.GetItemData(idx));
         ATLASSERT(prop);
         if( prop->GetKind() == PROPKIND_CATEGORY ) break;
         ctrl.SetItemData(idx, 0); // Clear data now, so WM_DELETEITEM doesn't delete
                                   // the IProperty in the DeleteString() call below
         ctrl.DeleteString(idx);
         m_arrItems.Add(prop);
      }
      m_fExpanded = false;
      ctrl.SetRedraw(TRUE);
      ctrl.Invalidate();
      return TRUE;
   }
   IProperty* GetProperty(int iIndex) const
   {
      if( iIndex < 0 || iIndex >= m_arrItems.GetSize() ) return NULL;
      return m_arrItems[iIndex];
   }
};

inline HPROPERTY PropCreateCategory(LPCTSTR pstrName, LPARAM lParam=0)
{
   return new CCategoryProperty(pstrName, lParam);
}


/////////////////////////////////////////////////////////////////////////////
// CPropertyList control

template< class T, class TBase = CListBox, class TWinTraits = CWinTraitsOR<LBS_OWNERDRAWVARIABLE|LBS_NOTIFY> >
class ATL_NO_VTABLE CPropertyListImpl : 
   public CWindowImpl< T, TBase, TWinTraits >,
   public COwnerDraw< CPropertyListImpl >
{
public:
   DECLARE_WND_SUPERCLASS(NULL, TBase::GetWndClassName())

   enum { CATEGORY_INDENT = 16 };

   PROPERTYDRAWINFO m_di;
   HWND m_hwndInplace;
   int  m_iInplaceIndex;
   DWORD m_dwExtStyle;
   CFont m_TextFont;
   CFont m_CategoryFont;
   CPen m_BorderPen;
   int m_iPrevious;
   int m_iPrevXGhostBar;
   int m_iMiddle;
   bool m_bColumnFixed;

   CPropertyListImpl() : 
      m_hwndInplace(NULL), 
      m_iInplaceIndex(-1), 
      m_dwExtStyle(0UL),
      m_iMiddle(0),
      m_bColumnFixed(false),
      m_iPrevious(0),
      m_iPrevXGhostBar(0)
   {
   }

   // Operations

   BOOL SubclassWindow(HWND hWnd)
   {
      ATLASSERT(m_hWnd==NULL);
      ATLASSERT(::IsWindow(hWnd));
      BOOL bRet = CWindowImpl< T, TBase, TWinTraits >::SubclassWindow(hWnd);
      if( bRet ) _Init();
      return bRet;
   }

   void SetExtendedListStyle(DWORD dwExtStyle)
   {
      ATLASSERT(::IsWindow(m_hWnd));
      // Assign styles
      if( dwExtStyle & PLS_EX_SORTED ) {
         ATLASSERT((dwExtStyle & PLS_EX_CATEGORIZED)==0); // We don't support sorted categories!
         ATLASSERT(GetStyle() & LBS_SORT);
         ATLASSERT(GetStyle() & LBS_HASSTRINGS);
      }
      m_dwExtStyle = dwExtStyle;
      // Recalc colours and fonts
      SendMessage(WM_SETTINGCHANGE);
   }
   DWORD GetExtendedListStyle() const
   {
      return m_dwExtStyle;
   }

   void ResetContent()
   {
      ATLASSERT(::IsWindow(m_hWnd));
      _DestroyInplaceWindow();
      TBase::ResetContent();
   }
   HPROPERTY AddItem(HPROPERTY prop)
   {
      ATLASSERT(::IsWindow(m_hWnd));
      ATLASSERT(prop);
      if( prop == NULL ) return NULL;
      prop->SetOwner(m_hWnd, NULL);
      int nItem = TBase::AddString(prop->GetName());
      if( nItem == LB_ERR ) return NULL;
      TBase::SetItemData(nItem, (DWORD_PTR) prop);
      return prop;
   }
   BOOL DeleteItem(HPROPERTY prop)
   {
      ATLASSERT(::IsWindow(m_hWnd));
      ATLASSERT(prop);
      ATLASSERT(prop->GetKind()!=PROPKIND_CATEGORY);
      // Delete *visible* property!
      int iIndex = FindProperty(prop);
      if( iIndex == -1 ) return FALSE;
      return TBase::DeleteString((UINT) iIndex) != LB_ERR;
   }
   HPROPERTY GetProperty(int index) const 
   {
      ATLASSERT(::IsWindow(m_hWnd));
      ATLASSERT(index!=-1);
      IProperty* prop = reinterpret_cast<IProperty*>(TBase::GetItemData(index));
      if( prop == (IProperty*) -1 ) prop = NULL;
      return prop;
   }
   HPROPERTY FindProperty(LPCTSTR pstrName) const
   {
      ATLASSERT(::IsWindow(m_hWnd));
      ATLASSERT(pstrName);
      if( pstrName == NULL ) return NULL;
      // Find property from title
      for( int i = 0; i < GetCount(); i++ ) {
         IProperty* prop = reinterpret_cast<IProperty*>(TBase::GetItemData(i));
         ATLASSERT(prop);
         if( ::lstrcmp(prop->GetName(), pstrName) == 0 ) return prop;
         // Search properties in collapsed category items
         if( prop->GetKind() == PROPKIND_CATEGORY ) {
            CCategoryProperty* pCategory = static_cast<CCategoryProperty*>(prop);
            int j = 0;
            IProperty* subprop = NULL;
            while( (subprop = pCategory->GetProperty(j++)) != NULL ) {
               if( ::lstrcmp(subprop->GetName(), pstrName) == 0 ) return subprop;
            }
         }
      }
      return NULL;
   }
   HPROPERTY FindProperty(LPARAM lParam) const
   {
      ATLASSERT(::IsWindow(m_hWnd));
      // Find property from item-data
      // Method mostly supplied by DJ (thanks to this masked site reader)
      for( int i = 0; i < GetCount(); i++ ) {
         IProperty* prop = reinterpret_cast<IProperty*>(TBase::GetItemData(i));
         ATLASSERT(prop);
         if( prop->GetItemData() == lParam ) return prop;
         // Search properties in collapsed category items
         if( prop->GetKind() == PROPKIND_CATEGORY ) {
            CCategoryProperty* pCategory = static_cast<CCategoryProperty*>(prop);
            IProperty* subprop = NULL;
            int j = 0;
            while( (subprop = pCategory->GetProperty(j++)) != NULL ) {
               if( subprop->GetItemData() == lParam ) return subprop;
            }
         }
      }
      return NULL;
   }
   int FindProperty(HPROPERTY prop) const
   {
      // Find *visible* property!
      ATLASSERT(::IsWindow(m_hWnd));
      ATLASSERT(prop);
      if( prop == NULL ) return 0;
      for( int i = 0; i < GetCount(); i++ ) {
         if( TBase::GetItemData(i) == (DWORD_PTR) prop ) return i;
      }
      return -1;
   }
   void GetItemName(HPROPERTY prop, LPTSTR pstr, UINT cchMax) const
   {
      ATLASSERT(::IsWindow(m_hWnd));
      ATLASSERT(prop);
      if( prop == NULL ) return;
      ::lstrcpyn(pstr, prop->GetName(), cchMax);
   }
   BOOL GetItemValue(HPROPERTY prop, VARIANT* pValue) const
   {
      ATLASSERT(::IsWindow(m_hWnd));
      ATLASSERT(prop);
      ATLASSERT(pValue);
      if( prop == NULL || pValue == NULL ) return FALSE;
      return prop->GetValue(pValue);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -