뭔가 왜이렇게 허접해보이냐 .. 읽기도 드럽게 어렵고 2년전에 만들어서 그런가 ㅡㅡ;
//CustomProperty.h
#pragma once
#include <string>
using namespace std;
class CCheckBoxProp : public CMFCPropertyGridProperty
{
public:
CCheckBoxProp(const CString& strName, const COleVariant& varValue, LPCTSTR lpszDescr = NULL, DWORD_PTR dwData = 0,
LPCTSTR lpszEditMask = NULL, LPCTSTR lpszEditTemplate = NULL, LPCTSTR lpszValidChars = NULL) :
CMFCPropertyGridProperty(strName, (COleVariant) &varValue, lpszDescr, dwData, lpszEditMask,lpszEditTemplate, lpszValidChars)
{
m_rectCheck.SetRectEmpty();
};
CCheckBoxProp(const CString& strGroupName, DWORD_PTR dwData = 0, BOOL bIsValueList = FALSE) :
CMFCPropertyGridProperty(strGroupName, dwData, bIsValueList)
{
m_rectCheck.SetRectEmpty();
};
CCheckBoxProp(const CString& strGroupName, DWORD_PTR dwData = 0, BOOL bIsValueList = FALSE, BOOL bCheck = FALSE) :
CMFCPropertyGridProperty(strGroupName, dwData, bIsValueList)
{
m_varValue.boolVal = bCheck;
m_rectCheck.SetRectEmpty();
};
protected:
virtual BOOL OnEdit(LPPOINT /*lptClick*/) { return FALSE; }
virtual void OnDrawButton(CDC* /*pDC*/, CRect /*rectButton*/) {}
virtual void OnDrawValue(CDC* /*pDC*/, CRect /*rect*/) {}
virtual BOOL HasButton() const { return FALSE; }
virtual BOOL PushChar(UINT nChar);
virtual void OnDrawCheckBox(CDC * pDC, CRect rectCheck, BOOL bChecked);
virtual void OnDrawName(CDC* pDC, CRect rect);
virtual void OnClickName(CPoint point);
virtual BOOL OnDblClk(CPoint point);
protected:
CRect m_rectCheck;
};
///////////////////////////////////////////////////////////////////////////////
// CPropCheckBoxCtrl class
class CPropCheckBoxCtrl : public CMFCPropertyGridProperty
{
protected :
CRect m_rectCheck;
public :
BOOL m_varValue;
BOOL isBRadioMode; //is Radio Mode?
public :
CPropCheckBoxCtrl::CPropCheckBoxCtrl(const CString& strName) : CMFCPropertyGridProperty(strName)
{
m_rectCheck.SetRectEmpty();
m_varValue = FALSE;
isBRadioMode = FALSE;
}
CPropCheckBoxCtrl::CPropCheckBoxCtrl(const CString& strName, BOOL bCheck, LPCTSTR lpszDescr, DWORD dwData) :
CMFCPropertyGridProperty(strName, COleVariant((long)bCheck), lpszDescr, dwData)
{
m_rectCheck.SetRectEmpty();
m_varValue = FALSE;
isBRadioMode = FALSE;
}
CPropCheckBoxCtrl(const CString& strName, const COleVariant& varValue, LPCTSTR lpszDescr = NULL, DWORD_PTR dwData = 0,
LPCTSTR lpszEditMask = NULL, LPCTSTR lpszEditTemplate = NULL, LPCTSTR lpszValidChars = NULL) :
CMFCPropertyGridProperty(strName, varValue,lpszDescr ,dwData ,lpszEditMask ,lpszEditTemplate,lpszValidChars)
{
m_rectCheck.SetRectEmpty();
m_varValue = FALSE;
isBRadioMode = FALSE;;
}
public :
void OnDrawName(CDC* pDC, CRect rect);
void OnClickName(CPoint point);
BOOL OnDblClk(CPoint point);
void OnDrawCheckBox(CDC * pDC, CRect rect, BOOL bChecked);
BOOL PushChar(UINT nChar);
void SetCheck(BOOL bCheck) ;
};
///////////////////////////////////////////////////////////////////////////////
// CPropCheckRadioBoxCtrl class
class CPropCheckRadioBoxCtrl : public CMFCPropertyGridProperty
{
public :
CArray<BOOL *> m_pChildRadioBool;
public :
CPropCheckRadioBoxCtrl::CPropCheckRadioBoxCtrl(const CString& strName) : CMFCPropertyGridProperty(strName)
{
}
CPropCheckRadioBoxCtrl::CPropCheckRadioBoxCtrl(const CString& strName, BOOL bCheck, LPCTSTR lpszDescr, DWORD dwData) :
CMFCPropertyGridProperty(strName, COleVariant((long)bCheck), lpszDescr, dwData)
{
}
CPropCheckRadioBoxCtrl(const CString& strName, const COleVariant& varValue, LPCTSTR lpszDescr = NULL, DWORD_PTR dwData = 0,
LPCTSTR lpszEditMask = NULL, LPCTSTR lpszEditTemplate = NULL, LPCTSTR lpszValidChars = NULL) :
CMFCPropertyGridProperty(strName, varValue,lpszDescr ,dwData ,lpszEditMask ,lpszEditTemplate,lpszValidChars)
{
}
public :
BOOL AddSubItem(CMFCPropertyGridProperty * pChild);
void RadioCheck(BOOL * );
};
///////////////////////////////////////////////////////////////////////////////
// CUserPropertyGridFile class
class CUserPropertyGridFile : public CMFCPropertyGridFileProperty
{
public:
DECLARE_DYNAMIC(CUserPropertyGridFile)
public:
CUserPropertyGridFile(const CString& strName, const CString& strFolderName, DWORD_PTR dwData = 0, LPCTSTR lpszDescr = NULL);
CUserPropertyGridFile(const CString& strName, BOOL bOpenFileDialog, const CString& strFileName, LPCTSTR lpszDefExt = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, LPCTSTR lpszDescr = NULL, DWORD_PTR dwData = 0);
~CUserPropertyGridFile();
public:
void OnClickButton( CPoint point );
void OnClickButtonFolder( CPoint point );
void SetDefaultPath( const char* szDefaultPath );
public:
char m_szDefaultPath[256];
};
//CustomProperty.cpp
#include "stdafx.h"
#include "CustomProperty.h"
#include <string>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//using namespace std;
////////////////////////////////////////////////////////////////////////////////
// CCheckBoxProp class
void CCheckBoxProp::OnDrawName(CDC* pDC, CRect rect)
{
m_rectCheck = rect;
m_rectCheck.DeflateRect(1, 1);
m_rectCheck.right = m_rectCheck.left + m_rectCheck.Height();
rect.left = m_rectCheck.right + 1;
CMFCPropertyGridProperty::OnDrawName(pDC, rect);
OnDrawCheckBox(pDC, m_rectCheck, (m_varValue.boolVal));
}
void CCheckBoxProp::OnClickName(CPoint point)
{
if (m_bEnabled && m_rectCheck.PtInRect(point))
{
m_varValue.boolVal = !(m_varValue.boolVal);
m_pWndList->InvalidateRect(m_rectCheck);
}
}
BOOL CCheckBoxProp::OnDblClk(CPoint point)
{
if (m_bEnabled && m_rectCheck.PtInRect(point))
{
return TRUE;
}
m_varValue.boolVal = !(m_varValue.boolVal);
m_pWndList->InvalidateRect(m_rectCheck);
return TRUE;
}
void CCheckBoxProp::OnDrawCheckBox(CDC * pDC, CRect rect, BOOL bChecked)
{
COLORREF clrTextOld = pDC->GetTextColor();
CMFCVisualManager::GetInstance()->OnDrawCheckBox(pDC, rect, FALSE, bChecked, m_bEnabled);
pDC->SetTextColor(clrTextOld);
}
BOOL CCheckBoxProp::PushChar(UINT nChar)
{
if (nChar == VK_SPACE)
{
OnDblClk(CPoint(-1, -1));
}
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////
// CPropCheckBoxCtrl class
void CPropCheckBoxCtrl::OnDrawName(CDC* pDC, CRect rect)
{
m_rectCheck = rect;
m_rectCheck.DeflateRect(1, 1);
m_rectCheck.right = m_rectCheck.left + m_rectCheck.Height();
rect.left = m_rectCheck.right + 1;
CMFCPropertyGridProperty::OnDrawName(pDC, rect);
OnDrawCheckBox(pDC, m_rectCheck, (m_varValue));
}
void CPropCheckBoxCtrl::OnClickName(CPoint point)
{
if (m_bEnabled && m_rectCheck.PtInRect(point))
{
if(isBRadioMode==FALSE)
{
m_varValue = !(m_varValue);
}
m_pWndList->InvalidateRect(m_rectCheck);
}
try
{
CPropCheckRadioBoxCtrl * parent ;
if(parent = dynamic_cast<CPropCheckRadioBoxCtrl *> (this->m_pParent)) // can casting inheritance?
{
}
else
{
throw -1;
}
if(parent->m_pChildRadioBool.GetCount() > 0) // null check
{
parent->RadioCheck(&m_varValue);
m_pWndList->InvalidateRect(m_rectCheck);
if(isBRadioMode == FALSE)
{
isBRadioMode = TRUE;
}
}
}
catch(...)//all exception
{
//error casting or error pointing
}
}
void CPropCheckBoxCtrl::SetCheck(BOOL bCheck)
{
m_varValue = bCheck;
try
{
CPropCheckRadioBoxCtrl * parent ;
if(parent = dynamic_cast<CPropCheckRadioBoxCtrl *> (this->m_pParent))
{
}
else
{
throw -1;
}
if(parent->m_pChildRadioBool.GetCount() > 0)
{
parent->RadioCheck(&m_varValue);
m_pWndList->InvalidateRect(m_rectCheck);
}
}
catch(...)
{ }
}
BOOL CPropCheckBoxCtrl::OnDblClk(CPoint point)
{
return TRUE;
}
void CPropCheckBoxCtrl::OnDrawCheckBox(CDC * pDC, CRect rect, BOOL bChecked)
{
COLORREF clrTextOld = pDC->GetTextColor();
CMFCVisualManager::GetInstance()->OnDrawCheckBox(pDC, rect, FALSE, bChecked, m_bEnabled);
pDC->SetTextColor(clrTextOld);
}
BOOL CPropCheckBoxCtrl::PushChar(UINT nChar)
{
if (nChar == VK_SPACE)
{
OnDblClk(CPoint(-1, -1));
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// CPropCheckRadioBoxCtrl class
BOOL CPropCheckRadioBoxCtrl::AddSubItem(CMFCPropertyGridProperty * pChild)
{
BOOL isbTrue = CMFCPropertyGridProperty::AddSubItem(pChild); // pre precedure
CPropCheckBoxCtrl * pChildCheckBox = (CPropCheckBoxCtrl*) pChild;
m_pChildRadioBool.Add(&(pChildCheckBox->m_varValue));
*m_pChildRadioBool[0] =TRUE; //init first element radio
return isbTrue;
}
void CPropCheckRadioBoxCtrl::RadioCheck(BOOL * CallRadio)
{
for(int i = 0 ;i < m_pChildRadioBool.GetCount(); i++)
{
if(m_pChildRadioBool[i] != CallRadio)
{
*m_pChildRadioBool[i] = FALSE;
}
}
*CallRadio = TRUE;
InvalidateRect(0,0,1);
}
///////////////////////////////////////////////////////////////////////////////
// CUserPropertyGridFile class
IMPLEMENT_DYNAMIC( CUserPropertyGridFile, CMFCPropertyGridFileProperty )
CUserPropertyGridFile::CUserPropertyGridFile(const CString& strName, BOOL bOpenFileDialog, const CString& strFileName, LPCTSTR lpszDefExt, DWORD dwFlags, LPCTSTR lpszFilter, LPCTSTR lpszDescr, DWORD_PTR dwData )
: CMFCPropertyGridFileProperty( strName, bOpenFileDialog, strFileName, lpszDefExt, dwFlags, lpszFilter, lpszDescr, dwData )
{
m_bIsFolder = FALSE;
}
CUserPropertyGridFile::~CUserPropertyGridFile()
{
}
void CUserPropertyGridFile::SetDefaultPath( const char* szDefaultPath )
{
m_strDefExt = szDefaultPath;
}
void CUserPropertyGridFile::OnClickButton( CPoint point )
{
char programPath[_MAX_PATH];
GetCurrentDirectory(_MAX_PATH, programPath);
CString tmpStr = programPath + m_strDefExt;
const char* szDefaultPath = tmpStr.GetBuffer(0);
// Open file option
DWORD dwFlags = m_dwFileOpenFlags | OFN_NOCHANGEDIR;
// File open dialog
CFileDialog dlgOpen( TRUE , m_strDefExt, NULL, dwFlags, m_strFilter );
dlgOpen.m_ofn.lpstrInitialDir = szDefaultPath;
if( dlgOpen.DoModal() != 1 ) {
return ;
}
// Selected file name
CString sFile = dlgOpen.GetPathName();
const char* szFullFile = sFile.GetBuffer(0);
// Remove file name
CString selFile = (CString)szFullFile;
CString selPath = selFile.Left(selFile.ReverseFind('\\'));
for( int i = 0 ; i < (int)strlen( szDefaultPath ) ; i++ ){
if( tolower( szDefaultPath[i] ) != tolower( selPath.GetAt(i) ) ) {
AfxMessageBox(_T("Select file in Config folder"), MB_OK | MB_ICONSTOP, 0);
return ;
}
}
int nDefaultLen = strlen( szDefaultPath );
if( szDefaultPath[nDefaultLen-1] == '\\' ) {
nDefaultLen--;
}
char szFile[256];
strcpy( szFile, &szFullFile[nDefaultLen+1] );
//SetValue( szFile );
CString strFileOnly = (CString)szFile;
strFileOnly = strFileOnly.Left(strFileOnly.ReverseFind('.'));
SetValue( strFileOnly );
}
//사용법
1.checkbox쓰고싶으면 checkbox클래스 생성해서 사용하면된다
2.radio를사용하고싶으면 radio클래스를 만든다음 그아래에 checkbox를 2개이상 자식으로 등록하면 자동으로 radio기능이된다 :D
3.체크여부는 클래스안에있는 bool값을사용하면 되며 기본 클래스에 add할경우 dynamic_cast<타입> (변환대상) 으로 리턴받아서 사용하면된다.
'C++' 카테고리의 다른 글
윈도우와 리눅스에서 MacAddress가져오기 (0) | 2017.06.01 |
---|---|
hexa데이터를 string으로 변환 (0) | 2017.05.26 |
옛날에 만든 Hex Stream 으로 wireshark세이브 파일 포맷로그저장하는 클래스! (0) | 2016.12.15 |
로컬의 Mac값 알아내기 +_+ (0) | 2016.12.15 |
Raw소켓짤때 헤더(Header)와 프로토콜(Protocol) 작성규칙 (0) | 2016.12.15 |