在DesktopLyricsDemo例子基础上修改
实现代码
BOOL CGDIAPPDlg::UpdateDisplay(int Transparent) { RECT rct; ::GetWindowRect(m_hWnd, &rct); int iWinRectW(rct.right - rct.left); int iWinRectH(rct.bottom - rct.top); HDC hdcTemp=GetDC()->m_hDC; m_hdcMemory=CreateCompatibleDC(hdcTemp);//hdcSrc HBITMAP hBitMap=CreateCompatibleBitmap(hdcTemp,iWinRectW,iWinRectH); SelectObject(m_hdcMemory,hBitMap); if(Transparent<0||Transparent>100) Transparent=100; m_Blend.SourceConstantAlpha=int(Transparent*2.55);//1~255 //封装一个 GDI+ 绘图图面。无法继承此类。 Graphics graphics(m_hdcMemory); //设置此 Graphics 的呈现质量 SmoothingModeAntiAlias 抗锯齿,平滑 graphics.SetSmoothingMode(SmoothingModeAntiAlias); //设置此 Graphics 关联的插补模式 InterpolationModeHighQualityBicubic 双立方体 graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic); ///显示矩形边框/ if(m_bShowClient) { SolidBrush rectBrush(Color(125,228,228,228)); //填充由一对坐标、一个宽度和一个高度指定的矩形的内部。 graphics.FillRectangle(&rectBrush//确定填充特性的 Brush ,3//要填充的矩形的左上角的 x 坐标 ,5//要填充的矩形的左上角的 y 坐标 ,iWinRectW-2//要填充的矩形的宽度 ,iWinRectH-2//要填充的矩形的高度 ); Pen pen1(Color(255,223,223,223)); Pen pen2(Color(255,223,223,223)); //绘制由坐标对、宽度和高度指定的矩形。 graphics.DrawRectangle(&pen1,2,6,iWinRectW-1,iWinRectW-1); // graphics.DrawRectangle(&pen2,1,5,iWinRectW+1,iWinRectW+1); } //显示png// Image image(L"ni_png_0120.png"); if(m_bShowImg) { int iImgX(10); int iImgY(10); int iImgSizeW(160); int iImgSizeH(160); graphics.DrawImage(&image,iImgX,iImgY,iImgSizeW,iImgSizeH); } //显示文字 wchar_t pszFontFamily[][80]={ {L"Arial Black"}, {L"Times New Roman"}, {L"华文楷体"}, {L"华文彩云"} }; //FontFamily fontFamily(L"Arial Black"); FontFamily fontFamily(pszFontFamily[3]); StringFormat strformat; strformat.SetAlignment(StringAlignmentCenter); Point pOrg(10,10); wchar_t pszbuf[][80]={{L"Hello everyone!"}, {L"Happy New Year!"}, {L"I wish you will lead a happy life!"}, {L"Love you!"}, {L"Thanks all the people on CP!"}, {L"I'm Luke Sky walker!"} }; GraphicsPath path;//表示一系列相互连接的直线和曲线。无法继承此类。 //向此路径添加文本字符串 path.AddString(pszbuf[m_iLyricsIndex] ,wcslen(pszbuf[m_iLyricsIndex]) ,&fontFamily ,FontStyleBold///,FontStyleBold//,FontStyleRegular//FontStyleBoldItalic ,50//,38 //FontSize ,m_rectText//它表示文本从其起始的点 ,&strformat); Color clrPenColor(155 ,255 ,0 ,0);//alpha, red, green, and blue (215,215,215) Pen pen(clrPenColor ,1//Real number that specifies the width of this pen's stroke ); graphics.DrawPath(&pen,&path); for(int i=1; i<9; i+=1) { Pen pen(Color(62, 0, 2, 2), (float)i); pen.SetLineJoin(LineJoinRound);//LineJoinRound 两线相交处圆滑处理 graphics.DrawPath(&pen, &path); } if(m_bLinearGradientBrush) { //使用线性渐变封装 Brush。无法继承此类。 LinearGradientBrush linGrBrush( Point(0,10)//表示线性渐变起始点的 Point 结构 ,Point(0,70)//表示线性渐变终结点的 Point 结构 ,Color(255,255,255,255)//表示线性渐变起始色的 Color 结构 ,Color(255,30,120,195)//表示线性渐变结束色的 Color 结构 ); LinearGradientBrush linGrBrushW( Point(0,10) ,Point(0,70) ,Color(255,255,255,255) ,Color(15,1,1,1) ); graphics.FillPath(&linGrBrush,&path); graphics.FillPath(&linGrBrushW,&path); } /显示覆盖文字 if(m_bWalkPath) { GraphicsPath WalkPath; WalkPath.AddString(pszbuf[m_iLyricsIndex] ,wcslen(pszbuf[m_iLyricsIndex]) ,&fontFamily ,FontStyleBold//,FontStyleBold//,FontStyleRegular//FontStyleBoldItalic ,50//,38 //FontSize ,m_rectText//,Point(10,10)//它表示文本从其起始的点 ,&strformat); graphics.SetClip(m_clipRect); SolidBrush FillBrush(Color(255,0,0,255)); graphics.FillPath(&FillBrush//确定填充特性的 Brush ,&WalkPath ); } /// POINT ptNewWinPos={rct.left,rct.top}; SIZE newSizeWindow={iWinRectW,iWinRectH}; POINT ptSrcWinPos={0,0}; DWORD dwExStyle=GetWindowLong(m_hWnd,GWL_EXSTYLE); if((dwExStyle&0x80000)!=0x80000) SetWindowLong(m_hWnd,GWL_EXSTYLE,dwExStyle^0x80000);//#define WS_EX_LAYERED 0x00080000 HDC hdcDstScreen=::GetDC (m_hWnd); //hdcDst //COLORREF crKey(RGB(0,0,255)); COLORREF crKey(RGB(255,255,255)); BOOL bRet=FALSE; bRet= UpdateLayeredWindow(m_hWnd//hwnd [in] Handle to a layered window. //A layered window is created by specifying WS_EX_LAYERED when creating the window with the CreateWindowEx function. ,hdcDstScreen//hdcDst [in] Handle to a device context (DC) for the screen. ,&ptNewWinPos//pptDst [in] Pointer to a POINT structure that specifies the new screen position of the layered window. ,&newSizeWindow//psize [in] Pointer to a SIZE structure that specifies the new size of the layered window. //If the size of the window is not changing, psize can be NULL. If hdcSrc is NULL, psize must be NULL. ,m_hdcMemory//hdcSrc [in] Handle to a DC for the surface that defines the layered window. ,&ptSrcWinPos//pptSrc [in] Pointer to a POINT structure that specifies the location of the layer in the device context. ,NULL//,crKey//crKey [in] Pointer to a COLORREF value that specifies the color key to be used when composing the layered window. ,&m_Blend//Pointer to a BLENDFUNCTION structure that specifies the transparency value to be used when composing the layered window ,2//dwFlags [in] This parameter can be one of the following values //#define ULW_COLORKEY 1 #define ULW_ALPHA 2 #define ULW_OPAQUE 4 ); DWORD dwErrCode(0); if (!bRet) { dwErrCode = GetLastError(); } ModifyStyleEx(0,WS_EX_TRANSPARENT);//WS_EX_TRANSPARENT That is, any windows that are beneath the window are not obscured by the window. //SetWindowPos(&wndBottom,ptNewWinPos.x,ptNewWinPos.y,newSizeWindow.cx,newSizeWindow.cy,SWP_SHOWWINDOW); 计算在指定font的hdc中字符串长度 unit:pixel/ string strText; strText = ws2s(L"大h"); SIZE szTextSize; GetTextExtentPoint32(hdcDstScreen,strText.c_str(),strText.length(),&szTextSize); /// graphics.ReleaseHDC(m_hdcMemory); ::ReleaseDC(m_hWnd,hdcDstScreen); hdcDstScreen=NULL; ::ReleaseDC(m_hWnd,hdcTemp); hdcTemp=NULL; DeleteObject(hBitMap); hBitMap=NULL; DeleteDC(m_hdcMemory); m_hdcMemory=NULL; return bRet; }