void CSBoxToolTip::ShowTip(LPCTSTR lpszTipText) { if(lpszTipText==NULL) return; CString strTip(lpszTipText); if(m_hTextBitmap) DeleteObject(m_hTextBitmap); CDC *pDC = GetDC(); CDC MemDC; MemDC.CreateCompatibleDC(pDC); MemDC.SetBkMode(TRANSPARENT); MemDC.SetTextColor(m_crText); HGDIOBJ pOldFont = MemDC.SelectObject(&m_fontText); CSize sz = MemDC.GetTextExtent(strTip); int reminder = sz.cx % 4; sz.cx += 4 - reminder; MoveWindow(0,0,sz.cx+8,sz.cy+8); BITMAPINFOHEADER stBmpInfoHeader = { 0 }; int nBytesPerLine = ((sz.cx * 32 + 31) & (~31)) >> 3; //(sz.cx*32+31)/32*4; stBmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER); stBmpInfoHeader.biWidth = sz.cx; stBmpInfoHeader.biHeight = sz.cy; stBmpInfoHeader.biPlanes = 1; stBmpInfoHeader.biBitCount = 32; stBmpInfoHeader.biCompression = BI_RGB; stBmpInfoHeader.biClrUsed = 0; stBmpInfoHeader.biSizeImage = nBytesPerLine * sz.cy; PVOID pvBits = NULL; m_hTextBitmap = ::CreateDIBSection(NULL, (PBITMAPINFO)&stBmpInfoHeader, DIB_RGB_COLORS, &pvBits, NULL, 0); ASSERT(m_hTextBitmap != NULL); memset(pvBits,0,sz.cx*4*sz.cy); HGDIOBJ hbmpOld = MemDC.SelectObject( m_hTextBitmap ); MemDC.DrawText(strTip,strTip.GetLength(),CRect(0,0,sz.cx,sz.cy),DT_CENTER); POINT ptSrc = { 0, 0}; POINT ptWinPos = { 0, 0}; ClientToScreen(&ptWinPos); SIZE szWin = { sz.cx, sz.cy}; BYTE SourceConstantAlpha = 255; BLENDFUNCTION stBlend = { AC_SRC_OVER, 0, SourceConstantAlpha, AC_SRC_ALPHA }; BOOL b = UpdateLayeredWindow( pDC, &ptWinPos, &szWin, &MemDC, &ptSrc, 0, &stBlend, ULW_ALPHA ); MemDC.SelectObject(hbmpOld); MemDC.SelectObject(pOldFont); ReleaseDC(&MemDC); ReleaseDC(pDC); } 创建窗口 BOOL CSBoxToolTip::Create(const CRect& rect, CWnd* pParentWnd) { HWND hParent = NULL; if(pParentWnd) hParent = pParentWnd->m_hWnd; LPCTSTR lpszClassName = AfxRegisterWndClass( CS_VREDRAW | CS_HREDRAW, ::LoadCursor(NULL, IDC_ARROW), (HBRUSH) ::GetStockObject(NULL_BRUSH), ::LoadIcon(NULL, IDI_APPLICATION)); BOOL bRet = CreateEx(WS_EX_TRANSPARENT|WS_EX_LAYERED,lpszClassName,_T("SBoxToolTip"),WS_POPUP,rect.left,rect.top,rect.Width(),rect.Height(),hParent,NULL); return bRet; }