win32context.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 2,636 行 · 第 1/5 页

CPP
2,636
字号
	}	if ( theme ) {		RECT btnRect;		btnRect.left = rect->left_;		btnRect.top = rect->top_;		btnRect.right = rect->right_;		btnRect.bottom = rect->bottom_;		btnRect.right = btnRect.left + (btnRect.bottom - btnRect.top);		int dcs = SaveDC( dc_ );		int btnState = 0;		bool normal = true;		if ( state.isPressed() ) {			btnState |= state.isToggled() ? CBS_CHECKEDPRESSED : CBS_UNCHECKEDPRESSED;			normal = false;		}		if ( !state.isEnabled() ) {			btnState |= state.isToggled() ? CBS_CHECKEDDISABLED : CBS_UNCHECKEDDISABLED;			normal = false;		}		if ( state.isFocused() || state.isHighlighted() ) {			btnState |= state.isToggled() ? CBS_CHECKEDHOT : CBS_UNCHECKEDHOT;			normal = false;		}		if ( normal ) {			btnState |= state.isToggled() ? CBS_CHECKEDNORMAL : CBS_UNCHECKEDNORMAL;		}		SIZE val = {0};		Win32VisualStylesWrapper::GetThemePartSize (theme, dc_, BP_CHECKBOX, 0, NULL, TS_DRAW, &val );		btnRect.right = btnRect.left + val.cx;		Win32VisualStylesWrapper::DrawThemeBackground(theme, dc_, BP_CHECKBOX, btnState, &btnRect, 0);		btnRect.left = btnRect.right;		btnRect.right = rect->right_;		SetBkMode(dc_, TRANSPARENT);		VCF::Font btnFont = *context_->getCurrentFont();		HFONT font = NULL;		if ( System::isUnicodeEnabled() ) {			LOGFONTW* lf = (LOGFONTW*) btnFont.getFontPeer()->getFontHandleID();			font = ::CreateFontIndirectW( lf );			::SelectObject( dc_, font );		}		else {			LOGFONTA* lf = (LOGFONTA*) btnFont.getFontPeer()->getFontHandleID();			font = ::CreateFontIndirectA( lf );			::SelectObject( dc_, font );		}		btnRect.left += 5; //this is totally made up! Where would I get this from????		Win32VisualStylesWrapper::DrawThemeText(theme, dc_, BP_CHECKBOX, btnState,			state.buttonCaption_.c_str(),			state.buttonCaption_.length(),			DT_SINGLELINE | DT_VCENTER, NULL,			&btnRect);		Win32VisualStylesWrapper::CloseThemeData( theme );		RestoreDC(dc_, dcs );		DeleteObject( font );	}	else {	/**	JC	it turns out that the height/width is ALWAYS 13 pixels - no matter what	the DPI is	GetSystemMetrics( SM_CXMENUCHECK ) returns a value that changes based on the DPI	13 at 96 DPI and 17 at 120 DPI		*/		int checkBoxHeight = 13;		tmp.top_ = (int)(rect->top_ + rect->getHeight() /2.0 + 0.5);		tmp.top_ -= checkBoxHeight/2;		tmp.bottom_ = tmp.top_ + checkBoxHeight;		tmp.right_ = tmp.left_ + checkBoxHeight;		RECT r = {0,0,0,0};		r.left = (long)tmp.left_;		r.top = (long)tmp.top_;		r.right = (long)tmp.right_;		r.bottom = (long)tmp.bottom_;		UINT chkState =  (state.isToggled() || state.isPressed()) ?  DFCS_BUTTONCHECK | DFCS_CHECKED : DFCS_BUTTONCHECK;		int err = ::DrawFrameControl( dc_, &r, DFC_BUTTON, chkState );		tmp = *rect;		tmp.left_ = r.right + 3;		tmp.inflate( -1.0, -1.0 );		/*		JC - commented this out - it looked dorky???		if ( state.isFocused() ) {		drawThemeFocusRect( &tmp, state );		}		*/		releaseHandle();		long flags = GraphicsContext::tdoCenterVertAlign;		context_->textBoundedBy( &tmp, state.buttonCaption_, flags );	}}void Win32Context::drawThemeRadioButtonRect( Rect* rect, ButtonState& state ){	checkHandle();	Rect tmp = *rect;	HTHEME theme = NULL;	if ( Win32VisualStylesWrapper::IsThemeActive() ) {		theme = Win32VisualStylesWrapper::OpenThemeData( NULL, L"BUTTON" );	}	if ( theme ) {		RECT btnRect;		btnRect.left = rect->left_;		btnRect.top = rect->top_;		btnRect.right = rect->right_;		btnRect.bottom = rect->bottom_;		btnRect.right = btnRect.left + (btnRect.bottom - btnRect.top);		int dcs = SaveDC( dc_ );		int btnState = 0;		bool normal = true;		if ( state.isPressed() ) {			btnState |= state.isToggled() ? RBS_CHECKEDPRESSED : RBS_UNCHECKEDPRESSED;			normal = false;		}		if ( !state.isEnabled() ) {			btnState |= state.isToggled() ? RBS_CHECKEDDISABLED : RBS_UNCHECKEDDISABLED;			normal = false;		}		if ( state.isFocused() || state.isHighlighted() ) {			btnState |= state.isToggled() ? RBS_CHECKEDHOT : RBS_UNCHECKEDHOT;			normal = false;		}		if ( normal ) {			btnState |= state.isToggled() ? RBS_CHECKEDNORMAL : RBS_UNCHECKEDNORMAL;		}		SIZE val = {0};		Win32VisualStylesWrapper::GetThemePartSize (theme, dc_, BP_RADIOBUTTON, 0, NULL, TS_DRAW, &val );		btnRect.right = btnRect.left + val.cx;		Win32VisualStylesWrapper::DrawThemeBackground(theme, dc_, BP_RADIOBUTTON, btnState, &btnRect, 0);		btnRect.left = btnRect.right;		btnRect.right = rect->right_;		SetBkMode(dc_, TRANSPARENT);		VCF::Font btnFont = *context_->getCurrentFont();		HFONT font = NULL;		if ( System::isUnicodeEnabled() ) {			LOGFONTW* lf = (LOGFONTW*) btnFont.getFontPeer()->getFontHandleID();			font = ::CreateFontIndirectW( lf );			::SelectObject( dc_, font );		}		else {			LOGFONTA* lf = (LOGFONTA*) btnFont.getFontPeer()->getFontHandleID();			font = ::CreateFontIndirectA( lf );			::SelectObject( dc_, font );		}		btnRect.left += 5; //this is totally made up! Where would I get this from????		Win32VisualStylesWrapper::DrawThemeText(theme, dc_, BP_RADIOBUTTON, btnState,			state.buttonCaption_.c_str(),			state.buttonCaption_.length(),			DT_SINGLELINE | DT_VCENTER, NULL,			&btnRect);		Win32VisualStylesWrapper::CloseThemeData( theme );		RestoreDC(dc_, dcs );		DeleteObject( font );	}	else {	/**	JC	it turns out that the height/width is ALWAYS 13 pixels - no matter what	the DPI is	GetSystemMetrics( SM_CXMENUCHECK ) returns a value that changes based on the DPI	13 at 96 DPI and 17 at 120 DPI		*/		int radioBoxHeight = 13;		tmp.top_ = (int)(rect->top_ + rect->getHeight() /2.0 + 0.5);		tmp.top_ -= radioBoxHeight/2;		tmp.bottom_ = tmp.top_ + radioBoxHeight;		tmp.right_ = tmp.left_ + radioBoxHeight;		RECT r = {0,0,0,0};		r.left = (long)tmp.left_;		r.top = (long)tmp.top_;		r.right = (long)tmp.right_;		r.bottom = (long)tmp.bottom_;		UINT btnState =  state.isToggled() ?  DFCS_BUTTONRADIO | DFCS_CHECKED : DFCS_BUTTONRADIO;		::DrawFrameControl( dc_, &r, DFC_BUTTON, btnState );		tmp = *rect;		tmp.left_ = r.right + 3;		tmp.inflate( -1.0, -1.0 );		/*		JC - commented this out - it looked dorky???		if ( state.isFocused() ) {		drawThemeFocusRect( &tmp, state );		}		*/		releaseHandle();		long flags = GraphicsContext::tdoCenterVertAlign;		context_->textBoundedBy( &tmp, state.buttonCaption_, flags );	}}void Win32Context::drawThemeComboboxRect( Rect* rect, ButtonState& state ){	HTHEME theme = NULL;	RECT r = {0};	r.left = rect->left_;	r.top = rect->top_;	r.right = rect->right_;	r.bottom = rect->bottom_;	if ( Win32VisualStylesWrapper::IsThemeActive() ) {		theme = Win32VisualStylesWrapper::OpenThemeData( NULL, L"EDIT" );	}	if ( theme ) {		int dcs = SaveDC( dc_ );		int btnState = 0;		btnState |= state.isPressed() ? CBXS_PRESSED : CBXS_NORMAL;		if ( !state.isEnabled() ) {			btnState |= CBXS_DISABLED;		}		if ( state.isFocused() || state.isHighlighted() ) {			btnState |= CBXS_HOT;		}		int editState = state.isEnabled() ? ETS_NORMAL : ETS_DISABLED;		if ( state.isEnabled() ) {			if ( state.isFocused() ) {				editState |= ETS_FOCUSED;				editState |= ETS_SELECTED;			}			if ( state.isHighlighted() ) {				editState |= ETS_HOT;			}		}		Win32VisualStylesWrapper::DrawThemeBackground(theme, dc_, EP_EDITTEXT, editState, &r, 0);		SetBkMode(dc_, TRANSPARENT);		VCF::Font btnFont = *context_->getCurrentFont();		HFONT font = NULL;		if ( System::isUnicodeEnabled() ) {			LOGFONTW* lf = (LOGFONTW*) btnFont.getFontPeer()->getFontHandleID();			font = ::CreateFontIndirectW( lf );			::SelectObject( dc_, font );		}		else {			LOGFONTA* lf = (LOGFONTA*) btnFont.getFontPeer()->getFontHandleID();			font = ::CreateFontIndirectA( lf );			::SelectObject( dc_, font );		}		r.left += 2;		r.top += 2;		r.bottom -= 2;		r.right -= 2;		Win32VisualStylesWrapper::DrawThemeText(theme, dc_, EP_EDITTEXT, editState,												state.buttonCaption_.c_str(),												state.buttonCaption_.length(),												DT_SINGLELINE | DT_VCENTER | DT_LEFT | DT_EXPANDTABS,												NULL,												&r);		Win32VisualStylesWrapper::CloseThemeData( theme );		r.left -= 1;		r.top -= 1;		r.bottom += 1;		r.right += 1;		//now do drop down button		theme = Win32VisualStylesWrapper::OpenThemeData( NULL, L"COMBOBOX" );		SIZE val = {0};		HRESULT hr = Win32VisualStylesWrapper::GetThemePartSize (theme, dc_, CP_DROPDOWNBUTTON, 0, NULL, TS_DRAW, &val );		if ( SUCCEEDED(hr) && val.cx != 0 ) {			r.left = r.right - val.cx;		}		else {			//do it the old way???			NONCLIENTMETRICS ncm;			memset( &ncm, 0, sizeof(NONCLIENTMETRICS) );			ncm.cbSize = sizeof(NONCLIENTMETRICS);			SystemParametersInfo( SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0 );			r.left = r.right - ncm.iScrollWidth;		}		Win32VisualStylesWrapper::DrawThemeBackground(theme, dc_, CP_DROPDOWNBUTTON, btnState, &r, 0);		Win32VisualStylesWrapper::CloseThemeData( theme );		RestoreDC(dc_, dcs );		DeleteObject( font );	}	else {		int dcState = ::SaveDC( dc_ );		::FillRect( dc_, &r, (HBRUSH)(COLOR_WINDOW+1) );		RECT btnRect = r;		NONCLIENTMETRICS ncm;		memset( &ncm, 0, sizeof(NONCLIENTMETRICS) );		ncm.cbSize = sizeof(NONCLIENTMETRICS);		SystemParametersInfo( SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0 );		btnRect.left = r.right - ncm.iScrollWidth;		::FillRect( dc_, &btnRect, (HBRUSH)(COLOR_3DFACE+1) );		UINT flags = 0;		flags |= DFCS_SCROLLDOWN;		if ( state.isPressed() ) {			// Native win32 pressed combobox buttons are always flat, so			// we're going to do our best to make it look flat in our			// combobox emulation too			flags |= DFCS_PUSHED | DFCS_FLAT;		}		if ( !state.isEnabled() ) {			flags |= DFCS_INACTIVE;		}		DrawFrameControl( dc_, &btnRect, DFC_SCROLL, flags );		RECT bkRect = r;		bkRect.right = btnRect.left;		SetBkMode( dc_, TRANSPARENT );		COLORREF bkColor;		COLORREF textColor;		if ( !state.isEnabled() ) {			bkColor = GetSysColor( COLOR_BTNFACE );			textColor = GetSysColor( COLOR_WINDOWTEXT );		}		else {			InflateRect( &bkRect, -2, -2 );			if ( state.isFocused() ) {				bkColor = GetSysColor( COLOR_HIGHLIGHT );				textColor = GetSysColor( COLOR_HIGHLIGHTTEXT );				InflateRect( &bkRect, +1, +1 );				::DrawFocusRect( dc_, &bkRect );				InflateRect( &bkRect, -1, -1 );			}			else {				bkColor = GetSysColor( COLOR_WINDOW );				textColor = GetSysColor( COLOR_WINDOWTEXT );			}		}		HBRUSH bkBrush = CreateSolidBrush( bkColor );		FillRect( dc_, &bkRect, bkBrush );		SetTextColor( dc_, textColor );		UINT fmt = DT_VCENTER | DT_SINGLELINE | DT_LEFT | DT_EXPANDTABS | DT_END_ELLIPSIS;		HFONT font;		prepareDCWithContextFont( font );		::SelectObject( dc_, font );		if ( System::isUnicodeEnabled() ) {			DrawTextW( dc_, state.buttonCaption_.c_str(), state.buttonCaption_.size(), &bkRect, fmt );		}		else {			AnsiString tmp = state.buttonCaption_;			DrawTextA( dc_, tmp.c_str(), tmp.size(), &bkRect, fmt );		}		::RestoreDC( dc_, dcState );		DeleteObject( font );		DeleteObject( bkBrush );	}}void Win32Context::drawThemeScrollButtonRect( Rect* rect, ScrollBarState& state ){	checkHandle();	RECT r = {0,0,0,0};	r.left = (long)rect->left_;	r.top = (long)rect->top_;	r.right = (long)rect->right_;	r.bottom = (long)rect->bottom_;	HTHEME theme = NULL;	if ( Win32VisualStylesWrapper::IsThemeActive() ) {		theme = Win32VisualStylesWrapper::OpenThemeData( NULL, L"SCROLLBAR" );	}	if ( theme ) {		int scrollState = 0;		if ( state.isVertical() ) {			if ( state.isUpOrLeftBtn() ) {				scrollState |= state.isEnabled() ? ABS_UPNORMAL : ABS_UPDISABLED;			}			else if ( state.isDownOrRightBtn() ) {				scrollState |= state.isEnabled() ? ABS_DOWNNORMAL : ABS_DOWNDISABLED;			}			if ( state.isEnabled() ) {				if ( state.isScrollBarIncrArrowPressed() ) {					scrollState |= ABS_UPPRESSED;				}				else if ( state.isScrollBarDecrArrowPressed() ) {					scrollState |= ABS_DOWNPRESSED;				}			}		}		else if ( !state.isVertical() ) {			if ( state.isDownOrRightBtn() ) {				scrollState |= state.isEnabled() ? ABS_RIGHTNORMAL : ABS_RIGHTDISABLED;			}			else if ( state.isUpOrLeftBtn() ) {				scrollState |= state.isEnabled() ? ABS_LEFTNORMAL : ABS_LEFTDISABLED;			}			if ( state.isEnabled() ) {				if ( state.isScrollBarIncrArrowPressed() ) {					scrollState |= ABS_LEFTPRESSED;				}				else if ( state.isScrollBarDecrArrowPressed() ) {					scrollState |= ABS_RIGHTPRESSED;				}			}		}		if ( true == state.isScrollBarThumbPr

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?