win32toolbar.cpp
来自「这是VCF框架的代码」· C++ 代码 · 共 1,556 行 · 第 1/3 页
CPP
1,556 行
//Win32Toolbar.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#include "vcf/ApplicationKit/ApplicationKit.h"#include "vcf/ApplicationKit/ApplicationKitPrivate.h"#include "vcf/ApplicationKit/Win32Toolbar.h"#include "vcf/ApplicationKit/Toolbar.h"using namespace VCF;Win32Toolbar::Win32Toolbar(Control* control): AbstractWin32Component(control), imageListCtrl_(NULL), currentlyModifyingItem_(false){}Win32Object::CreateParams Win32Toolbar::createParams(){ Win32Object::CreateParams result; result.first = WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT | CCS_NODIVIDER;// | CCS_NORESIZE;; result.second = 0; return result;}void Win32Toolbar::create( Control* control ){ Win32ToolKit* toolkit = (Win32ToolKit*)UIToolkit::internal_getDefaultUIToolkit(); HWND parent = toolkit->getDummyParent(); CreateParams params = createParams(); if ( System::isUnicodeEnabled() ) { hwnd_ = ::CreateWindowExW( params.second, TOOLBARCLASSNAMEW, NULL, params.first, 0, 0, 1, 1, parent, NULL, ::GetModuleHandleW(NULL), NULL ); } else { hwnd_ = ::CreateWindowExA( params.second, TOOLBARCLASSNAMEA, NULL, params.first, 0, 0, 1, 1, parent, NULL, ::GetModuleHandleA(NULL), NULL ); } if ( NULL != hwnd_ ) { Win32Object::registerWin32Object( this ); subclassWindow(); registerForFontChanges(); SendMessage(hwnd_, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0); DWORD btnSize = SendMessage(hwnd_, TB_GETBUTTONSIZE, 0, 0 ); SendMessage(hwnd_, TB_SETIMAGELIST, 0, 0 ); currentlyModifyingItem_ = true; Size sz; sz.width_ = LOWORD(btnSize); sz.height_ = HIWORD(btnSize); ((Toolbar*)peerControl_)->setButtonSize(sz); currentlyModifyingItem_ = false; SendMessage(hwnd_, TB_AUTOSIZE, 0, 0 ); } control->getViewModel()->addModelHandler( new ModelEventHandler<Win32Toolbar>( this, &Win32Toolbar::onModelChanged, "Win32Toolbar::onModelChanged" ) );}void Win32Toolbar::setEnableAutoResize( const bool& val ){ int style = GetWindowLong( hwnd_, GWL_STYLE ); if( val ) { style &= ~CCS_NORESIZE; } else { style |= CCS_NORESIZE; } SetWindowLong( hwnd_, GWL_STYLE, style ); ::SetWindowPos( hwnd_, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE ); ::UpdateWindow( hwnd_ );}bool Win32Toolbar::isAutoResizeEnabled(){ int style = GetWindowLong( hwnd_, GWL_STYLE ); return (style & CCS_NORESIZE) ? false : true;}bool Win32Toolbar::handleEventMessages( UINT message, WPARAM wParam, LPARAM lParam, LRESULT& wndProcResult, WNDPROC defaultWndProc ){ bool result = false; wndProcResult = 0; switch ( message ) { case WM_ERASEBKGND :{ //result = CallWindowProc( oldToolbarWndProc_, hwnd_, message, wParam, lParam ); wndProcResult = 0; result = true; } break; case WM_PAINT :{ PAINTSTRUCT ps; HDC dc = BeginPaint( hwnd_, &ps ); RECT r; GetClientRect( hwnd_, &r ); ////FillRect( dc, &r, (HBRUSH) (COLOR_3DFACE + 1) ); HDC memDC = doControlPaint( dc, r, NULL, cpControlOnly ); defaultWndProcedure( WM_PAINT, (WPARAM)memDC, 0 ); updatePaintDC( dc, r, NULL ); EndPaint( hwnd_, &ps ); wndProcResult = 1; result = true; } break; case WM_NCCALCSIZE: { wndProcResult = handleNCCalcSize( wParam, lParam ); result = true; } break; case WM_NCPAINT: { wndProcResult = handleNCPaint( wParam, lParam ); return true; } break; case WM_DESTROY : { if ( NULL != imageListCtrl_ ) { //destroy the old one int err = ImageList_Destroy( imageListCtrl_ ); imageListCtrl_ = NULL; } result = AbstractWin32Component::handleEventMessages( message, wParam, lParam, wndProcResult ); } break; case WM_SIZE : { AbstractWin32Component::handleEventMessages( message, wParam, lParam, wndProcResult ); DWORD style = ::GetWindowLong( hwnd_, GWL_STYLE ); resizeToolbarItems(); } break; case TBN_GETDISPINFOW : { NMTBDISPINFOW* dispInfo = (NMTBDISPINFOW*)lParam; ToolbarItem* item = (ToolbarItem*)dispInfo->lParam; dispInfo->iImage = item->getImageIndex(); } break; case TBN_GETDISPINFOA : { NMTBDISPINFOA* dispInfo = (NMTBDISPINFOA*)lParam; ToolbarItem* item = (ToolbarItem*)dispInfo->lParam; dispInfo->iImage = item->getImageIndex(); } break; case TBN_GETINFOTIPW : { NMTBGETINFOTIPW * dispInfo = (NMTBGETINFOTIPW *)lParam; ToolbarItem* item = (ToolbarItem*)dispInfo->lParam; String tooltip = item->getTooltip(); int size = minVal<int>(tooltip.size(),dispInfo->cchTextMax); tooltip.copy( dispInfo->pszText, size ); dispInfo->pszText[size] = 0; } break; case TBN_GETINFOTIPA : { NMTBGETINFOTIPA * dispInfo = (NMTBGETINFOTIPA *)lParam; ToolbarItem* item = (ToolbarItem*)dispInfo->lParam; AnsiString tooltip = item->getTooltip(); int size = minVal<int>(tooltip.size(),dispInfo->cchTextMax); tooltip.copy( dispInfo->pszText, size ); dispInfo->pszText[size] = 0; } break; case NM_CLICK : { if ( System::isUnicodeEnabled() ) { TBNOTIFYW* tbn = (TBNOTIFYW*)lParam; TBBUTTONINFOW info = {0}; info.dwMask = TBIF_STATE | TBIF_LPARAM ; info.cbSize = sizeof(info); if ( SendMessage( hwnd_, TB_GETBUTTONINFOW, tbn->iItem, (LPARAM)&info ) >= 0 ) { ToolbarItem* item = (ToolbarItem*)info.lParam; long state = item->getState(); if ( (TBSTATE_CHECKED & info.fsState) && ( state & ToolbarItem::tisChecked ) ) { state |= ToolbarItem::tisPressed; } else { state &= ~ToolbarItem::tisPressed; } currentlyModifyingItem_ = true; item->setState( state ); currentlyModifyingItem_ = false; } } else { TBNOTIFYA* tbn = (TBNOTIFYA*)lParam; TBBUTTONINFOA info = {0}; info.dwMask = TBIF_STATE | TBIF_LPARAM ; info.cbSize = sizeof(info); if ( SendMessage( hwnd_, TB_GETBUTTONINFOA, tbn->iItem, (LPARAM)&info ) >= 0 ) { ToolbarItem* item = (ToolbarItem*)info.lParam; long state = item->getState(); if ( (TBSTATE_CHECKED & info.fsState) && ( state & ToolbarItem::tisChecked ) ) { state |= ToolbarItem::tisPressed; } else { state &= ~ToolbarItem::tisPressed; } currentlyModifyingItem_ = true; item->setState( state ); currentlyModifyingItem_ = false; } } } break; case WM_COMMAND : { WORD wNotifyCode = HIWORD(wParam); // notification code WORD wID = LOWORD(wParam); // item, control, or accelerator identifier HWND hwndCtl = (HWND) lParam; if ( System::isUnicodeEnabled() ) { TBBUTTONINFOW info = {0}; info.cbSize = sizeof(info); info.dwMask |= TBIF_LPARAM | TBIF_STATE; SendMessage( hwnd_, TB_GETBUTTONINFOW, wID, (LPARAM)&info ); ToolbarItem* item = (ToolbarItem*)info.lParam; if ( NULL != item ) { long state = item->getState(); if ( (TBSTATE_CHECKED & info.fsState) && ( state & ToolbarItem::tisChecked ) ) { state |= ToolbarItem::tisPressed; } else { state &= ~ToolbarItem::tisPressed; } currentlyModifyingItem_ = true; item->setState( state ); currentlyModifyingItem_ = false; item->click(); } } else { TBBUTTONINFOA info = {0}; info.cbSize = sizeof(info); info.dwMask |= TBIF_LPARAM | TBIF_STATE; SendMessage( hwnd_, TB_GETBUTTONINFOA, wID, (LPARAM)&info ); ToolbarItem* item = (ToolbarItem*)info.lParam; if ( NULL != item ) { long state = item->getState(); if ( (TBSTATE_CHECKED & info.fsState) && ( state & ToolbarItem::tisChecked ) ) { state |= ToolbarItem::tisPressed; } else { state &= ~ToolbarItem::tisPressed; } currentlyModifyingItem_ = true; item->setState( state ); currentlyModifyingItem_ = false; item->click(); } } } break; case NM_CUSTOMDRAW : { NMTBCUSTOMDRAW* lpNMCustomDraw = (NMTBCUSTOMDRAW*) lParam; switch ( lpNMCustomDraw->nmcd.dwDrawStage ) { case CDDS_PREPAINT : { wndProcResult = CDRF_NOTIFYITEMDRAW; result = true; } break; case CDDS_ITEMPREPAINT : { ToolbarItem* item = (ToolbarItem*)lpNMCustomDraw->nmcd.lItemlParam; if ( NULL != item ) { } if ( System::isUnicodeEnabled() ) { TBBUTTONINFOW info = {0}; info.cbSize = sizeof(info); info.dwMask |= TBIF_STATE; if ( SendMessage( hwnd_, TB_GETBUTTONINFOW, lpNMCustomDraw->nmcd.dwItemSpec, (LPARAM)&info ) >= 0 ) { long state = item->getState(); if ( (TBSTATE_CHECKED & info.fsState) && ( state & ToolbarItem::tisChecked ) ) { state |= ToolbarItem::tisPressed; } else { state &= ~ToolbarItem::tisPressed; } currentlyModifyingItem_ = true; item->setState( state ); currentlyModifyingItem_ = false; } } else { TBBUTTONINFOA info = {0}; info.cbSize = sizeof(info); info.dwMask |= TBIF_STATE; if ( SendMessage( hwnd_, TB_GETBUTTONINFOA, lpNMCustomDraw->nmcd.dwItemSpec, (LPARAM)&info ) >= 0 ) { long state = item->getState(); if ( (TBSTATE_CHECKED & info.fsState) && ( state & ToolbarItem::tisChecked ) ) { state |= ToolbarItem::tisPressed; } else { state &= ~ToolbarItem::tisPressed; } currentlyModifyingItem_ = true; item->setState( state ); currentlyModifyingItem_ = false; } } wndProcResult = CDRF_DODEFAULT; result = true; } break; } } break; default : { result = AbstractWin32Component::handleEventMessages( message, wParam, lParam, wndProcResult ); } break; } return result;}void Win32Toolbar::onModelChanged( ModelEvent* e ){ if ( currentlyModifyingItem_ ) { return; } int index = 0; switch( e->getType() ) { case ToolbarItem::tbCaptionChanged : { ToolbarModelEvent* tme = (ToolbarModelEvent*)e; if ( System::isUnicodeEnabled() ) { TBBUTTONINFOW info = {0}; info.dwMask = TBIF_TEXT ; info.cbSize = sizeof(info); VCFChar tmp[256]; String caption = tme->getItem()->getCaption(); if ( tme->getItem()->getUseLocaleStrings() ) { caption = System::getCurrentThreadLocale()->translate( caption ); } int size = minVal<int>(caption.size(), 255); caption.copy( tmp, size ); tmp[size] = 0; info.cchText = size; info.pszText = tmp; SendMessage( hwnd_, TB_SETBUTTONINFOW, tme->getItem()->getIndex(), (LPARAM)&info ); } else { TBBUTTONINFOA info = {0}; info.dwMask = TBIF_TEXT ; info.cbSize = sizeof(info); char tmp[256]; String caption = tme->getItem()->getCaption(); if ( tme->getItem()->getUseLocaleStrings() ) { caption = System::getCurrentThreadLocale()->translate( caption ); } AnsiString ansiCaption = caption; int size = minVal<int>(ansiCaption.size(), 255); ansiCaption.copy( tmp, size ); tmp[size] = 0; info.cchText = size; info.pszText = tmp; SendMessage( hwnd_, TB_SETBUTTONINFOA, tme->getItem()->getIndex(), (LPARAM)&info ); } resizeToolbarItems(); } break; case ToolbarItem::tbImageIndexChanged : { ToolbarModelEvent* tme = (ToolbarModelEvent*)e; if ( System::isUnicodeEnabled() ) { TBBUTTONINFOW info = {0}; info.dwMask = TBIF_IMAGE ; info.cbSize = sizeof(info); index = tme->getItem()->getIndex(); SendMessage( hwnd_, TB_GETBUTTONINFOW, index, (LPARAM)&info ); info.iImage = tme->getItem()->getImageIndex(); SendMessage( hwnd_, TB_SETBUTTONINFOW, index, (LPARAM)&info ); } else { TBBUTTONINFOA info = {0}; info.dwMask = TBIF_IMAGE ; info.cbSize = sizeof(info); index = tme->getItem()->getIndex(); SendMessage( hwnd_, TB_GETBUTTONINFOA, index, (LPARAM)&info ); info.iImage = tme->getItem()->getImageIndex(); SendMessage( hwnd_, TB_SETBUTTONINFOA, index, (LPARAM)&info ); } } break; case ToolbarItem::tbSelected : { } break; case ToolbarItem::tbGroupChanged : { ToolbarModelEvent* tme = (ToolbarModelEvent*)e; if (System::isUnicodeEnabled() ) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?