void CScrollV::DrawRange(CDC &dc, CRect rc, int nXPos) { Graphics graphics(dc); CRect rcTemp; rcTemp.SetRect(rc.left, rc.top, rc.left + rc.Width(), rc.top + m_sizeImage.cy); DrawImageFrame(graphics, m_pImage, rcTemp, nXPos, 0, m_sizeImage.cx, m_sizeImage.cy, 2); rcTemp.SetRect(rc.left, rc.top + m_sizeImage.cy, rc.left + rc.Width(), rc.bottom - m_sizeImage.cy); graphics.DrawImage(m_pImage, Rect(rcTemp.left, rcTemp.top, rcTemp.Width(), rcTemp.Height()), nXPos, m_sizeImage.cy, m_sizeImage.cx,m_sizeImage.cy , UnitPixel); rcTemp.SetRect(rc.left, rc.bottom - m_sizeImage.cy, rc.left + rc.Width(), rc.bottom); DrawImageFrame(graphics, m_pImage, rcTemp, nXPos, m_sizeImage.cy * 2, m_sizeImage.cx, m_sizeImage.cy, 2); }
// 画图片信息 void CPopupList::DrawWindowEx(CDC &dc, CRect rcClient) { int nItemCount = m_vecItem.size(); CRect rcItem; CSize sizeImage; Graphics graphics(dc); for(int i = 0; i < nItemCount; i++) { EditListItem &editListItem = m_vecItem.at(i); rcItem = editListItem.rcItem; sizeImage = editListItem.sizeImage; // 删除按钮 if((i == m_nHoverItem) && (m_pImageClose != NULL)) { graphics.DrawImage(m_pImageClose, RectF((Gdiplus::REAL)m_rcClose.left, (Gdiplus::REAL)m_rcClose.top, (Gdiplus::REAL)m_rcClose.Width(), (Gdiplus::REAL)m_rcClose.Height()), (Gdiplus::REAL)(m_buttonState * m_sizeClose.cx), 0, (Gdiplus::REAL)m_sizeClose.cx, (Gdiplus::REAL)m_sizeClose.cy, UnitPixel); } // 列表图片 if(editListItem.pImage) { CRect rcHead(rcItem.left + 1, rcItem.top + 2, rcItem.left + 1 + rcItem.Height() - 4, rcItem.top + 2 + rcItem.Height() - 4); graphics.DrawImage(editListItem.pImage, RectF((Gdiplus::REAL)rcHead.left, (Gdiplus::REAL)rcHead.top, (Gdiplus::REAL)rcHead.Width(), (Gdiplus::REAL)rcHead.Height()), 0, 0, (Gdiplus::REAL)editListItem.sizeImage.cx, (Gdiplus::REAL)editListItem.sizeImage.cy, UnitPixel); DrawImageFrame(graphics, m_pImageHead, rcHead, i == m_nHoverItem ? m_sizeHead.cx : 0, 0, m_sizeHead.cx, m_sizeHead.cy, 5); } } }
void CPhotoFrame::Draw(CDC &dc, CRect rcUpdate) { CRect rc = m_rc; if (m_state == pfDown) { rc.left += 2; rc.top += 2; m_state = pfDown; } Graphics graphics(dc); graphics.DrawImage(m_pImage, RectF(m_rc.left + 1, m_rc.top + 1, m_rc.Width() - 2, m_rc.Height() - 2), 0, 0, m_sizeImage.cx, m_sizeImage.cy, UnitPixel); if(m_pFrameImage && m_pImage) { DrawImageFrame(graphics, m_pFrameImage, m_rc, 0, 0, m_sizeFrameImage.cx, m_sizeFrameImage.cy, 4); } }
void CDuiSlider::DrawControl(CDC &dc, CRect rcUpdate) { int nWidth = m_rc.Width(); int nHeight = m_rc.Height(); if(!m_bUpdate) { UpdateMemDC(dc, nWidth, nHeight * 4); Graphics graphics(m_memDC); // 画4个状态的内存图片 for(int i = 0; i < 4; i++) { // 复制背景 m_memDC.BitBlt(0, i * nHeight, nWidth, nHeight, &dc, m_rc.left ,m_rc.top, SRCCOPY); int nPosY = i * nHeight; if(m_pImageForeGround != NULL) // 使用背景和前景图片画进度条 { if(m_pImageBackGround != NULL) // 画背景 { DrawImageFrameMID(graphics, m_pImageBackGround, CRect(0, nPosY + m_nThumbTop, nWidth, nPosY + m_nThumbTop + m_nSliderHeight), 0, 0, m_sizeBackGround.cx, m_sizeBackGround.cy, m_nHeadLength, 0, m_nHeadLength, 0); } if(m_nProgress != 0) // 画前景 { DrawImageFrameMID(graphics, m_pImageForeGround, CRect(0, nPosY + m_nThumbTop, nWidth * m_nProgress / m_nMaxProgress, nPosY + m_nThumbTop + m_nSliderHeight), 0, 0, m_sizeForeGround.cx, m_sizeForeGround.cy, m_nHeadLength, 0, m_nHeadLength, 0); } }else if(m_pImage != NULL) // 使用单张图片画进度条 { DrawImageFrame(graphics, m_pImage, CRect(0, nPosY + m_nThumbTop, nWidth, nPosY + m_nThumbTop + m_nSliderHeight), 0, 0, m_sizeImage.cx, m_sizeImage.cy, 2); if(m_nProgress != 0) { DrawImageFrame(graphics, m_pImage, CRect(0, nPosY + m_nThumbTop, nWidth * m_nProgress / m_nMaxProgress, nPosY + m_nThumbTop + m_nSliderHeight), m_sizeImage.cx, 0, m_sizeImage.cx, m_sizeImage.cy, 2); } } // 画滑块 if(m_pImageThumb != NULL) { // 计算滑块的位置 int nPos = (int)__max(m_rc.Width() * m_nProgress / m_nMaxProgress - m_nThumbWidth / 2, 0); nPos = (int)__min(nPos, m_rc.Width() - m_nThumbWidth); Rect rect(nPos, nPosY, m_nThumbWidth, m_nThumbHeight); graphics.DrawImage(m_pImageThumb, rect, i * m_sizeThumb.cx, 0, m_sizeThumb.cx, m_sizeThumb.cy, UnitPixel); } // 画进度文字 if(m_bShowText) { BSTR bsFont = m_strFont.AllocSysString(); FontFamily fontFamily(bsFont); Font font(&fontFamily, (REAL)m_nFontWidth, m_fontStyle, UnitPixel); ::SysFreeString(bsFont); SolidBrush solidBrush(m_clrText); graphics.SetTextRenderingHint( TextRenderingHintClearTypeGridFit ); // 设置水平和垂直对齐方式 DUI_STRING_ALIGN_DEFINE(); strFormat.SetFormatFlags( StringFormatFlagsNoClip | StringFormatFlagsMeasureTrailingSpaces); CString strText; // 只有最大值设置为100情况下才会显示百分号 strText.Format(_T("%s%d%s"), m_strTitle, m_nProgress, (m_nMaxProgress == 100) ? _T("%") : _T("")); BSTR bsTitle = strText.AllocSysString(); RectF rect((Gdiplus::REAL)(0), (Gdiplus::REAL)nPosY, (Gdiplus::REAL)nWidth, (Gdiplus::REAL)nHeight); graphics.DrawString(bsTitle, (INT)wcslen(bsTitle), &font, rect, &strFormat, &solidBrush); ::SysFreeString(bsTitle); } } } dc.BitBlt(m_rc.left,m_rc.top, m_rc.Width(), m_rc.Height(), &m_memDC, 0, m_enButtonState * nHeight, SRCCOPY); }
void CDuiEdit::DrawControl(CDC &dc, CRect rcUpdate) { Graphics graphics(dc); DrawImageFrame(graphics, m_pImage, m_rc, m_EditState * m_sizeImage.cx, 0, m_sizeImage.cx, m_sizeImage.cy, 4); if(m_pLeftImage) { CRect rc; rc.left = m_rc.left + 2; rc.top = m_rc.top + (m_rc.Height() - m_sizeLeftImage.cy) / 2; rc.right = rc.left + m_sizeLeftImage.cx; rc.bottom = rc.top + m_sizeLeftImage.cy; if(m_nLeftImageCount > m_buttonState) { graphics.DrawImage(m_pLeftImage, RectF((Gdiplus::REAL)rc.left , (Gdiplus::REAL)rc.top, (Gdiplus::REAL)rc.Width(), (Gdiplus::REAL)rc.Height()), (Gdiplus::REAL)(m_buttonState * m_sizeLeftImage.cx), 0, (Gdiplus::REAL)m_sizeLeftImage.cx, (Gdiplus::REAL)m_sizeLeftImage.cy, UnitPixel); }else { graphics.DrawImage(m_pLeftImage, RectF((Gdiplus::REAL)rc.left , (Gdiplus::REAL)rc.top, (Gdiplus::REAL)rc.Width(), (Gdiplus::REAL)rc.Height()), 0, 0, (Gdiplus::REAL)m_sizeLeftImage.cx, (Gdiplus::REAL)m_sizeLeftImage.cy, UnitPixel); } } if(m_pSmallImage) { CRect rc; rc.left = m_rc.right - m_sizeSmallImage.cx - 2; rc.top = m_rc.top + (m_rc.Height() - m_sizeSmallImage.cy) / 2; rc.right = rc.left + m_sizeSmallImage.cx; rc.bottom = rc.top + m_sizeSmallImage.cy; if(m_nSmallImageCount > m_buttonState) { graphics.DrawImage(m_pSmallImage, RectF((Gdiplus::REAL)rc.left , (Gdiplus::REAL)rc.top, (Gdiplus::REAL)rc.Width(), (Gdiplus::REAL)rc.Height()), (Gdiplus::REAL)(m_buttonState * m_sizeSmallImage.cx), 0, (Gdiplus::REAL)m_sizeSmallImage.cx, (Gdiplus::REAL)m_sizeSmallImage.cy, UnitPixel); }else { graphics.DrawImage(m_pSmallImage, RectF((Gdiplus::REAL)rc.left , (Gdiplus::REAL)rc.top, (Gdiplus::REAL)rc.Width(), (Gdiplus::REAL)rc.Height()), 0, 0, (Gdiplus::REAL)m_sizeSmallImage.cx, (Gdiplus::REAL)m_sizeSmallImage.cy, UnitPixel); } } BSTR bsFont = m_strFont.AllocSysString(); FontFamily fontFamily(bsFont); Font font(&fontFamily, (REAL)m_nFontWidth, m_fontStyle, UnitPixel); SolidBrush solidBrush(m_clrText); SolidBrush solidBrushTip(m_clrTooltip); graphics.SetTextRenderingHint( TextRenderingHintClearTypeGridFit ); ::SysFreeString(bsFont); StringFormat strFormat; strFormat.SetAlignment(StringAlignmentNear); // 水平方向左对齐 if(!m_bMultiLine) { // 单行文字 strFormat.SetLineAlignment(StringAlignmentCenter); // 垂直方向中间对齐 strFormat.SetFormatFlags( StringFormatFlagsNoWrap | StringFormatFlagsMeasureTrailingSpaces); }else { strFormat.SetLineAlignment(StringAlignmentNear); // 垂直方向上对齐 } RectF rect((Gdiplus::REAL)m_rcText.left, (Gdiplus::REAL)(m_rcText.top+2), (Gdiplus::REAL)m_rcText.Width(), (Gdiplus::REAL)(m_rcText.Height()-2)); if(!m_strTitle.IsEmpty()) { // 文字非空 CString strTitle = m_strTitle; if(m_bPassWord) { int nlen = strTitle.GetLength(); strTitle = ""; for(int i = 0; i < nlen; i++) { strTitle += '*'; } } BSTR bsTitle = strTitle.AllocSysString(); graphics.DrawString(bsTitle, (INT)wcslen(bsTitle), &font, rect, &strFormat, &solidBrush); ::SysFreeString(bsTitle); }else if(!m_strTooltip.IsEmpty()) { // 如果没有文字,但设置了tooltip,则显示tooltip BSTR bsTooltip = m_strTooltip.AllocSysString(); graphics.DrawString(bsTooltip, (INT)wcslen(bsTooltip), &font, rect, &strFormat, &solidBrushTip); ::SysFreeString(bsTooltip); } }
void CDlgPopup::DrawWindow(CDC *pDC) { if(!m_bInitFinish) return; CRect rcClient; GetClientRect(&rcClient); CDC MemDC; MemDC.CreateCompatibleDC(pDC); CBitmap memBit; //memBit.CreateCompatibleBitmap(pDC, rcClient.Width(), rcClient.Height()); // 因为直接使用CreateCompatibleBitmap在16位颜色下会有问题,因此改为调用DuiCreateCompatibleBitmap函数 HBITMAP hMemBitmap = DuiCreateCompatibleBitmap(rcClient); memBit.Attach(hMemBitmap); CBitmap *pOldBit = MemDC.SelectObject(&memBit); MemDC.SetBkMode(TRANSPARENT); if(m_pImage) { Graphics graphics(MemDC); if(m_enBackMode == enBMFrame) // 边框模式 { DrawImageFrame(graphics, m_pImage, rcClient, 0, 0, m_sizeBKImage.cx, m_sizeBKImage.cy, m_nFrameSize); }else if(m_enBackMode == enBMMID) // 九宫格边框模式 { DrawImageFrameMID(graphics, m_pImage, rcClient, 0, 0, m_sizeBKImage.cx, m_sizeBKImage.cy, m_nFrameWLT, m_nFrameHLT, m_nFrameWRB, m_nFrameHRB); }else // 图片模式 { graphics.DrawImage(m_pImage, Rect(0, 0, m_sizeBKImage.cx, m_sizeBKImage.cy), 0, 0, m_sizeBKImage.cx, m_sizeBKImage.cy, UnitPixel); } } else { MemDC.FillSolidRect(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), RGB(0, 147, 209)); MemDC.FillSolidRect(rcClient.left + 1, rcClient.top + 1, rcClient.Width() - 2, rcClient.Height() - 2, RGB(255, 255, 255)); } DrawWindow(MemDC, rcClient); DrawWindowEx(MemDC, rcClient); for (size_t i = 0; i < m_vecArea.size(); i++) { CControlBase * pControlBase = m_vecArea.at(i); if (pControlBase) { pControlBase->Draw(MemDC, rcClient); } } for (size_t i = 0; i < m_vecControl.size(); i++) { CControlBase * pControlBase = m_vecControl.at(i); if (pControlBase) { pControlBase->Draw(MemDC, rcClient); } } ClientToScreen(&rcClient); POINT pointDes; pointDes.x = rcClient.left; pointDes.y = rcClient.top; POINT pointSrc; pointSrc.x = 0; pointSrc.y = 0; SIZE sizeDes; sizeDes.cx = rcClient.Width(); sizeDes.cy = rcClient.Height(); BLENDFUNCTION blend; memset( &blend, 0, sizeof( blend) ); blend.AlphaFormat = AC_SRC_ALPHA ; blend.SourceConstantAlpha = m_nBackTranslucent; HWND hWnd = GetSafeHwnd(); SetWindowLong(hWnd,GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED); // 如果允许背景Alpha通道,则根据是否有背景图片决定是否使用Aplha通道 DWORD dwFlags = 0; if(m_bBackAlpha) { dwFlags = (m_pImage ? ULW_ALPHA : ULW_COLORKEY); } UpdateLayeredWindow(pDC, &pointDes, &sizeDes, &MemDC, &pointSrc, 0, &blend, dwFlags); MemDC.SelectObject(pOldBit); memBit.DeleteObject(); MemDC.DeleteDC(); }
void CWndPopup::DrawWindow() { CRect rcClient; GetClientRect(&rcClient); CDC *pDC = GetDC(); CDC MemDC; MemDC.CreateCompatibleDC(pDC); CBitmap memBit; memBit.CreateCompatibleBitmap(pDC, rcClient.Width(), rcClient.Height()); CBitmap *pOldBit = MemDC.SelectObject(&memBit); MemDC.SetBkMode(TRANSPARENT); if(m_pImage) { Graphics graphics(MemDC); DrawImageFrame(graphics, m_pImage, rcClient,0, 0, m_sizeBKImage.cx, m_sizeBKImage.cy, 32); } else { MemDC.FillSolidRect(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), RGB(0, 147, 209)); MemDC.FillSolidRect(rcClient.left + 1, rcClient.top + 1, rcClient.Width() - 2, rcClient.Height() - 2, RGB(255, 255, 255)); } CDC TempMemDC; TempMemDC.CreateCompatibleDC(&MemDC); TempMemDC.SetBkMode(TRANSPARENT); CBitmap TempMemBit; TempMemBit.CreateCompatibleBitmap(&MemDC, rcClient.Width(), rcClient.Height()); CBitmap *pTempOldBit = TempMemDC.SelectObject(&TempMemBit); TempMemDC.FillSolidRect(0, 0, rcClient.Width(), rcClient.Height(), RGB(255, 0, 255)); DrawWindow(TempMemDC, rcClient); DWORD dwSize = TempMemBit.GetBitmapBits(0,NULL); unsigned char *lpBuffer = new unsigned char[dwSize]; TempMemBit.GetBitmapBits(dwSize,lpBuffer); for( int i = 0 ; i+3 < dwSize ; i+=4 ) { if(lpBuffer[i] == 255 && lpBuffer[i + 1] == 0 && lpBuffer[i + 2] == 255) { lpBuffer[i] = 0; lpBuffer[i + 1] = 0; lpBuffer[i + 2] = 0; } else { if(lpBuffer[i] == 0 && lpBuffer[i + 1] == 0 && lpBuffer[i + 2] == 0) lpBuffer[i] = 1; lpBuffer[i+3] = 255; } } TempMemBit.SetBitmapBits(dwSize, lpBuffer); delete[] lpBuffer; BLENDFUNCTION blendTemp = {AC_SRC_OVER,0,255,AC_SRC_ALPHA}; MemDC.AlphaBlend(0, 0, rcClient.Width(), rcClient.Height(), &TempMemDC, 0, 0, rcClient.Width(), rcClient.Height(), blendTemp); TempMemDC.SelectObject(pTempOldBit); TempMemBit.DeleteObject(); TempMemDC.DeleteDC(); DrawWindowEx(MemDC, rcClient); DWORD dwSizeMen = memBit.GetBitmapBits(0,NULL); unsigned char *lpBufferMen = new unsigned char[dwSize]; memBit.GetBitmapBits(dwSizeMen,lpBufferMen); for( int i = 0 ; i+3 < dwSizeMen ; i+=4 ) { if(lpBufferMen[i] == 0 && lpBufferMen[i + 1] == 0 && lpBufferMen[i + 2] == 0) lpBufferMen[i] = 1; } memBit.SetBitmapBits(dwSizeMen, lpBufferMen); delete[] lpBufferMen; ClientToScreen(&rcClient); POINT pointDes; pointDes.x = rcClient.left; pointDes.y = rcClient.top; POINT pointSrc; pointSrc.x = 0; pointSrc.y = 0; SIZE sizeDes; sizeDes.cx = rcClient.Width(); sizeDes.cy = rcClient.Height(); BLENDFUNCTION blend; memset( &blend, 0, sizeof( blend) ); blend.AlphaFormat = AC_SRC_ALPHA ; blend.SourceConstantAlpha = 255; HWND hWnd = GetSafeHwnd(); SetWindowLong(hWnd,GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED); UpdateLayeredWindow(pDC, &pointDes, &sizeDes, &MemDC, &pointSrc, 0, &blend,m_pImage ? ULW_ALPHA : ULW_COLORKEY); MemDC.SelectObject(pOldBit); memBit.DeleteObject(); MemDC.DeleteDC(); ReleaseDC(pDC); }
void DrawRasterText(SDL_Surface* surface, SDL_Surface* destSurface, char string[], int x, int y, int charSize) { for(int i = 0; i < strlen(string); i++) DrawImageFrame(surface, destSurface, x+i*charSize, y, charSize, charSize, string[i]-32); }