Бывалый

Профиль
Группа: Участник
Сообщений: 171
Регистрация: 2.5.2009
Репутация: нет Всего: нет
|
Когда то писал.... ToolBar.h | Код | /********************************************************************************************************* File: ToolBar.h Logic: Implementation ToolBar Module: GUI(Graphic User Interface) Author: Dr.Hex *********************************************************************************************************/ #ifndef _TOOL_BAR_ #define _TOOL_BAR_ #include "windows.h" #include <commctrl.h>
namespace IceGui { class IcePopMenu {};
/* Data of data */ class ToolBarButtonData {}; //position of ToolBar namespace ToolBarPosition { enum enumToolBarPosition { Unknown = 0, Top = CCS_TOP, Left = CCS_LEFT, Bottom = CCS_BOTTOM, Right = CCS_RIGHT }; }
/* Present ToolBar object */ class ToolBar { private: HINSTANCE m_hInstance; HWND m_hWnd; HWND m_hWndParent; HWND m_hWndToolTip;
HIMAGELIST hImgList; HIMAGELIST hImgListDisabled; HIMAGELIST hImgListHot;
public: /* Initialize toolbar - create window, create list image and etc. */ bool Initialize(HINSTANCE hInstance, HWND hParent, unsigned int dwIconWidth, unsigned int dwIconHeight, unsigned int ToolBarImageSize, unsigned int toolBarImageGrowSize);
/* Information about tool bar */ HWND GetHwnd(); //hwnd of ToolBar
HWND GetHwndParent();//hwnd parent
int GetButtonCount(); // return count button on the ToolBar
BOOL EnableDivider(BOOL bEnable);
BOOL EnableParentAlign(BOOL bEnable);
BOOL SetDirection(BOOL isHorizontal); //set direction horizontal or vertical
BOOL GetDirectionIsHorizontal(); //get directory, if true directory is horizontal, else vertical
BOOL SetPosition(ToolBarPosition::enumToolBarPosition position ); //set position
ToolBarPosition::enumToolBarPosition GetPosition();//get position
BOOL GetRect(RECT* rect); BOOL Move(int X, int Y, int nWidth, int nHeight, bool bRepaint);
BOOL SetWindowPos(int X, int Y, int nWidth, int nHeight);
void RedrawWindow();
BOOL UpdateWindow();
/* Add buttons */ BOOL AddButton(int buttonId, TCHAR* textButton, TCHAR* ToolTip);
BOOL AddButton(int buttonId, int iconId, TCHAR* ToolTip);
BOOL AddButton(int buttonId, char* buttonText, BYTE buttonStyle, BYTE buttonState, int IconEnableId, int iconDisableId, int iconHotId, TCHAR* ToolTip);
BOOL AddButton(int buttonId, char* buttonText, BYTE buttonStyle, BYTE buttonState, int IconEnableId, int iconDisableId, int iconHotId, TCHAR* ToolTip, IcePopMenu* popMenu);
/* Add separator to tool bar */ void AddSeparator(); /* Change property for button */ BOOL EnableButton(int buttonId, BOOL bEnable);
BOOL SetButtonText(int buttonId, TCHAR* buttonText);
BOOL SetButtonState(int buttonId, BYTE buttonState);
BYTE GetButtonState(int buttonId);
BYTE GetButtonStyle(int buttonId);
/* Events */ BOOL OnNotify(WPARAM wParam, LPARAM lParam);
/* Destructor, free memory/resource */ /*virtual*/ ~ToolBar(); private: void FreeImageLists(); }; }
#endif
|
ToolBar.cpp | Код | #include "stdafx.h" #include "ToolBar.h" #define Toolbar_MARKER_PROP_NAME (L"CToolbarMarkerPropName")
/* Initialize toolbar, CreateWindow, create image list, and etc. */ bool IceGui::ToolBar::Initialize(HINSTANCE hInstance, HWND hParent, unsigned int dwIconWidth, unsigned int dwIconHeight, unsigned int toolBarImageSize, unsigned int toolBarImageGrowSize) { m_hInstance = hInstance; m_hWndParent = hParent;
DWORD dwStyle = WS_CHILD | TBSTYLE_TOOLTIPS | WS_VISIBLE | TBSTYLE_LIST | TBSTYLE_FLAT; m_hWnd = CreateWindowExW(0, TOOLBARCLASSNAME, (LPTSTR) NULL, dwStyle, 0, // x 0, // y 0, // width 0, // height m_hWndParent, (HMENU) NULL, m_hInstance, NULL);
SendMessage(m_hWnd, TB_SETEXTENDEDSTYLE, 0, (LPARAM)TBSTYLE_EX_DRAWDDARROWS); SendMessage(m_hWnd, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
if (dwIconWidth && dwIconHeight) { SendMessage((HWND) m_hWnd, (UINT) TB_SETBITMAPSIZE, (WPARAM) 0,//not used, must be zero (LPARAM) MAKELONG (dwIconWidth, dwIconHeight)); } else { /* "If an application does not explicitly set the bitmap size, the size defaults to 16 by 15 pixels." (MSDN) */ dwIconWidth = 16; dwIconHeight = 15; }
hImgList = ImageList_Create(dwIconWidth, dwIconHeight, ILC_MASK|ILC_COLOR32, toolBarImageSize, toolBarImageGrowSize);
hImgListDisabled = ImageList_Create(dwIconWidth, dwIconHeight, ILC_MASK|ILC_COLOR32, toolBarImageSize, toolBarImageGrowSize);
hImgListHot = ImageList_Create(dwIconWidth, dwIconHeight, ILC_MASK|ILC_COLOR32, toolBarImageSize, toolBarImageGrowSize);
SendMessage(m_hWnd, TB_SETIMAGELIST, 0, (LPARAM)hImgList);
SendMessage(m_hWnd, TB_SETDISABLEDIMAGELIST, 0, (LPARAM)hImgListDisabled);
SendMessage(m_hWnd, TB_SETHOTIMAGELIST, 0, (LPARAM)hImgListHot);
m_hWndToolTip = (HWND)SendMessage(m_hWnd, TB_GETTOOLTIPS, 0, 0);
SendMessage(m_hWnd, TB_AUTOSIZE, 0, 0);
SetProp(m_hWnd, Toolbar_MARKER_PROP_NAME, (HANDLE)this);
return true; }
BOOL IceGui::ToolBar::EnableDivider(BOOL bEnable) { BOOL bRet; LONG style; style = GetWindowLongPtr(m_hWnd,GWL_STYLE); if (bEnable) style &= ~CCS_NODIVIDER; else style |= CCS_NODIVIDER;
// apply new style bRet = (BOOL)SetWindowLongPtr(m_hWnd, GWL_STYLE, style);
// force item to be redrawn ::RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN);
return bRet; }
BOOL IceGui::ToolBar::EnableParentAlign(BOOL bEnable) { BOOL bRet; LONG style; style = GetWindowLongPtr(m_hWnd, GWL_STYLE); if (bEnable) style &= ~CCS_NOPARENTALIGN; else style |= CCS_NOPARENTALIGN;
// apply new style bRet = SetWindowLongPtr(m_hWnd, GWL_STYLE, style);
// force item to be redrawn RedrawWindow(); return bRet; }
HWND IceGui::ToolBar::GetHwnd() { return m_hWnd; }
HWND IceGui::ToolBar::GetHwndParent() { return m_hWndParent; }
int IceGui::ToolBar::GetButtonCount() { return (int)SendMessage(m_hWnd, (UINT)TB_BUTTONCOUNT, 0, 0); }
void IceGui::ToolBar::RedrawWindow() { ::RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN); }
BOOL IceGui::ToolBar::SetWindowPos(int X, int Y, int nWidth, int nHeight) { return ::SetWindowPos(m_hWnd, HWND_BOTTOM, X, Y, nWidth, nHeight, SWP_SHOWWINDOW); }
BOOL IceGui::ToolBar::GetRect(RECT* rect) { return GetWindowRect(m_hWnd, rect); }
BOOL IceGui::ToolBar::Move(int X, int Y, int nWidth, int nHeight, bool bRepaint) { return MoveWindow(m_hWnd, X, Y, nWidth, nHeight, bRepaint); }
BOOL IceGui::ToolBar::UpdateWindow() { return ::UpdateWindow(m_hWnd); }
BOOL IceGui::ToolBar::SetDirection(BOOL isHorizontal) { BOOL bRet; LONG style;
style = GetWindowLongPtr(m_hWnd, GWL_STYLE); if (isHorizontal) style &= ~CCS_VERT; else style |= CCS_VERT;
bRet = SetWindowLongPtr(m_hWnd, GWL_STYLE, style);
RedrawWindow();
return bRet; }
BOOL IceGui::ToolBar::GetDirectionIsHorizontal() { LONG_PTR style;
style = GetWindowLongPtr(m_hWnd, GWL_STYLE); if(style | CCS_VERT) return FALSE;
return TRUE; }
BOOL IceGui::ToolBar::SetPosition(ToolBarPosition::enumToolBarPosition position) { BOOL bRet; LONG style;
//create style for position style = GetWindowLongPtr(m_hWnd, GWL_STYLE); style &= ~(CCS_TOP | CCS_LEFT | CCS_BOTTOM | CCS_RIGHT); style |= (LONG)position;
//apply bRet = SetWindowLongPtr(m_hWnd, GWL_STYLE, style);
//update RedrawWindow();
return bRet; }
IceGui::ToolBarPosition::enumToolBarPosition IceGui::ToolBar::GetPosition() { return (IceGui::ToolBarPosition::enumToolBarPosition)GetWindowLongPtr(m_hWnd, GWL_STYLE); }
BOOL IceGui::ToolBar::AddButton(int buttonId, TCHAR* textButton , TCHAR* ToolTip) { TBBUTTON new_button ={0};
// add button to toolbar new_button.iBitmap = I_IMAGENONE; new_button.idCommand = buttonId; new_button.fsState = TBSTATE_ENABLED; new_button.fsStyle = BTNS_SHOWTEXT | BTNS_AUTOSIZE; new_button.iString = (INT_PTR)textButton;
// store tooltip new_button.dwData = (DWORD_PTR)new ToolBarButtonData();
//send add button if (!SendMessage(m_hWnd, TB_ADDBUTTONS, (WPARAM) 1, (LPARAM) (LPTBBUTTON) &new_button)) return FALSE;
// resize toolbar if required SendMessage(m_hWnd, TB_AUTOSIZE, 0, 0);
return TRUE; }
BOOL IceGui::ToolBar::AddButton(int buttonId, int iconId, TCHAR* ToolTip) { return AddButton(buttonId, NULL, BTNS_BUTTON|BTNS_AUTOSIZE, TBSTATE_ENABLED, iconId, iconId, iconId, ToolTip); }
BOOL IceGui::ToolBar::AddButton(int buttonId, char* buttonText, BYTE buttonStyle, BYTE buttonState, int iconEnableId, int iconDisableId, int iconHotId, TCHAR* ToolTip) { return AddButton(buttonId, NULL, BTNS_BUTTON|BTNS_AUTOSIZE, TBSTATE_ENABLED, iconEnableId, iconDisableId, iconHotId, ToolTip, NULL); }
BOOL IceGui::ToolBar::AddButton(int buttonId, char* buttonText, BYTE buttonStyle, BYTE buttonState, int iconEnableId, int iconDisableId, int iconHotId, TCHAR* ToolTip, IcePopMenu* popMenu) { TBBUTTON button = {0}; int iIconEnable; int iIconDisable; int iIconHot; HICON hicon;
// load enable icon hicon = (HICON)LoadImage(m_hInstance, MAKEINTRESOURCE(iconEnableId), IMAGE_ICON, 0, 0, LR_SHARED); if (!hicon) return FALSE; iIconEnable=ImageList_AddIcon(hImgList, hicon); if (iIconEnable==-1) return FALSE;
// load disable icon hicon=(HICON)LoadImage(m_hInstance, MAKEINTRESOURCE(iconDisableId), IMAGE_ICON, 0, 0, LR_SHARED); if (!hicon) { ImageList_Remove(hImgList,iIconEnable); return FALSE; }
iIconDisable = ImageList_AddIcon(hImgListDisabled, hicon); if (iIconDisable == -1) { // remove icon from enable list to keep same index in the lists ImageList_Remove(hImgList,iIconEnable); return FALSE; }
// load hot icon hicon = (HICON)LoadImage(m_hInstance, MAKEINTRESOURCE(iconHotId), IMAGE_ICON, 0, 0, LR_SHARED); if (!hicon) { ImageList_Remove(hImgList,iIconEnable); ImageList_Remove(hImgListDisabled,iIconDisable); return FALSE; }
// add it to hot list iIconHot = ImageList_AddIcon(hImgListHot, hicon); if (iIconHot==-1) { // remove icon from enable and disables list to keep same index in the lists ImageList_Remove(hImgList,iIconEnable); ImageList_Remove(hImgListDisabled,iIconDisable); return FALSE; }
// icon index should be the same in all imagelists if ((iIconEnable!=iIconDisable) ||(iIconEnable!=iIconHot)) return FALSE;
// add button to toolbar button.iBitmap = iIconEnable; button.idCommand = buttonId; button.fsState = buttonState; button.fsStyle = buttonStyle; button.iString = (INT_PTR)buttonText;
// store UserData button.dwData = (DWORD_PTR)new ToolBarButtonData();//(ToolTip,PopUpMenu);
if (!SendMessage(m_hWnd, TB_ADDBUTTONS, (WPARAM)1, (LPARAM) (LPTBBUTTON)&button)) return FALSE;
// resize toolbar if required SendMessage(m_hWnd, TB_AUTOSIZE, 0, 0);
return TRUE; }
void IceGui::ToolBar::AddSeparator() { TBBUTTON separator={0}; // add button to toolbar separator.iBitmap = I_IMAGENONE; separator.idCommand = 0; separator.fsState = TBSTATE_ENABLED; separator.fsStyle = BTNS_SEP; separator.dwData = 0; separator.iString = 0;
SendMessage(m_hWnd, TB_ADDBUTTONS, (WPARAM)1, (LPARAM)(LPTBBUTTON)&separator); }
BOOL IceGui::ToolBar::SetButtonText(int buttonId, TCHAR* buttonText) { TBBUTTONINFO buttonInfo ={0}; buttonInfo.dwMask = TBIF_TEXT; buttonInfo.cbSize = sizeof(TBBUTTONINFO); buttonInfo.pszText = buttonText; return (BOOL)SendMessage( m_hWnd, (UINT)TB_SETBUTTONINFO, (WPARAM) buttonId,(LPARAM) &buttonInfo); }
BOOL IceGui::ToolBar::SetButtonState(int buttonId, BYTE buttonState) { TBBUTTONINFO buttonInfo ={0}; buttonInfo.dwMask = TBIF_STATE; buttonInfo.fsState = buttonState; buttonInfo.cbSize = sizeof(TBBUTTONINFO);
return (BOOL)SendMessage( m_hWnd, (UINT)TB_SETBUTTONINFO, (WPARAM) buttonId,(LPARAM) &buttonInfo); }
BYTE IceGui::ToolBar::GetButtonState(int buttonId) { TBBUTTONINFO buttonInfo = {0}; buttonInfo.dwMask = TBIF_STATE; buttonInfo.cbSize = sizeof(TBBUTTONINFO);
// get button info if (SendMessage( m_hWnd, (UINT)TB_GETBUTTONINFO, (WPARAM)buttonId, (LPARAM)&buttonInfo) == -1 ) return 0;
return buttonInfo.fsState; }
BYTE IceGui::ToolBar::GetButtonStyle(int buttonId) { TBBUTTONINFO buttonInfo ={0}; buttonInfo.dwMask = TBIF_STATE; buttonInfo.cbSize = sizeof(TBBUTTONINFO);
// get button info if (SendMessage( m_hWnd, (UINT)TB_GETBUTTONINFO, (WPARAM)buttonId, (LPARAM)&buttonInfo) == -1 ) return 0;
return buttonInfo.fsStyle; }
BOOL IceGui::ToolBar::EnableButton(int buttonId,BOOL bEnable) { return (BOOL)SendMessage(m_hWnd, (UINT) TB_ENABLEBUTTON, (WPARAM) buttonId, (LPARAM) MAKELONG(bEnable,0)); }
/* Events */ BOOL IceGui::ToolBar::OnNotify(WPARAM wParam, LPARAM lParam) { return TRUE; }
/* Destructor, free memory/resource */ void IceGui::ToolBar::FreeImageLists() { ImageList_Destroy(this->hImgList); ImageList_Destroy(this->hImgListDisabled); ImageList_Destroy(this->hImgListHot); }
IceGui::ToolBar::~ToolBar() { FreeImageLists(); } /* End Of file */
|
--------------------
google.com и это все.
|