bool TextNoOutlineStrategy::DrawString( Gdiplus::Graphics* pGraphics, Gdiplus::FontFamily* pFontFamily, Gdiplus::FontStyle fontStyle, int nfontSize, const wchar_t*pszText, Gdiplus::Rect rtDraw, Gdiplus::StringFormat* pStrFormat) { using namespace Gdiplus; GraphicsPath path; Status status = path.AddString(pszText,wcslen(pszText),pFontFamily,fontStyle,nfontSize,rtDraw,pStrFormat); if(status!=Ok) return false; Status status2 = Ok; if(m_bClrText) { SolidBrush brush(m_clrText); status2 = pGraphics->FillPath(&brush, &path); } else { status2 = pGraphics->FillPath(m_pbrushText, &path); } return status2 == Ok; }
bool TextDblOutlineStrategy::DrawString( Gdiplus::Graphics* pGraphics, Gdiplus::FontFamily* pFontFamily, Gdiplus::FontStyle fontStyle, int nfontSize, const wchar_t*pszText, Gdiplus::Point ptDraw, Gdiplus::StringFormat* pStrFormat) { using namespace Gdiplus; GraphicsPath path; Status status = path.AddString(pszText,wcslen(pszText),pFontFamily,fontStyle,nfontSize,ptDraw,pStrFormat); if(status!=Ok) return false; Pen pen2(m_clrOutline2,m_nThickness1+m_nThickness2); pen2.SetLineJoin(LineJoinRound); pGraphics->DrawPath(&pen2, &path); Pen pen1(m_clrOutline1,m_nThickness1); pen1.SetLineJoin(LineJoinRound); pGraphics->DrawPath(&pen1, &path); Status status2 = Ok; if(m_bClrText) { SolidBrush brush(m_clrText); status2 = pGraphics->FillPath(&brush, &path); } else { status2 = pGraphics->FillPath(m_pbrushText, &path); } return status2 == Ok; }
bool TextNoOutlineStrategy::MeasureString( Gdiplus::Graphics* pGraphics, Gdiplus::FontFamily* pFontFamily, Gdiplus::FontStyle fontStyle, int nfontSize, const wchar_t*pszText, Gdiplus::Rect rtDraw, Gdiplus::StringFormat* pStrFormat, float* pfPixelsStartX, float* pfPixelsStartY, float* pfDestWidth, float* pfDestHeight ) { using namespace Gdiplus; GraphicsPath path; Status status = path.AddString(pszText,wcslen(pszText),pFontFamily,fontStyle,nfontSize,rtDraw,pStrFormat); if(status!=Ok) return false; *pfDestWidth= rtDraw.GetLeft(); *pfDestHeight= rtDraw.GetTop(); bool b = GDIPath::MeasureGraphicsPath(pGraphics, &path, pfPixelsStartX, pfPixelsStartY, pfDestWidth, pfDestHeight); return b; }
TexturePod OverlayText(string message) { InitializeGdi(); PezConfig cfg = PezGetConfig(); // Skip GDI text generation if the string is unchanged: if (message == oc.PreviousMessage) return oc.MessageTexture; oc.PreviousMessage = message; // Create the GDI+ drawing context and set it up: Graphics* gfx = Graphics::FromImage(oc.GdiBitmap); gfx->Clear(Color::Transparent); gfx->SetSmoothingMode(SmoothingModeAntiAlias); gfx->SetInterpolationMode(InterpolationModeHighQualityBicubic); // Select a font: FontFamily fontFamily(L"Trebuchet MS"); const float fontSize = 14; PointF origin(10.0f, 10.0f); StringFormat format(StringAlignmentNear); // Create a path along the outline of the glyphs: GraphicsPath path; path.AddString( wstring(message.begin(), message.end()).c_str(), -1, &fontFamily, FontStyleRegular, fontSize, origin, &format); // Draw some glow to steer clear of crappy AA: for (float width = 0; width < 3; ++width) { Pen pen(Color(64, 0, 0, 0), width); pen.SetLineJoin(LineJoinRound); gfx->DrawPath(&pen, &path); } // Fill the glyphs: SolidBrush brush(Color(50, 100, 200)); gfx->FillPath(&brush, &path); // Lock the raw pixel data and pass it to OpenGL: BitmapData data; oc.GdiBitmap->LockBits(0, ImageLockModeRead, PixelFormat32bppARGB, &data); _ASSERT(data.Stride == sizeof(unsigned int) * cfg.Width); glBindTexture(GL_TEXTURE_2D, oc.MessageTexture.Handle); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cfg.Width, cfg.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.Scan0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); oc.GdiBitmap->UnlockBits(&data); return oc.MessageTexture; }
void GPDrawShadowText( Graphics& gc, CString& strTxtIn, CRect& rcIn, Gdiplus::Font& fontIn, ARGB BrushClrIn, ARGB PenClrIn , ARGB shadowPenClrIn /*= 0xff000000*/, ARGB shadowBrushClrIn /*= 0xff000000*/, int nOffXIn /*= 2*/, int nOffYIn /*= 2*/, StringFormat* fmtIn /*= NULL*/ ) { Gdiplus::Font& gcfont = fontIn; FontFamily fmy; gcfont.GetFamily(&fmy); int nfontStyle = gcfont.GetStyle(); REAL dFontSize = gcfont.GetSize(); Rect rcText = CRect2Rect(rcIn); StringFormat fmt; fmt.SetAlignment(StringAlignmentCenter); fmt.SetTrimming(StringTrimmingEllipsisWord); fmt.SetLineAlignment(StringAlignmentCenter); StringFormat& fmtUse = fmtIn == NULL? fmt:*fmtIn; GraphicsContainer gcContainer = gc.BeginContainer(); gc.SetSmoothingMode(SmoothingModeAntiAlias); CComBSTR btrTxtIn(strTxtIn); GraphicsPath textPath; textPath.AddString(btrTxtIn, -1, &fmy, nfontStyle, dFontSize, rcText, &fmtUse); Matrix mx; mx.Translate(nOffXIn, nOffYIn); textPath.Transform(&mx); Pen textPen(ARGB2Color(shadowPenClrIn), 1); SolidBrush textbrush(ARGB2Color(shadowBrushClrIn)); textPen.SetLineJoin(LineJoinRound); if (shadowBrushClrIn != 0) { gc.FillPath(&textbrush, &textPath); } if (shadowPenClrIn != 0) { gc.DrawPath(&textPen, &textPath); } mx.Invert(); textPath.Transform(&mx); textPen.SetColor(ARGB2Color(PenClrIn)); textbrush.SetColor(ARGB2Color(BrushClrIn)); if (BrushClrIn != 0) { gc.FillPath(&textbrush, &textPath); } if (PenClrIn != 0) { gc.DrawPath(&textPen, &textPath); } gc.EndContainer(gcContainer); }
wxGraphicsPath OOPDesktopLyric::InitLineTextPath(wxGraphicsContext *gc) { wxASSERT(gc); // 文本轮廓 wxGraphicsPen pen = gc->CreatePen(wxPen(wxColour(0,0,0,255), 3)); const wxString textToDraw(IsCurrLineValid() ? (*m_currLine)->GetLyric() : GetInteractiveOutput()); wxGraphicsPath path = gc->CreatePath(); //======================================== // 添加文本 FontFamily fontFamily(m_style.fontFace); StringFormat stringFormat; wxCoord winHeight; GetSize(NULL, &winHeight); wxASSERT(size_t(winHeight) >= m_style.pxFontSize); GraphicsPath *nativePath = (GraphicsPath *) path.GetNativePath(); nativePath->AddString(textToDraw, -1, &fontFamily, m_style.bold ? FontStyleBold : FontStyleRegular, m_style.pxFontSize, Point(0, (winHeight - m_style.pxFontSize) / 2), &stringFormat); WXGDIPlusPenData *penImpl = (WXGDIPlusPenData *) pen.GetGraphicsData(); RectF bounds; nativePath->GetBounds(&bounds, NULL, penImpl->m_pen); m_textPathBounds.m_x = bounds.X; m_textPathBounds.m_y = bounds.Y; m_textPathBounds.m_width = bounds.Width; m_textPathBounds.m_height = bounds.Height; path.UnGetNativePath(nativePath); //======================================== // 添加到路径中 gc->SetPen(pen); gc->StrokePath(path); return path; }
int MusicUtils::TestStringCx(const wchar_t *szText) { //CDC tdc; //tdc.CreateCompatibleDC(CDC::FromHandle(::GetDC(0))); //HBITMAP hbitmap = CreateCompatibleBitmap(tdc.m_hDC,600,15); //tdc.SelectObject(hbitmap); //return tdc.GetOutputTextExtent(szText).cx; GraphicsPath path; FontFamily fontfamily; Font font(L"宋体",14); font.GetFamily(&fontfamily); StringFormat strFormat; strFormat.SetAlignment(StringAlignmentNear); path.AddString(szText,-1,&fontfamily,font.GetStyle(),font.GetSize(),PointF(0,0),&strFormat); RectF rcBound; path.GetBounds(&rcBound); return rcBound.Width; }
// 获取文字需要的显示区域 Size GetTextBounds(const Font& font,const StringFormat& strFormat,const CString& strText) { GraphicsPath path; FontFamily fontfamily; font.GetFamily(&fontfamily); BSTR bsText = strText.AllocSysString(); path.AddString(bsText,-1,&fontfamily,font.GetStyle(),font.GetSize(),PointF(0,0),&strFormat); ::SysFreeString(bsText); RectF rcBound; path.GetBounds(&rcBound); REAL rHeight = font.GetHeight(0.0f); return Size((int)(rcBound.Width > (int)rcBound.Width ? rcBound.Width + 1 : rcBound.Width), (int)(rHeight > (int)rHeight ? rHeight + 4 : rHeight + 1)); //return Size((int)(rcBound.Width > (int)rcBound.Width ? rcBound.Width + 1 : rcBound.Width), // (int)(rcBound.Height > (int)rcBound.Height ? rcBound.Height + 2 : rcBound.Height + 1)); }
// 获取文字需要的显示区域(使用非换行的默认格式) Size GetTextBounds(const Font& font,const CString& strText) { StringFormat strFormat; strFormat.SetAlignment(StringAlignmentNear); strFormat.SetFormatFlags( StringFormatFlagsNoWrap | StringFormatFlagsMeasureTrailingSpaces); GraphicsPath path; FontFamily fontfamily; font.GetFamily(&fontfamily); BSTR bsText = strText.AllocSysString(); path.AddString(bsText,-1,&fontfamily,font.GetStyle(),font.GetSize(),PointF(0,0),&strFormat); ::SysFreeString(bsText); RectF rcBound; path.GetBounds(&rcBound); REAL rHeight = font.GetHeight(0.0f); return Size((int)(rcBound.Width > (int)rcBound.Width ? rcBound.Width + 1 : rcBound.Width), (int)(rHeight > (int)rHeight ? rHeight + 4 : rHeight + 1)); //return Size((int)(rcBound.Width > (int)rcBound.Width ? rcBound.Width + 1 : rcBound.Width), // (int)(rcBound.Height > (int)rcBound.Height ? rcBound.Height + 2 : rcBound.Height + 1)); }
int Window::drawLine(HDC hdc, int oy, std::wstring string, TextFont* fnt) { using namespace Gdiplus; int x = (showed ? 0 : leftMargin) - 5; int y = (showed ? 0 : topMargin) + oy; const wchar_t* text = string.c_str(); if (fnt == 0) fnt = font; Graphics graphics(hdc); graphics.SetSmoothingMode(SmoothingModeAntiAlias); FontFamily fontFamily; fnt->GetFamily(&fontFamily); RectF boundRect = fnt->getTextBounds(hdc, clientRect, text); x += ((clientRect.right - clientRect.left) - (int)(boundRect.GetRight() - boundRect.GetLeft()) - 2) / 2; y -= ((int)boundRect.GetBottom()) + 5; StringFormat strformat; GraphicsPath path; path.AddString(text, wcslen(text), &fontFamily, fnt->GetStyle(), graphics.GetDpiY() * fnt->GetSize() / 72, Gdiplus::Point(x, y), &strformat ); // Outline color + size Pen pen(Color(0, 0, 0), fnt->GetSize()/7); pen.SetLineJoin(LineJoinRound); graphics.DrawPath(&pen, &path); // Text color SolidBrush brush(Color(254, 254, 254)); graphics.FillPath(&brush, &path); Rect bounds; path.GetBounds(&bounds, 0, &pen); return (int)boundRect.GetBottom(); }
bool DiffusedShadowStrategy::DrawString( Gdiplus::Graphics* pGraphics, Gdiplus::FontFamily* pFontFamily, Gdiplus::FontStyle fontStyle, int nfontSize, const wchar_t*pszText, Gdiplus::Rect rtDraw, Gdiplus::StringFormat* pStrFormat) { using namespace Gdiplus; GraphicsPath path; Status status = path.AddString(pszText,wcslen(pszText),pFontFamily,fontStyle,nfontSize,rtDraw,pStrFormat); if(status!=Ok) return false; for(int i=1; i<=m_nThickness; ++i) { Pen pen(m_clrOutline,i); pen.SetLineJoin(LineJoinRound); pGraphics->DrawPath(&pen, &path); } Status status2 = Ok; if(m_bOutlinetext==false) { for(int i=1; i<=m_nThickness; ++i) { SolidBrush brush(m_clrText); status2 = pGraphics->FillPath(&brush, &path); } } else { SolidBrush brush(m_clrText); status2 = pGraphics->FillPath(&brush, &path); } return status2 == Ok; }
void CMyDlg::OnPaint() { CPaintDC dc(this); CRect rcClient; GetClientRect(&rcClient); Graphics graphics(dc); if (version&&bIsAero) graphics.Clear(Color.Black); else graphics.Clear(Color.White); Bitmap CacheImage(rcClient.Width(),rcClient.Height()); Graphics buffer(&CacheImage); buffer.SetSmoothingMode(SmoothingModeAntiAlias); buffer.SetInterpolationMode(InterpolationModeHighQualityBicubic); Image *logo; Image *button; ImageFromIDResource(2,"png",logo); ImageFromIDResource(1,"png",button); buffer.DrawImage(logo, -5,20); buffer.DrawImage(button, 165,122); buffer.DrawImage(button, 350,122); FontFamily fontFamily(version?L"微软雅黑":L"宋体"); StringFormat strformat; wchar_t pszbuf[512]; wsprintfW(pszbuf,L"本程序适用于 迅雷5.9 系列\n如果本软件有错,我概不负责,但我会尽力解决。\n只有极少数时候需要您指定安装目录(比如非官方版)。\n如果会员补丁失效,重新破解即可。\n\n\n请选择: 一键增强 %s取消增强\n\n2010年4月7日更新 www.shuax.com",version?L" ":L" "); GraphicsPath path; path.AddString(pszbuf, wcslen(pszbuf), &fontFamily, FontStyleRegular, version?15:16, Gdiplus::Point(version?90:80,version?9:20), &strformat ); Pen pen(Color(18, 255, 255, 255), 3.0); //pen.SetLineJoin(LineJoinRound); buffer.DrawPath(&pen, &path); SolidBrush brush(Color(0,0,0)); buffer.FillPath(&brush, &path); graphics.DrawImage(&CacheImage, 0, 0); graphics.ReleaseHDC(dc); }
bool TextDblOutlineStrategy::MeasureString( Gdiplus::Graphics* pGraphics, Gdiplus::FontFamily* pFontFamily, Gdiplus::FontStyle fontStyle, int nfontSize, const wchar_t*pszText, Gdiplus::Rect rtDraw, Gdiplus::StringFormat* pStrFormat, float* pfPixelsStartX, float* pfPixelsStartY, float* pfDestWidth, float* pfDestHeight ) { using namespace Gdiplus; GraphicsPath path; Status status = path.AddString(pszText,wcslen(pszText),pFontFamily,fontStyle,nfontSize,rtDraw,pStrFormat); if(status!=Ok) return false; *pfDestWidth= rtDraw.GetLeft(); *pfDestHeight= rtDraw.GetTop(); bool b = GDIPath::MeasureGraphicsPath(pGraphics, &path, pfPixelsStartX, pfPixelsStartY, pfDestWidth, pfDestHeight); if(false==b) return false; float pixelThick = 0.0f; b = GDIPath::ConvertToPixels(pGraphics,m_nThickness1+m_nThickness2,0.0f,NULL,NULL,&pixelThick,NULL); if(false==b) return false; *pfDestWidth += pixelThick; *pfDestHeight += pixelThick; return true; }
STDMETHODIMP CTextOverlay::Render(LONG hdc) { PointF zero(0.0, 0.0); // Get the text we want to draw! BSTR text; IMeter *iMeter = 0; get_meter(&iMeter); if (!iMeter) { text = format.copy(); } else { _bstr_t tmp(""); BSTR tCopy = tmp.copy(); text = tCopy; iMeter->GetAsString(format, selector, &text); if (text != tCopy) ::SysFreeString(tCopy); } // Create graphics object Graphics graphics((HDC)hdc); graphics.SetInterpolationMode(InterpolationModeHighQuality); graphics.SetPixelOffsetMode(PixelOffsetModeHalf); graphics.SetSmoothingMode(SmoothingModeAntiAlias); graphics.SetTextRenderingHint(getDefaultRenderingHint(textRenderingHint)); graphics.SetCompositingMode(CompositingModeSourceOver); graphics.SetTextContrast(textContrast); // Calculate world transformation REAL el[6]; CumulativeTransform(el); Matrix matrix(el[0], el[1], el[2], el[3], el[4], el[5]); // matrix.Invert(); graphics.SetTransform(&matrix); // Create fontFamily for path-based font rendering BSTR fName; get_FontName(&fName); FontFamily fontFamily(fName); ::SysFreeString(fName); // Get some extra stuff for path-based rendering int fStyle; get_FontStyle(&fStyle); float fSize; get_FontSize(&fSize); // Set font for alternate rendering // Font font((HDC)hdc, &logFont); Font font(&fontFamily, fSize, fStyle, UnitPixel); // Set font color SolidBrush fg(color); // Set up string format StringFormat format(vertical ? StringFormatFlagsDirectionVertical : 0, LANG_NEUTRAL); if (!wrap) format.SetFormatFlags(format.GetFormatFlags() | StringFormatFlagsNoWrap); format.SetAlignment((StringAlignment)hAlign); format.SetLineAlignment((StringAlignment)vAlign); // Calculate clipping rectangle RectF clipRect(0.0, 0.0, (float)width, (float)height); if (hAlign == 1) // Center clipRect.X = (float)-width/2; else if (hAlign == 2) // Right clipRect.X = (float)-width; if (vAlign == 1) // Center clipRect.Y = (float)-height/2; else if (vAlign == 2) // Right clipRect.Y = (float)-height; // Draw blur if (radius > 0) { GraphicsPath blurPath; if (width > -1 && height > -1) { blurPath.AddString(text, -1, &fontFamily, fStyle, fSize, clipRect, &format); } else { blurPath.AddString(text, -1, &fontFamily, fStyle, fSize, zero, &format); } BYTE alpha; get_Alpha(&alpha); for (int i=radius; i>0; i--) { Color _blurColor((BYTE)(alpha/radius), (BYTE)(blurColor >> Color::BlueShift), (BYTE)(blurColor >> Color::GreenShift), (BYTE)(blurColor >> Color::RedShift)); if (blurColor == -1) { _blurColor = Color(alpha/radius, color.GetRed(), color.GetGreen(), color.GetBlue()); } Pen blurPen(_blurColor, (float)2*i); blurPen.SetEndCap(LineCapRound); blurPen.SetMiterLimit(1); graphics.DrawPath(&blurPen, &blurPath); } GraphicsState state = graphics.Save(); graphics.SetClip(&blurPath); Color trans(0, 0, 0, 0); SolidBrush brush(trans); graphics.FillPath(&brush, &blurPath); graphics.Restore(state); }
void ProcessDateTime::UpdateTexture() { Status stat; RectF layoutBox; SIZE textSize; RectF boundingBox(0.0f, 0.0f, 32.0f, 32.0f); UpdateCurrentText(); if (bUpdateTexture) { bUpdateTexture = false; if (hFont) { DeleteObject(hFont); hFont = NULL; } hFont = GetFont(); if (!hFont) return; } StringFormat format(Gdiplus::StringFormat::GenericTypographic()); SetStringFormat(format); HDC hdc = CreateCompatibleDC(NULL); Font font(hdc, hFont); Graphics graphics(hdc); graphics.SetTextRenderingHint(TextRenderingHintAntiAlias); if (strCurrentText.IsValid()) { stat = graphics.MeasureString(strCurrentText, -1, &font, PointF(0.0f, 0.0f), &format, &boundingBox); if (stat != Ok) Log::writeError(LOG_RTSPSERV, 1, "TextSource::UpdateTexture: Gdiplus::Graphics::MeasureString failed: %u", (int)stat); if (bUseOutline) { //Note: since there's no path widening in DrawOutlineText the padding is half than what it was supposed to be. boundingBox.Width += outlineSize; boundingBox.Height += outlineSize; } } DeleteDC(hdc); hdc = NULL; if (boundingBox.Height < size) { textSize.cy = size; boundingBox.Height = float(size); } else textSize.cy = LONG(boundingBox.Height + EPSILON); textSize.cx = LONG(boundingBox.Width + EPSILON); //textSize.cx &= 0xFFFFFFFE; //textSize.cy &= 0xFFFFFFFE; textSize.cx += textSize.cx % 2; textSize.cy += textSize.cy % 2; ClampVal(textSize.cx, 32, 8192); ClampVal(textSize.cy, 32, 8192); //---------------------------------------------------------------------- // write image { HDC hTempDC = CreateCompatibleDC(NULL); BITMAPINFO bi; zero(&bi, sizeof(bi)); void* lpBits; BITMAPINFOHEADER &bih = bi.bmiHeader; bih.biSize = sizeof(bih); bih.biBitCount = 32; bih.biWidth = textSize.cx; bih.biHeight = textSize.cy; bih.biPlanes = 1; HBITMAP hBitmap = CreateDIBSection(hTempDC, &bi, DIB_RGB_COLORS, &lpBits, NULL, 0); Bitmap bmp(textSize.cx, textSize.cy, 4 * textSize.cx, PixelFormat32bppARGB, (BYTE*)lpBits); Graphics graphics(&bmp); SolidBrush brush(Gdiplus::Color(GetAlphaVal(opacity) | (color & 0x00FFFFFF))); DWORD bkColor; bkColor = ((strCurrentText.IsValid()) ? GetAlphaVal(0) : GetAlphaVal(0)); if (textSize.cx > boundingBox.Width || textSize.cy > boundingBox.Height) { stat = graphics.Clear(Gdiplus::Color(0x00000000)); if (stat != Gdiplus::Ok) Log::writeError(LOG_RTSPSERV, 1, "TextSource::UpdateTexture: Graphics::Clear failed: %u", (int)stat); SolidBrush *bkBrush = new SolidBrush(Gdiplus::Color(bkColor)); graphics.FillRectangle(bkBrush, boundingBox); delete bkBrush; } else { stat = graphics.Clear(Gdiplus::Color(bkColor)); if (stat != Ok) Log::writeError(LOG_RTSPSERV, 1, "TextSource::UpdateTexture: Graphics::Clear failed: %u", (int)stat); } graphics.SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAlias); graphics.SetCompositingMode(Gdiplus::CompositingModeSourceOver); graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias); if (strCurrentText.IsValid()) { if (bUseOutline) { boundingBox.Offset(outlineSize / 2, outlineSize / 2); FontFamily fontFamily; GraphicsPath path; font.GetFamily(&fontFamily); path.AddString(strCurrentText, -1, &fontFamily, font.GetStyle(), font.GetSize(), boundingBox, &format); DrawOutlineText(&graphics, font, path, format, &brush); } else { stat = graphics.DrawString(strCurrentText, -1, &font, boundingBox, &format, &brush); if (stat != Gdiplus::Ok) Log::writeError(LOG_RTSPSERV, 1, "TextSource::UpdateTexture: Graphics::DrawString failed: %u", (int)stat); } } //---------------------------------------------------------------------- // upload texture if (textureSize.cx != textSize.cx || textureSize.cy != textSize.cy) { if (texture) { delete texture; texture = NULL; } mcpy(&textureSize, &textSize, sizeof(textureSize)); texture = CreateTexture(textSize.cx, textSize.cy, GS_BGRA, lpBits, FALSE, FALSE); } else if (texture) SetImage(texture,lpBits, GS_IMAGEFORMAT_BGRA, 4 * textSize.cx); if (!texture) { Log::writeError(LOG_RTSPSERV,1,"TextSource::UpdateTexture: could not create texture"); DeleteObject(hFont); } DeleteDC(hTempDC); DeleteObject(hBitmap); } }
BOOL GDIPluseExt::DrawBoardsText(Graphics& gp,PointF startpf,CString strText,CString strFont,INT nfontsize,Color textcolor,Color boardscolor) { gp.SetSmoothingMode(SmoothingModeAntiAlias); gp.SetInterpolationMode(InterpolationModeHighQualityBicubic); #ifdef _UNICODE StringFormat strformat; FontFamily fontFamily(strFont); GraphicsPath path; path.AddString(strText,strText.GetLength(), &fontFamily, FontStyleRegular, (REAL)nfontsize, startpf, &strformat ); for(int i=1; i < 4; ++i) { Pen pen(boardscolor, (REAL)i); pen.SetLineJoin(LineJoinRound); gp.DrawPath(&pen, &path); } SolidBrush textBrush(textcolor); gp.FillPath(&textBrush, &path); #else //字符转换 int font_len = strFont.GetLength(); WCHAR* pfont_w = new WCHAR[font_len]; MultiByteToWideChar(CP_ACP,0,strFont.GetBuffer(),-1,pfont_w,font_len); strFont.ReleaseBuffer(); //字符转换 int text_len = strText.GetLength(); WCHAR* ptext_w = new WCHAR[text_len]; MultiByteToWideChar(CP_ACP,0,strText.GetBuffer(),-1,ptext_w,text_len); strText.ReleaseBuffer(); FontFamily fontFamily(pfont_w); Font font(&fontFamily, (REAL)nfontsize, FontStyleRegular, UnitPixel); GraphicsPath path; StringFormat strformat; path.AddString(ptext_w,wcsnlen_s(ptext_w,text_len), &fontFamily, font.GetStyle(), font.GetSize(), startpf, &strformat ); for(int i=1; i < 4; ++i) { Pen pen(boardscolor,(REAL)i); pen.SetLineJoin(LineJoinRound); gp.DrawPath(&pen, &path); } SolidBrush textBrush(textcolor); gp.FillPath(&textBrush, &path); DEL_P(ptext_w); DEL_P(pfont_w); #endif return TRUE; }
// // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_COMMAND: { int wmId = LOWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code that uses hdc here... { Graphics graphics(hdc); Color color; color.SetFromCOLORREF(params.m_ccBackground.rgbResult); graphics.Clear(color); HFONT fnIndirect = CreateFontIndirect(params.m_cf.lpLogFont); Font font(hdc, fnIndirect); if (params.m_bAntialiasing) graphics.SetSmoothingMode(SmoothingModeHighQuality); Matrix matrix; RectF rect; graphics.MeasureString(params.m_text.c_str(), params.m_text.length(), &font, params.m_pStart, &rect); matrix.Translate(params.m_pStart.X + rect.Width / 2, params.m_pStart.Y + rect.Height / 2); matrix.Rotate(params.m_fRotAngle); matrix.Scale(params.m_fScale, params.m_fScale, MatrixOrderAppend); graphics.MultiplyTransform(&matrix); StringFormat strformat; GraphicsPath path; FontFamily fnFamily; font.GetFamily(&fnFamily); path.AddString(params.m_text.c_str(), params.m_text.length(), &fnFamily, font.GetStyle(), font.GetSize(), PointF(-rect.Width / 2, -rect.Height / 2), strformat.GenericTypographic()); color.SetFromCOLORREF(params.m_ccCircuit.rgbResult); Pen pen(color, 3); pen.SetLineJoin(LineJoinRound); graphics.DrawPath(&pen, &path); color.SetFromCOLORREF(params.m_ccFill.rgbResult); SolidBrush brush(color); graphics.FillPath(&brush, &path); Region region(&path); hRgn = region.GetHRGN(&graphics); graphics.ResetTransform(); RECT rc; GetWindowRect(hWnd, &rc); POINT pt; pt.x = rc.left; pt.y = rc.top; ScreenToClient(hWnd, &pt); matrix.Translate((REAL)-pt.x, (REAL)-pt.y, MatrixOrderAppend); region.Transform(&matrix); HRGN hRgnFr = region.GetHRGN(&graphics); OffsetRect(&rc, -rc.left, -rc.top); /*HRGN hRgnWnd = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom); CombineRgn(hRgn, hRgn, hRgnWnd, RGN_XOR); CombineRgn(hRgnFr, hRgnFr, hRgnWnd, RGN_XOR);*/ SetWindowRgn(hWnd, params.m_bNonRectRg ? hRgnFr : NULL, TRUE); } EndPaint(hWnd, &ps); } break; case WM_LBUTTONDOWN: { int xPos = GET_X_LPARAM(lParam); int yPos = GET_Y_LPARAM(lParam); if (PtInRegion(hRgn, xPos, yPos)) { DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_TEXT_PARAMS), hWnd, TextParams); InvalidateRect(hWnd, NULL, TRUE); } } break; case WM_ERASEBKGND: return TRUE; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
bool RFontTexture::MakeFontBitmap(HFONT hFont, RCHARINFO *pInfo, const TCHAR* szText, int nOutlineStyle, DWORD nColorArg1, DWORD nColorArg2) { HFONT hPrevFont = (HFONT)SelectObject(m_hDC, hFont); SIZE size; GetTextExtentPoint32(m_hDC, szText, _tcslen(szText), &size); int nWidth = min(size.cx,m_nCellSize); #ifdef _USE_GDIPLUS // GDI+ Font WCHAR wstrText[256]; int nTextLen = strlen(szText)+1; MultiByteToWideChar(CP_ACP, 0, szText, -1, wstrText, nTextLen-1); wstrText[nTextLen-1] = 0; Graphics graphics(m_hDC); Gdiplus::Font font(m_hDC, hFont); // graphics.SetTextRenderingHint(TextRenderingHintAntiAlias); graphics.Clear(Color(0,0,0,0)); const StringFormat* pTypoFormat = StringFormat::GenericTypographic(); if (nOutlineStyle == 0) { SolidBrush solidBrush(Color(255, 255, 255, 255)); graphics.DrawString(wstrText, -1, &font, PointF(0.0f, 0.0f), pTypoFormat, &solidBrush); } else if (nOutlineStyle == 1) // 아웃라인이 그려진다 { GraphicsPath path; FontFamily fontFamily; font.GetFamily(&fontFamily); TEXTMETRIC tm; GetTextMetrics(m_hDC, &tm); int nHeight; nHeight = min( (int)tm.tmHeight, (int)m_nCellSize-2); path.AddString( wstrText, -1, &fontFamily, FontStyleBold, nHeight, PointF(-1.0f, -1.0f), pTypoFormat); graphics.SetSmoothingMode(SmoothingModeAntiAlias); Pen pen(Color(nColorArg2), 2.0f); graphics.DrawPath(&pen, &path); SolidBrush brush(Color((ARGB)nColorArg1)); graphics.FillPath((Brush*)&brush, &path); } else if (nOutlineStyle == 2) // 그림자만 포함된다. 좌측의 한픽셀 여백이 있으므로 왼쪽으로 한픽셀. 자간은 같다 { SolidBrush solidBrush2(Color((ARGB)nColorArg2)); graphics.DrawString(wstrText, -1, &font, PointF(1.0f, 1.0f), pTypoFormat, &solidBrush2); SolidBrush solidBrush1(Color((ARGB)nColorArg1)); graphics.DrawString(wstrText, -1, &font, PointF(0.0f, 0.0f), pTypoFormat, &solidBrush1); // 숫자일 경우엔 자간이 좁기 때문에 1을 늘린다. char chChar = (char)wstrText[0]; if ( (chChar >= '0') && (chChar <= '9')) nWidth++; } #else SetTextColor(m_hDC, RGB(255,255,255)); SetBkColor(m_hDC, 0x00000000); SetTextAlign(m_hDC, TA_TOP); ExtTextOut(m_hDC, 0, 0, ETO_OPAQUE, NULL, szText, _tcslen(szText), NULL); #endif bool bRet = UploadTexture(pInfo,m_pBitmapBits,nWidth,m_nCellSize); // _ASSERT(bRet); SelectObject(m_hDC, hPrevFont); return bRet; }