www.pudn.com > ToolBar_ATL.rar > KToolBarPpgGeneral.h
// KToolBarPpgGeneral.h : Declaration of the CKToolBarPpgGeneral
#ifndef __KTOOLBARPPGGENERAL_H_
#define __KTOOLBARPPGGENERAL_H_
#include "resource.h" // main symbols
#include "KToolBarCP.h"
#include
#include
#include "picholder.h"
#include "KToolBar.h"
#include "typedef.h"
using namespace std;
EXTERN_C const CLSID CLSID_KToolBarPpgGeneral;
/////////////////////////////////////////////////////////////////////////////
// CKToolBarPpgGeneral
class ATL_NO_VTABLE CKToolBarPpgGeneral :
public CComObjectRootEx,
public CComCoClass,
public IPropertyPageImpl,
public CDialogImpl
{
public:
~CKToolBarPpgGeneral();
CKToolBarPpgGeneral()
{
m_dwTitleID = IDS_TITLEToolBarPpgGeneral;
m_dwHelpFileID = IDS_HELPFILEToolBarPpgGeneral;
m_dwDocStringID = IDS_DOCSTRINGToolBarPpgGeneral;
m_BtnNew.Enabled = TRUE;
m_BtnNew.Icon = NULL;
m_BtnNew.Style = BTN_ICON;
m_BtnNew.Key = NULL;
m_BtnNew.ToolTips = NULL;
m_bAppEnabled = TRUE;
}
enum {IDD = IDD_TOOLBARPPGGENERAL};
DECLARE_REGISTRY_RESOURCEID(IDR_TOOLBARPPGGENERAL)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(CKToolBarPpgGeneral)
COM_INTERFACE_ENTRY(IPropertyPage)
END_COM_MAP()
BEGIN_MSG_MAP(CKToolBarPpgGeneral)
CHAIN_MSG_MAP(IPropertyPageImpl)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_HANDLER(IDC_PREVBTN, BN_CLICKED, OnClickedPrevbtn)
COMMAND_HANDLER(IDC_NEXTBTN, BN_CLICKED, OnClickedNextbtn)
COMMAND_HANDLER(IDC_COMBOBTNSTYLE, CBN_SELCHANGE, OnSelchangeCombobtnstyle)
COMMAND_HANDLER(IDC_RADIOOFF, BN_CLICKED, OnClickedRadiooff)
COMMAND_HANDLER(IDC_RADIOON, BN_CLICKED, OnClickedRadioon)
COMMAND_HANDLER(IDC_BROWSEBTN, BN_CLICKED, OnClickedBrowsebtn)
COMMAND_HANDLER(IDC_BTNDEL, BN_CLICKED, OnClickedBtndel)
COMMAND_HANDLER(IDC_BTNINSERT, BN_CLICKED, OnClickedBtninsert)
COMMAND_HANDLER(IDC_BTNDEL, BN_CLICKED, OnClickedBtndel)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
COMMAND_HANDLER(IDC_DELICON, BN_CLICKED, OnClickedDelIcon)
COMMAND_HANDLER(IDC_KEY, EN_CHANGE, OnChangeKey)
COMMAND_HANDLER(IDC_TOOLTIPS, EN_CHANGE, OnChangeToolTips)
COMMAND_HANDLER(IDC_KEY, EN_KILLFOCUS, OnKillfocusKey)
COMMAND_HANDLER(IDC_TOOLTIPS, EN_KILLFOCUS, OnKillfocusTooltips)
END_MSG_MAP()
// Handler prototypes:
// LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
// LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
// LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
STDMETHOD(Apply)(void)
{
ATLTRACE(_T("CKToolBarPpgGeneral::Apply\n"));
if(this->m_bDirty == FALSE)return S_OK;
int i,j;
BSTR bStr;
for (i = 0; i < m_nObjects; i++)
{
// Do something interesting here
IKToolBarCtrl* pIKBar;
m_ppUnk[i]->QueryInterface(IID_IKToolBarCtrl, (void**)&pIKBar);
pIKBar->put_BtnCnt(m_nBtnCnt);
if(m_nBtnCnt>0){
list::iterator iterRead;
iterRead=m_listBtns.begin();
for(j=0;jput_BtnIcon(j,iterRead->Icon);
pIKBar->put_BtnStyle(j,iterRead->Style);
pIKBar->put_BtnEnabled(j,iterRead->Enabled);
if(iterRead->ToolTips){
bStr = ConvertLPSTRToBSTR(iterRead->ToolTips);
pIKBar->put_BtnToolTips(j,bStr);
::SysFreeString(bStr);
}else{
pIKBar->put_BtnToolTips(j,NULL);
}
if(iterRead->Key){
bStr = ConvertLPSTRToBSTR(iterRead->Key);
pIKBar->put_BtnKey(j,bStr);
::SysFreeString(bStr);
}else{
pIKBar->put_BtnKey(j,NULL);
}
iterRead++;
}
}
pIKBar->Release();
}
SetDirty(FALSE);
return S_OK;
}
STDMETHOD(Show)( UINT nCmdShow )
{
if(nCmdShow == SW_SHOW ||
nCmdShow == SW_SHOWNORMAL)
{
}
HRESULT hr = IPropertyPageImpl::Show(nCmdShow);
return hr;
}
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// TODO : Add Code for message handler. Call DefWindowProc if necessary.
int i,j;
HWND hWnd;
m_bAppEnabled = FALSE;
//默认为第1个按钮
m_CurBtnIndex = 0;
m_listPicture.clear();
hWnd = GetDlgItem(IDC_COMBOBTNSTYLE);
SendMessage(hWnd,CB_RESETCONTENT,0,0);
SendMessage(hWnd,CB_ADDSTRING,0,(DWORD)(LPCSTR)"Icon");
SendMessage(hWnd,CB_ADDSTRING,0,(DWORD)(LPCSTR)"Seperator");
SendMessage(hWnd,CB_SETCURSEL,0,0);
//默认Enable
SendMessage(GetDlgItem(IDC_RADIOON),BM_SETCHECK,1,0);
//设置当前按钮设置
::EnableWindow(GetDlgItem(IDC_PREVBTN),FALSE);
//获取Button存储
for (i = 0; i < m_nObjects; i++)
{
// Do something interesting here
IKToolBarCtrl* pIKBar;
m_ppUnk[i]->QueryInterface(IID_IKToolBarCtrl, (void**)&pIKBar);
BOOL BtnEnable;
BTNSTYLE BtnStyle;
BSTR bStr;
//首先获得按钮数
pIKBar->get_BtnCnt(&m_nBtnCnt);
m_listBtns.clear();
for(j=0;jget_BtnIcon(j,&m_SwapPic);
m_picHolder.SetPictureDispatch(m_SwapPic);
m_SwapBtn.Icon = m_picHolder.GetPictureDispatch();
pIKBar->get_BtnEnabled(j, &BtnEnable);
m_SwapBtn.Enabled = BtnEnable;
pIKBar->get_BtnStyle(j, &BtnStyle);
m_SwapBtn.Style = BtnStyle;
pIKBar->get_BtnToolTips(j,&bStr);
m_SwapBtn.ToolTips = ConvertBSTRToLPSTR(bStr);
::SysFreeString(bStr);
pIKBar->get_BtnKey(j,&bStr);
m_SwapBtn.Key = ConvertBSTRToLPSTR(bStr);
::SysFreeString(bStr);
m_listBtns.push_back(m_SwapBtn);
//默认显示第1张图片
if(j==0){
DrawBtn(m_SwapBtn.Icon);
SendMessage(GetDlgItem(IDC_COMBOBTNSTYLE),CB_SETCURSEL,BtnStyle,0);
if(BtnEnable == TRUE){
SendMessage(GetDlgItem(IDC_RADIOON),BM_SETCHECK,1,0);
SendMessage(GetDlgItem(IDC_RADIOOFF),BM_SETCHECK,0,0);
}else{
SendMessage(GetDlgItem(IDC_RADIOON),BM_SETCHECK,0,0);
SendMessage(GetDlgItem(IDC_RADIOOFF),BM_SETCHECK,1,0);
}
SetDlgItemText(IDC_TOOLTIPS, m_SwapBtn.ToolTips);
SetDlgItemText(IDC_KEY, m_SwapBtn.Key);
}
}
//If there is less than 1 button, Disable the next button.
if(m_nBtnCnt <= 1){
::EnableWindow(GetDlgItem(IDC_NEXTBTN),FALSE);
//If there is no buttons, disable the setting units.
if(m_nBtnCnt == 0){
::EnableWindow(GetDlgItem(IDC_COMBOBTNSTYLE),FALSE);
::EnableWindow(GetDlgItem(IDC_RADIOON),FALSE);
::EnableWindow(GetDlgItem(IDC_RADIOOFF),FALSE);
::EnableWindow(GetDlgItem(IDC_BROWSEBTN),FALSE);
::EnableWindow(GetDlgItem(IDC_BTNDEL),FALSE);
::EnableWindow(GetDlgItem(IDC_DELICON),FALSE);
::EnableWindow(GetDlgItem(IDC_TOOLTIPS),FALSE);
::EnableWindow(GetDlgItem(IDC_KEY),FALSE);
}
}
if(m_nBtnCnt == 0){
SetDlgItemInt(IDC_CURBTN,0,FALSE);
}else{
SetDlgItemInt(IDC_CURBTN,m_CurBtnIndex+1,FALSE);
}
SetDlgItemInt(IDC_BTN_TOTAL,m_nBtnCnt,FALSE);
m_iterBtn = m_listBtns.begin();
pIKBar->Release();
}
m_bAppEnabled = TRUE;
return 0;
}
public :
BEGIN_CONNECTION_POINT_MAP(CKToolBarPpgGeneral)
END_CONNECTION_POINT_MAP()
LRESULT OnClickedPrevbtn(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
m_bAppEnabled = FALSE;
m_CurBtnIndex--;
m_iterBtn--;
::EnableWindow(GetDlgItem(IDC_NEXTBTN),TRUE);
//Disable the Previous button when arrived the first button
if(m_CurBtnIndex <= 0){
::EnableWindow(hWndCtl,FALSE);
m_CurBtnIndex = 0;
}
RefreshBtnStatus();
SetDlgItemInt(IDC_CURBTN,m_CurBtnIndex+1,FALSE);
m_bAppEnabled = TRUE;
return 0;
}
LRESULT OnClickedNextbtn(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
m_bAppEnabled = FALSE;
m_CurBtnIndex++;
m_iterBtn++;
::EnableWindow(GetDlgItem(IDC_PREVBTN),TRUE);
//Disable the next button when arrived the last button
if(m_CurBtnIndex >= m_nBtnCnt-1){
::EnableWindow(hWndCtl,FALSE);
m_CurBtnIndex = m_nBtnCnt-1;
}
RefreshBtnStatus();
SetDlgItemInt(IDC_CURBTN,m_CurBtnIndex+1,FALSE);
m_bAppEnabled = TRUE;
return 0;
}
private:
void RefreshBtnStatus();
void DrawBtn(LPPICTUREDISP picDisp);
void CheckLR();
void CheckBtns();
HWND m_picHwnd;
CPictureHolder m_picHolder;
int m_CurBtnIndex;
list m_listPicture;
short m_nBtnCnt;
list m_listBtns;
list::iterator m_iterBtn;
LPPICTUREDISP m_SwapPic;
TBItem m_SwapBtn;
TBItem m_BtnNew;
BOOL m_bAppEnabled;
LRESULT OnSelchangeCombobtnstyle(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
LONG nCurSel;
nCurSel = SendMessage(hWndCtl,CB_GETCURSEL,0,0);
switch(nCurSel) {
case 0:
m_iterBtn->Style = BTN_ICON;
break;
case 1:
m_iterBtn->Style = BTN_SEPARATOR;
break;
default:
m_iterBtn->Style = BTN_ICON;
;
}
this->SetDirty(TRUE);
return 0;
}
LRESULT OnClickedRadiooff(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
SendMessage(GetDlgItem(IDC_RADIOON),BM_SETCHECK,0,0);
m_iterBtn->Enabled = FALSE;
SetDirty(TRUE);
return 0;
}
LRESULT OnClickedRadioon(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
SendMessage(GetDlgItem(IDC_RADIOOFF),BM_SETCHECK,0,0);
m_iterBtn->Enabled = TRUE;
SetDirty(TRUE);
return 0;
}
LRESULT OnClickedBrowsebtn(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
OPENFILENAME ofn;
//This must be done
memset(&ofn, 0, sizeof(ofn));
char strFile[256];
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFilter = "Icon File(*.ico)\0*.ico\0\0";
ofn.nMaxFile = sizeof(strFile);
ofn.lpstrFile = strFile;
ofn.lpstrFile[0] = '\0'; //This must be done.
ofn.lpstrFileTitle = "Open Icon File";
ofn.nFilterIndex = 1;
ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
if(::GetOpenFileName(&ofn) != 0){
//HICON aIcon=NULL;
//aIcon = LoadIcon(_Module.GetResourceInstance(),ofn.lpstrFile);
FILE* hFile;
int cbSize;
int readSize=0;
hFile = fopen(ofn.lpstrFile,"rb");
//Get the picture size.
fseek(hFile,0,SEEK_END);
cbSize = ftell(hFile);
if(cbSize>0){
HGLOBAL hGlobal = ::GlobalAlloc(GMEM_MOVEABLE, cbSize);
LPVOID lpBuf = ::GlobalLock( hGlobal );
IStream* stmTestLen=NULL;
IPictureDisp *lpPicture=NULL;
//Read picture data.
fseek(hFile,0,SEEK_SET);
readSize = fread(lpBuf,sizeof(char),cbSize,hFile);
::GlobalUnlock(hGlobal);
fclose(hFile);
HRESULT hr = ::CreateStreamOnHGlobal(hGlobal,TRUE,&stmTestLen);
if(SUCCEEDED(hr)){
//The third parameter must set to FALSE to keep origin format of picture.
hr = OleLoadPicture(stmTestLen,cbSize,FALSE,IID_IPictureDisp, (LPVOID*)&lpPicture);
if(SUCCEEDED(hr)){
m_picHolder.SetPictureDispatch(lpPicture);
//HICON hIcon=NULL;
m_iterBtn->Icon = m_picHolder.GetPictureDispatch();
HWND hWndPic;
hWndPic = GetDlgItem(IDC_BTNPIC);
RECT rectPic = {0,0,16,16};
//Clear previous icon.
::SendMessage(hWndPic,STM_SETICON,0,0);
//Draw new icon.
HDC hDC = ::GetDC(hWndPic);
m_picHolder.Render(hDC,rectPic,rectPic);
::ReleaseDC(hWndPic,hDC);
}
}
GlobalFree(hGlobal);
}
//aIcon = (HICON)LoadImage(NULL,ofn.lpstrFile,IMAGE_ICON,0,0,LR_LOADFROMFILE);
//m_picHolder.CreateFromIcon(aIcon);
//DestroyIcon(aIcon);
}
SetDirty(TRUE);
return S_OK;
}
LRESULT OnClickedBtninsert(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
//
list::iterator iterInsert;
m_nBtnCnt++;
//If insert a button after the last button.
if(m_nBtnCnt == 1 || m_nBtnCnt == m_CurBtnIndex-1){
m_listBtns.push_back(m_BtnNew);
if(m_nBtnCnt == 1){
m_iterBtn = m_listBtns.begin();
}else{
m_iterBtn++;
}
}else{
m_CurBtnIndex++;
iterInsert = m_iterBtn;
//Insert a button after current button.
m_listBtns.insert(++m_iterBtn,m_BtnNew);
m_iterBtn--;
}
//Switch to the new button
SetDlgItemInt(IDC_CURBTN,m_CurBtnIndex+1,FALSE);
//Set total count of buttons.
SetDlgItemInt(IDC_BTN_TOTAL,m_nBtnCnt,FALSE);
//Set the Style is: Icon
SendMessage(GetDlgItem(IDC_COMBOBTNSTYLE),CB_SETCURSEL,0,0);
//Enable the new button
SendMessage(GetDlgItem(IDC_RADIOON),BM_SETCHECK,1,0);
SendMessage(GetDlgItem(IDC_RADIOOFF),BM_SETCHECK,0,0);
//Clear previous button.
SendMessage(GetDlgItem(IDC_BTNPIC),STM_SETICON,NULL,0);
SetDlgItemText(IDC_TOOLTIPS,NULL);
SetDlgItemText(IDC_KEY,NULL);
CheckBtns();
SetDirty(TRUE);
return 0;
}
LRESULT OnClickedBtndel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
list::iterator aIterDel;
aIterDel = m_iterBtn;
//If current button is not the last button, skip to next button
if(m_CurBtnIndex != m_nBtnCnt-1){
m_iterBtn++;
//If current button is the last button, skip to previous button
}else if(m_CurBtnIndex != 0){
m_iterBtn--;
m_CurBtnIndex--;
//If there is no button exist, clear current button icon holder.
}else{
m_iterBtn->Icon = NULL;
}
//Clear current button in the list.
m_listBtns.erase(aIterDel);
m_nBtnCnt--;
CheckBtns();
SetDlgItemInt(IDC_BTN_TOTAL,m_nBtnCnt,FALSE);
if(m_nBtnCnt == 0){
SetDlgItemInt(IDC_CURBTN,0,FALSE);
}else{
SetDlgItemInt(IDC_CURBTN,m_CurBtnIndex+1,FALSE);
}
RefreshBtnStatus();
SetDirty(TRUE);
return 0;
}
LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// TODO : Add Code for message handler. Call DefWindowProc if necessary.
if(m_iterBtn->Icon != NULL){
DrawBtn(m_iterBtn->Icon);
}
bHandled = TRUE;
::DefWindowProc(m_hWnd,uMsg,wParam,lParam);
return 0;
}
LRESULT OnClickedDelIcon(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
m_iterBtn->Icon->Release();
m_iterBtn->Icon = NULL;
DrawBtn(NULL);
return 0;
}
LRESULT OnChangeKey(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
if(m_bAppEnabled == TRUE){
SetDirty(TRUE);
}
return 0;
}
LRESULT OnChangeToolTips(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
if(m_bAppEnabled == TRUE){
SetDirty(TRUE);
}
return 0;
}
LRESULT OnKillfocusKey(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
if(m_iterBtn->Key){
delete[] m_iterBtn->Key;
}
BSTR bSTR = ::SysAllocStringLen(NULL, 100);
GetDlgItemText(IDC_KEY, bSTR);
if(::SysStringLen(bSTR) > 0){
m_iterBtn->Key = ConvertBSTRToLPSTR(bSTR);
}else{
m_iterBtn->Key = NULL;
}
::SysFreeString(bSTR);
return 0;
}
LRESULT OnKillfocusTooltips(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// TODO : Add Code for control notification handler.
if(m_iterBtn->ToolTips){
delete[] m_iterBtn->ToolTips;
}
BSTR bSTR = ::SysAllocStringLen(NULL, 100);
GetDlgItemText(IDC_TOOLTIPS, bSTR);
if(::SysStringLen(bSTR) > 0){
m_iterBtn->ToolTips = ConvertBSTRToLPSTR(bSTR);
}else{
m_iterBtn->ToolTips = NULL;
}
::SysFreeString(bSTR);
return 0;
}
};
#endif //__TOOLBARPPGGENERAL_H_