?? propertyitemimpl.h
字號:
case VT_BOOL:
m_bValue = value.boolVal != VARIANT_FALSE;
return TRUE;
default:
ATLASSERT(false);
return FALSE;
}
}
void DrawValue(PROPERTYDRAWINFO& di)
{
int cxThumb = ::GetSystemMetrics(SM_CXMENUCHECK);
int cyThumb = ::GetSystemMetrics(SM_CYMENUCHECK);
RECT rcMark = di.rcItem;
rcMark.left += 10;
rcMark.right = rcMark.left + cxThumb;
rcMark.top += 2;
if( rcMark.top + cyThumb >= rcMark.bottom ) rcMark.top -= rcMark.top + cyThumb - rcMark.bottom + 1;
rcMark.bottom = rcMark.top + cyThumb;
UINT uState = DFCS_BUTTONCHECK | DFCS_FLAT;
if( m_bValue ) uState |= DFCS_CHECKED;
if( di.state & ODS_DISABLED ) uState |= DFCS_INACTIVE;
::DrawFrameControl(di.hDC, &rcMark, DFC_BUTTON, uState);
}
BOOL Activate(UINT action, LPARAM /*lParam*/)
{
switch( action ) {
case PACT_SPACE:
case PACT_CLICK:
case PACT_DBLCLICK:
if( IsEnabled() ) {
CComVariant v = !m_bValue;
::SendMessage(m_hWndOwner, WM_USER_PROP_CHANGEDPROPERTY, (WPARAM) (VARIANT*) &v, (LPARAM) this);
}
break;
}
return TRUE;
}
};
/////////////////////////////////////////////////////////////////////////////
// FileName property
class CPropertyFileNameItem : public CPropertyItem
{
public:
CPropertyFileNameItem(LPCTSTR pstrName, LPARAM lParam) : CPropertyItem(pstrName, lParam)
{
}
HWND CreateInplaceControl(HWND hWnd, const RECT& rc)
{
// Get default text
TCHAR szText[MAX_PATH] = { 0 };
if( !GetDisplayValue(szText, (sizeof(szText) / sizeof(TCHAR)) - 1) ) return NULL;
// Create EDIT control
CPropertyButtonWindow* win = new CPropertyButtonWindow();
ATLASSERT(win);
RECT rcWin = rc;
win->m_prop = this;
win->Create(hWnd, rcWin, szText);
ATLASSERT(win->IsWindow());
return *win;
}
BOOL SetValue(const VARIANT& value)
{
ATLASSERT(V_VT(&value)==VT_BSTR);
m_val = value;
return TRUE;
}
BOOL SetValue(HWND /*hWnd*/)
{
// Do nothing... A value should be set on reacting to the button notification.
// In other words: Use SetItemValue() in response to the PLN_BROWSE notification!
return TRUE;
}
BOOL Activate(UINT action, LPARAM /*lParam*/)
{
switch( action ) {
case PACT_BROWSE:
case PACT_DBLCLICK:
// Let control owner know
NMPROPERTYITEM nmh = { m_hWndOwner, ::GetDlgCtrlID(m_hWndOwner), PIN_BROWSE, this };
::SendMessage(::GetParent(m_hWndOwner), WM_NOTIFY, nmh.hdr.idFrom, (LPARAM) &nmh);
}
return TRUE;
}
BOOL GetDisplayValue(LPTSTR pstr, UINT cchMax) const
{
ATLASSERT(!::IsBadStringPtr(pstr, cchMax));
*pstr = _T('\0');
if( m_val.bstrVal == NULL ) return TRUE;
// Only display actual filename (strip path)
USES_CONVERSION;
LPCTSTR pstrFileName = OLE2CT(m_val.bstrVal);
LPCTSTR p = pstrFileName;
while( *p ) {
if( *p == _T(':') || *p == _T('\\') ) pstrFileName = p + 1;
p = ::CharNext(p);
}
::lstrcpyn(pstr, pstrFileName, cchMax);
return TRUE;
}
UINT GetDisplayValueLength() const
{
TCHAR szPath[MAX_PATH] = { 0 };
if( !GetDisplayValue(szPath, (sizeof(szPath) / sizeof(TCHAR)) - 1) ) return 0;
return ::lstrlen(szPath);
}
};
/////////////////////////////////////////////////////////////////////////////
// DropDown List property
class CPropertyListItem : public CPropertyItem
{
protected:
CSimpleArray<CComBSTR> m_arrList;
HWND m_hwndCombo;
public:
CPropertyListItem(LPCTSTR pstrName, LPARAM lParam) :
CPropertyItem(pstrName, lParam),
m_hwndCombo(NULL)
{
m_val = -1L;
}
BYTE GetKind() const
{
return PROPKIND_LIST;
}
HWND CreateInplaceControl(HWND hWnd, const RECT& rc)
{
// Get default text
UINT cchMax = GetDisplayValueLength() + 1;
LPTSTR pszText = (LPTSTR) _alloca(cchMax * sizeof(TCHAR));
ATLASSERT(pszText);
if( !GetDisplayValue(pszText, cchMax) ) return NULL;
// Create 'faked' DropDown control
CPropertyListWindow* win = new CPropertyListWindow();
ATLASSERT(win);
RECT rcWin = rc;
m_hwndCombo = win->Create(hWnd, rcWin, pszText);
ATLASSERT(win->IsWindow());
// Add list
USES_CONVERSION;
for( int i = 0; i < m_arrList.GetSize(); i++ ) win->AddItem(OLE2CT(m_arrList[i]));
win->SelectItem(m_val.lVal);
// Go...
return *win;
}
BOOL Activate(UINT action, LPARAM /*lParam*/)
{
switch( action ) {
case PACT_SPACE:
if( ::IsWindow(m_hwndCombo) ) {
// Fake button click...
::SendMessage(m_hwndCombo, WM_COMMAND, MAKEWPARAM(0, BN_CLICKED), 0);
}
break;
case PACT_DBLCLICK:
// Simulate neat VB control effect. DblClick cycles items in list...
// Set value and recycle edit control
if( IsEnabled() ) {
CComVariant v = m_val.lVal + 1;
::SendMessage(m_hWndOwner, WM_USER_PROP_CHANGEDPROPERTY, (WPARAM) (VARIANT*) &v, (LPARAM) this);
}
break;
}
return TRUE;
}
BOOL GetDisplayValue(LPTSTR pstr, UINT cchMax) const
{
ATLASSERT(m_val.vt==VT_I4);
ATLASSERT(!::IsBadStringPtr(pstr, cchMax));
*pstr = _T('\0');
if( m_val.lVal < 0 || m_val.lVal >= m_arrList.GetSize() ) return FALSE;
USES_CONVERSION;
::lstrcpyn( pstr, OLE2CT(m_arrList[m_val.lVal]), cchMax) ;
return TRUE;
}
UINT GetDisplayValueLength() const
{
ATLASSERT(m_val.vt==VT_I4);
if( m_val.lVal < 0 || m_val.lVal >= m_arrList.GetSize() ) return 0;
BSTR bstr = m_arrList[m_val.lVal];
return bstr == NULL ? 0 : ::lstrlenW(bstr);
};
BOOL SetValue(const VARIANT& value)
{
switch( value.vt ) {
case VT_BSTR:
{
m_val = 0;
for( long i = 0; i < m_arrList.GetSize(); i++ ) {
if( ::wcscmp(value.bstrVal, m_arrList[i]) == 0 ) {
m_val = i;
return TRUE;
}
}
return FALSE;
}
break;
default:
// Treat as index into list
if( FAILED( m_val.ChangeType(VT_I4, &value) ) ) return FALSE;
if( m_val.lVal >= m_arrList.GetSize() ) m_val.lVal = 0;
return TRUE;
}
}
BOOL SetValue(HWND hWnd)
{
ATLASSERT(::IsWindow(hWnd));
int len = ::GetWindowTextLength(hWnd) + 1;
LPTSTR pstr = (LPTSTR) _alloca(len * sizeof(TCHAR));
ATLASSERT(pstr);
if( !::GetWindowText(hWnd, pstr, len) ) {
if( ::GetLastError() != 0 ) return FALSE;
}
CComVariant v = pstr;
return SetValue(v);
}
void SetList(LPCTSTR* ppList)
{
ATLASSERT(ppList);
m_arrList.RemoveAll();
while( *ppList ) {
CComBSTR bstr(*ppList);
m_arrList.Add(bstr);
ppList++;
}
if( m_val.lVal == -1 ) m_val = 0;
}
void AddListItem(LPCTSTR pstrText)
{
ATLASSERT(!::IsBadStringPtr(pstrText,-1));
CComBSTR bstr(pstrText);
m_arrList.Add(bstr);
if( m_val.lVal == -1 ) m_val = 0;
}
};
/////////////////////////////////////////////////////////////////////////////
// Boolean property
class CPropertyBooleanItem : public CPropertyListItem
{
public:
CPropertyBooleanItem(LPCTSTR pstrName, LPARAM lParam) : CPropertyListItem(pstrName, lParam)
{
#ifdef IDS_TRUE
TCHAR szBuffer[32];
::LoadString(_Module.GetResourceInstance(), IDS_FALSE, szBuffer, sizeof(szBuffer) / sizeof(TCHAR));
AddListItem(szBuffer);
::LoadString(_Module.GetResourceInstance(), IDS_TRUE, szBuffer, sizeof(szBuffer) / sizeof(TCHAR));
AddListItem(szBuffer);
#else
AddListItem(_T("False"));
AddListItem(_T("True"));
#endif
}
};
/////////////////////////////////////////////////////////////////////////////
// ListBox Control property
class CPropertyComboItem : public CPropertyItem
{
public:
CListBox m_ctrl;
CPropertyComboItem(LPCTSTR pstrName, LPARAM lParam) :
CPropertyItem(pstrName, lParam)
{
}
HWND CreateInplaceControl(HWND hWnd, const RECT& rc)
{
ATLASSERT(::IsWindow(m_ctrl));
// Create window
CPropertyComboWindow* win = new CPropertyComboWindow();
ATLASSERT(win);
RECT rcWin = rc;
win->m_hWndCombo = m_ctrl;
win->Create(hWnd, rcWin);
ATLASSERT(::IsWindow(*win));
return *win;
}
BYTE GetKind() const
{
return PROPKIND_CONTROL;
}
void DrawValue(PROPERTYDRAWINFO& di)
{
RECT rc = di.rcItem;
::InflateRect(&rc, 0, -1);
DRAWITEMSTRUCT dis = { 0 };
dis.hDC = di.hDC;
dis.hwndItem = m_ctrl;
dis.CtlID = m_ctrl.GetDlgCtrlID();
dis.CtlType = ODT_LISTBOX;
dis.rcItem = rc;
dis.itemState = ODS_DEFAULT | ODS_COMBOBOXEDIT;
dis.itemID = m_ctrl.GetCurSel();
dis.itemData = (int) m_ctrl.GetItemData(dis.itemID);
::SendMessage(m_ctrl, OCM_DRAWITEM, dis.CtlID, (LPARAM) &dis);
}
BOOL GetValue(VARIANT* pValue) const
{
CComVariant v = (int) m_ctrl.GetItemData(m_ctrl.GetCurSel());
return SUCCEEDED( v.Detach(pValue) );
}
BOOL SetValue(HWND /*hWnd*/)
{
int iSel = m_ctrl.GetCurSel();
CComVariant v = (int) m_ctrl.GetItemData(iSel);
return SetValue(v);
}
BOOL SetValue(const VARIANT& value)
{
ATLASSERT(value.vt==VT_I4);
for( int i = 0; i < m_ctrl.GetCount(); i++ ) {
if( m_ctrl.GetItemData(i) == (DWORD_PTR) value.lVal ) {
m_ctrl.SetCurSel(i);
return TRUE;
}
}
return FALSE;
}
};
/////////////////////////////////////////////////////////////////////////////
//
// CProperty creators
//
inline HPROPERTY PropCreateVariant(LPCTSTR pstrName, const VARIANT& vValue, LPARAM lParam = 0)
{
CPropertyEditItem* prop = NULL;
ATLTRY( prop = new CPropertyEditItem(pstrName, lParam) );
ATLASSERT(prop);
if( prop ) prop->SetValue(vValue);
return prop;
}
inline HPROPERTY PropCreateSimple(LPCTSTR pstrName, LPCTSTR pstrValue, LPARAM lParam = 0)
{
CComVariant vValue = pstrValue;
return PropCreateVariant(pstrName, vValue, lParam);
}
inline HPROPERTY PropCreateSimple(LPCTSTR pstrName, int iValue, LPARAM lParam = 0)
{
CComVariant vValue = iValue;
return PropCreateVariant(pstrName, vValue, lParam);
}
inline HPROPERTY PropCreateSimple(LPCTSTR pstrName, bool bValue, LPARAM lParam = 0)
{
// NOTE: Converts to integer, since we're using value as an index to dropdown
CComVariant vValue = (int) bValue & 1;
CPropertyBooleanItem* prop = NULL;
ATLTRY( prop = new CPropertyBooleanItem(pstrName, lParam) );
ATLASSERT(prop);
if( prop ) prop->SetValue(vValue);
return prop;
}
inline HPROPERTY PropCreateFileName(LPCTSTR pstrName, LPCTSTR pstrFileName, LPARAM lParam = 0)
{
ATLASSERT(!::IsBadStringPtr(pstrFileName,-1));
CPropertyFileNameItem* prop = NULL;
ATLTRY( prop = new CPropertyFileNameItem(pstrName, lParam) );
ATLASSERT(prop);
if( prop == NULL ) return NULL;
CComVariant vValue = pstrFileName;
prop->SetValue(vValue);
return prop;
}
inline HPROPERTY PropCreateDate(LPCTSTR pstrName, const SYSTEMTIME stValue, LPARAM lParam = 0)
{
IProperty* prop = NULL;
ATLTRY( prop = new CPropertyDateItem(pstrName, lParam) );
ATLASSERT(prop);
if( prop == NULL ) return NULL;
CComVariant vValue;
vValue.vt = VT_DATE;
vValue.date = 0.0; // NOTE: Clears value in case of conversion error below!
if( stValue.wYear > 0 ) ::SystemTimeToVariantTime( (LPSYSTEMTIME) &stValue, &vValue.date );
prop->SetValue(vValue);
return prop;
}
inline HPROPERTY PropCreateList(LPCTSTR pstrName, LPCTSTR* ppList, int iValue = 0, LPARAM lParam = 0)
{
ATLASSERT(ppList);
CPropertyListItem* prop = NULL;
ATLTRY( prop = new CPropertyListItem(pstrName, lParam) );
ATLASSERT(prop);
if( prop && ppList ) {
prop->SetList(ppList);
CComVariant vValue = iValue;
prop->SetValue(vValue);
}
return prop;
}
inline HPROPERTY PropCreateComboControl(LPCTSTR pstrName, HWND hWnd, int iValue, LPARAM lParam = 0)
{
ATLASSERT(::IsWindow(hWnd));
CPropertyComboItem* prop = NULL;
ATLTRY( prop = new CPropertyComboItem(pstrName, lParam) );
ATLASSERT(prop);
if( prop ) {
prop->m_ctrl = hWnd;
CComVariant vValue = iValue;
prop->SetValue(vValue);
}
return prop;
}
inline HPROPERTY PropCreateCheckButton(LPCTSTR pstrName, bool bValue, LPARAM lParam = 0)
{
return new CPropertyCheckButtonItem(pstrName, bValue, lParam);
}
inline HPROPERTY PropCreateReadOnlyItem(LPCTSTR pstrName, LPCTSTR pstrValue = _T(""), LPARAM lParam = 0)
{
ATLASSERT(!::IsBadStringPtr(pstrValue,-1));
CPropertyItem* prop = NULL;
ATLTRY( prop = new CPropertyReadOnlyItem(pstrName, lParam) );
ATLASSERT(prop);
if( prop ) {
CComVariant v = pstrValue;
prop->SetValue(v);
}
return prop;
}
#endif // __PROPERTYITEMIMPL__H
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -