win32context.cpp
来自「这是VCF框架的代码」· C++ 代码 · 共 2,636 行 · 第 1/5 页
CPP
2,636 行
void Win32Context::rectangle(const double & x1, const double & y1, const double & x2, const double & y2){ int fixVal = 0; if ( true == inFillPath_ ){ fixVal = 1; } ::Rectangle( dc_, (long)x1, (long)y1, (long)(x2 + fixVal), (long)(y2 + fixVal) );}void Win32Context::roundRect(const double & x1, const double & y1, const double & x2, const double & y2, const double & xc, const double & yc){ int fixVal = 0; if ( true == inFillPath_ ){ fixVal = 1; } ::RoundRect( dc_, (long)x1, (long)y1, (long)x2 + fixVal, (long)y2 + fixVal, (long)xc, (long)yc );}void Win32Context::ellipse(const double & x1, const double & y1, const double & x2, const double & y2){ pathStarted_ = true; //swap out the values to ensure they are normalized since windows is brain dead about this double ax1 = x1; double ay1 = y1; double ax2 = x2; double ay2 = y2; double tmp = x2; if ( ax1 > ax2 ) { ax2 = ax1; ax1 = tmp; } tmp = ay2; if ( ay1 > ay2 ) { ay2 = ay1; ay1 = tmp; } int fixVal = 0; if ( true == inFillPath_ ){ fixVal = 1; } ::Ellipse( dc_, (long)ax1, (long)ay1, (long)(ax2 + fixVal), (long)(ay2 + fixVal) );}void Win32Context::arc(const double & x1, const double & y1, const double & x2, const double & y2, const double & x3, const double & y3, const double & x4, const double & y4){ pathStarted_ = true; //swap out the values to ensure they are normalized since windows is brain dead about this double ax1 = x1; double ay1 = y1; double ax2 = x2; double ay2 = y2; double tmp = x2; if ( ax1 > ax2 ) { ax2 = ax1; ax1 = tmp; } tmp = ay2; if ( ay1 > ay2 ) { ay2 = ay1; ay1 = tmp; } int fixVal = 0; if ( true == inFillPath_ ){ fixVal = 1; } ::Arc( dc_, (long)ax1, (long)ay1, (long)ax2 + fixVal, (long)ay2 + fixVal, (long)x3, (long)y3, (long)x4, (long)y4 );}void Win32Context::polyline( const std::vector<Point>& pts){ POINT* polyPts = new POINT[pts.size()]; std::vector<Point>::const_iterator it = pts.begin(); int i =0; while ( it != pts.end() ) { const Point& pt = *it; polyPts[i].x = (long)pt.x_; polyPts[i].y = (long)pt.y_; it++; i++; } if ( inFillPath_ ){ ::Polygon( dc_, polyPts, pts.size() ); } else{ ::Polyline( dc_, polyPts, pts.size() ); } delete[] polyPts;}void Win32Context::curve(const double & x1, const double & y1, const double & x2, const double & y2, const double & x3, const double & y3, const double & x4, const double & y4 ){ POINT bezPts[4]; memset( &bezPts[0], 0, sizeof(bezPts[0]) * 4 ); bezPts[0].x = (long)x1; bezPts[0].y = (long)y1; bezPts[1].x = (long)x2; bezPts[1].y = (long)y2; bezPts[2].x = (long)x3; bezPts[2].y = (long)y3; bezPts[3].x = (long)x4; bezPts[3].y = (long)y4; if ( inFillPath_ ){ ::BeginPath( dc_ ); ::PolyBezier( dc_, bezPts, 4 ); ::EndPath( dc_ ); ::FillPath( dc_ ); } else { ::PolyBezier( dc_, bezPts, 4 ); }}void Win32Context::lineTo(const double & x, const double & y){ ::LineTo( dc_, (long)x, (long)y );}void Win32Context::moveTo(const double & x, const double & y){ ::MoveToEx( dc_, (long)x, (long)y, NULL );}OSHandleID Win32Context::getContextID(){ return (OSHandleID)dc_;}void Win32Context::setOrigin( const double& x, const double& y ){ checkHandle(); POINT pt = {0,0}; int err = ::SetViewportOrgEx( dc_, (long)x, (long)y, &pt ); if ( !err ) { err = GetLastError(); } origin_.x_ = x; origin_.y_ = y; oldOrigin_.x_ = pt.x; oldOrigin_.y_ = pt.y; releaseHandle();}Point Win32Context::getOrigin(){ checkHandle(); POINT pt = {0,0}; ::GetViewportOrgEx( dc_, &pt ); origin_.x_ = pt.x; origin_.y_ = pt.y; releaseHandle(); return origin_;}void Win32Context::copyContext( const Rect& sourceRect, const Rect& destRect, ContextPeer* sourceContext ){ if ( NULL != sourceContext ){ checkHandle(); HDC dc = (HDC)sourceContext->getContextID(); ::BitBlt( dc_, (long)destRect.left_, (long)destRect.top_, destRect.getWidth(), destRect.getHeight(), dc, sourceRect.left_, sourceRect.top_, SRCCOPY ); releaseHandle(); } //else throw exception ???}bool Win32Context::isMemoryContext(){ return isMemoryCtx_;}void Win32Context::copyToImage( Win32Image* image ){}HDC Win32Context::getDC(){ return dc_;}void Win32Context::setContext( GraphicsContext* context ){ context_ = context;}GraphicsContext* Win32Context::getContext(){ return context_;}void Win32Context::setContextID( OSHandleID handle ){ dc_ = (HDC)handle;}void Win32Context::textAt( const Rect& bounds, const String& text, const long& drawOptions ){ //checkHandle(); if ( NULL == context_ ){ //throw exception ! } UINT textAlignment = (alignToBaseline_) ? TA_BASELINE | TA_LEFT : TA_TOP | TA_LEFT ; UINT oldTextAlignment = ::SetTextAlign( dc_, textAlignment ); RECT r = {0,0,0,0}; r.left = (long)bounds.left_; r.right = (long)bounds.right_; r.top = (long)bounds.top_; r.bottom = (long)bounds.bottom_; if ( r.left > r.right ) { r.left = r.right; } if ( (r.right == r.left) || (r.bottom == r.top) ) { //no-op! no height or no width return; } UINT formatOptions = 0; Matrix2D& currentXFrm = *context_->getCurrentTransform(); if ( !context_->isDefaultTransform() ) { formatOptions = DT_NOCLIP; //this was put here to make rotated text look better JC } if ( drawOptions & GraphicsContext::tdoLeftAlign ) { formatOptions |= DT_LEFT; } else if ( drawOptions & GraphicsContext::tdoCenterHorzAlign ) { formatOptions |= DT_CENTER; } else if ( drawOptions & GraphicsContext::tdoRightAlign ) { formatOptions |= DT_RIGHT; } if ( drawOptions & GraphicsContext::tdoTopAlign ) { formatOptions |= DT_TOP; } else if ( drawOptions & GraphicsContext::tdoCenterVertAlign ) { formatOptions |= DT_VCENTER; } else if ( drawOptions & GraphicsContext::tdoBottomAlign ) { formatOptions |= DT_BOTTOM; } if ( drawOptions & GraphicsContext::tdoWordWrap ) { formatOptions |= DT_WORDBREAK; } else { formatOptions |= DT_WORD_ELLIPSIS | DT_SINGLELINE; } formatOptions |= DT_EXPANDTABS; /* Not using for now DRAWTEXTPARAMS extraParams = {0}; extraParams.cbSize = sizeof(DRAWTEXTPARAMS); */ /* *determine the size of the text and adjust the rect accordingly. *can't use DrawText to figure out the rect because it doesn't *calc the bounds correctly. */ //SIZE textSize = {0}; //GetTextExtentPoint32( dc_, text.c_str(), text.size(), &textSize ); //r.right = r.left + textSize.cx; //r.bottom = r.top + textSize.cy; if ( System::isUnicodeEnabled() ) { VCFChar* textToDraw = new VCFChar[text.size()+1]; memset( textToDraw, 0, (text.size()+1)*sizeof(VCFChar) ); text.copy( textToDraw, text.size() ); if ( drawOptions & GraphicsContext::tdoWordWrap ) { if ( (drawOptions & GraphicsContext::tdoCenterVertAlign) || (drawOptions & GraphicsContext::tdoBottomAlign) ) { RECT r2 = r; int h = DrawTextExW( dc_, textToDraw, text.size(), &r, formatOptions | DT_CALCRECT, NULL ); if ( drawOptions & GraphicsContext::tdoCenterVertAlign ) { r.left = r2.left; r.right = r2.right; r.top = r2.top + ((r2.bottom - r2.top)/2) - (h/2); r.bottom = r.top + h; } else if ( drawOptions & GraphicsContext::tdoBottomAlign ) { r.left = r2.left; r.right = r2.right; r.top = r2.bottom - h; r.bottom = r.top + h; } } } DrawTextExW( dc_, textToDraw, text.size(), &r, formatOptions, NULL ); //clean up after ourselves delete[] textToDraw; } else { AnsiString tmpText = text; char* textToDraw = new char[tmpText.size()+1]; memset( textToDraw, 0, (tmpText.size()+1)*sizeof(char) ); text.copy( textToDraw, tmpText.size() ); if ( drawOptions & GraphicsContext::tdoWordWrap ) { if ( (drawOptions & GraphicsContext::tdoCenterVertAlign) || (drawOptions & GraphicsContext::tdoBottomAlign) ) { RECT r2 = r; int h = DrawTextExA( dc_, textToDraw, tmpText.size(), &r, formatOptions | DT_CALCRECT, NULL ); if ( drawOptions & GraphicsContext::tdoCenterVertAlign ) { r.left = r2.left; r.right = r2.right; r.top = r2.top + ((r2.bottom - r2.top)/2) - (h/2); r.bottom = r.top + h; } else if ( drawOptions & GraphicsContext::tdoBottomAlign ) { r.left = r2.left; r.right = r2.right; r.top = r2.bottom - h; r.bottom = r.top + h; } } } DrawTextExA( dc_, textToDraw, tmpText.size(), &r, formatOptions, NULL ); //clean up after ourselves delete[] textToDraw; } //releaseHandle();}bool Win32Context::isXORModeOn(){ return isXORModeOn_;}void Win32Context::setXORModeOn( const bool& XORModeOn ){ isXORModeOn_ = XORModeOn;}double Win32Context::getTextWidth( const String& text ){ double result = 0.0; checkHandle(); if ( NULL == context_ ){ //throw exception ! } Font* ctxFont = context_->getCurrentFont(); if ( NULL == ctxFont ){ //throw exception } FontPeer* fontImpl = ctxFont->getFontPeer(); if ( NULL == fontImpl ){ //throw exception } HFONT font = NULL; prepareDCWithContextFont( font ); /* if ( System::isUnicodeEnabled() ) { LOGFONTW* logFont = (LOGFONTW*)fontImpl->getFontHandleID(); font = CreateFontIndirectW( logFont ); } else { LOGFONTA* logFont = (LOGFONTA*)fontImpl->getFontHandleID(); font = CreateFontIndirectA( logFont ); } */ HFONT oldFont = (HFONT)::SelectObject( dc_, font ); SIZE textSize = {0,0}; if ( String::npos != text.find( "\t" ) ) { RECT r = {0,0,0,0}; if ( System::isUnicodeEnabled() ) { DrawTextW( dc_, text.c_str(), text.size(), &r, DT_CALCRECT | DT_EXPANDTABS | DT_SINGLELINE | DT_LEFT ); } else { AnsiString tmpText = text; DrawTextA( dc_, tmpText.c_str(), tmpText.size(), &r, DT_CALCRECT | DT_EXPANDTABS | DT_SINGLELINE | DT_LEFT ); } result = r.right - r.left; } else { if ( System::isUnicodeEnabled() ) { GetTextExtentPoint32W( dc_, text.c_str(), text.size(), &textSize ); } else { GetTextExtentPoint32A( dc_, text.ansi_c_str(), text.size(), &textSize ); } result = textSize.cx; } ::SelectObject( dc_, oldFont ); ::DeleteObject( font ); releaseHandle(); return result;}double Win32Context::getTextHeight( const String& text ){ checkHandle(); if ( NULL == context_ ){ //throw exception ! } Font* ctxFont = context_->getCurrentFont(); if ( NULL == ctxFont ){ //throw exception } FontPeer* fontImpl = ctxFont->getFontPeer(); if ( NULL == fontImpl ){ //throw exception } HFONT font = NULL; /* if ( System::isUnicodeEnabled() ) { LOGFONTW* logFont = (LOGFONTW*)fontImpl->getFontHandleID(); font = CreateFontIndirectW( logFont ); } else { LOGFONTA* logFont = (LOGFONTA*)fontImpl->getFontHandleID(); font = CreateFontIndirectA( logFont ); } */ prepareDCWithContextFont( font ); HFONT oldFont = (HFONT)::SelectObject( dc_, font ); SIZE textSize = {0,0}; if ( System::isUnicodeEnabled() ) { GetTextExtentPoint32W( dc_, text.c_str(), text.size(), &textSize ); } else { GetTextExtentPoint32A( dc_, text.ansi_c_str(), text.size(), &textSize ); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?