1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| HRESULT BlendText(HWND hwndApp, TCHAR *szNewText)
{
LONG cx, cy;
HRESULT hr;
// Read the default video size
hr = pWC->GetNativeVideoSize(&cx, &cy, NULL, NULL);
if (FAILED(hr))
{
Msg(TEXT("GetNativeVideoSize FAILED! hr=0x%x\r\n"), hr);
return hr;
}
// Create a device context compatible with the current window
HDC hdc = GetDC(hwndApp);
HDC hdcBmp = CreateCompatibleDC(hdc);
// Write with a known font by selecting it into our HDC
HFONT hOldFont = (HFONT) SelectObject(hdcBmp, g_hFont);
// Determine the length of the string, then determine the
// dimensions (in pixels) of the character string using the
// currently selected font. These dimensions are used to create
// a bitmap below.
int nLength, nTextBmpWidth, nTextBmpHeight;
SIZE sz={0};
nLength = _tcslen(szNewText);
GetTextExtentPoint32(hdcBmp, szNewText, nLength, &sz);
nTextBmpHeight = sz.cy;
nTextBmpWidth = sz.cx;
// Create a new bitmap that is compatible with the current window
HBITMAP hbm = CreateCompatibleBitmap(hdc, nTextBmpWidth, nTextBmpHeight);
ReleaseDC(hwndApp, hdc);
// Select our bitmap into the device context and save the old one
BITMAP bm;
HBITMAP hbmOld;
GetObject(hbm, sizeof(bm), &bm);
hbmOld = (HBITMAP)SelectObject(hdcBmp, hbm);
// Set initial bitmap settings
RECT rcText;
SetRect(&rcText, 0, 0, nTextBmpWidth, nTextBmpHeight);
SetBkColor(hdcBmp, RGB(255, 255, 255)); // Pure white background
SetTextColor(hdcBmp, g_rgbColors);
// Draw the requested text string onto the bitmap
TextOut(hdcBmp, 0, 0, szNewText, nLength);
// Configure the VMR's bitmap structure
VMRALPHABITMAP bmpInfo;
ZeroMemory(&bmpInfo, sizeof(bmpInfo) );
bmpInfo.dwFlags = VMRBITMAP_HDC;
bmpInfo.hdc = hdcBmp; // DC which has selected our bitmap
// Remember the width of this new bitmap
g_nImageWidth = bm.bmWidth;
// Save the ratio of the bitmap's width to the width of the video file.
// This value is used to reposition the bitmap in composition space.
g_fBitmapCompWidth = (float)g_nImageWidth / (float)cx;
// Display the bitmap in the bottom right corner.
// rSrc specifies the source rectangle in the GDI device context
// rDest specifies the destination rectangle in composition space (0.0f to 1.0f)
bmpInfo.rDest.left = 0.0f + X_EDGE_BUFFER;
bmpInfo.rDest.right = 1.0f - X_EDGE_BUFFER;
bmpInfo.rDest.top = (float)(cy - bm.bmHeight) / (float)cy - Y_EDGE_BUFFER;
bmpInfo.rDest.bottom = 1.0f - Y_EDGE_BUFFER;
bmpInfo.rSrc = rcText;
// Transparency value 1.0 is opaque, 0.0 is transparent.
bmpInfo.fAlpha = TRANSPARENCY_VALUE;
// Set the COLORREF so that the bitmap outline will be transparent
SetColorRef(bmpInfo);
// Give the bitmap to the VMR for display
hr = pBMP->SetAlphaBitmap(&bmpInfo);
if (FAILED(hr))
Msg(TEXT("SetAlphaBitmap FAILED! hr=0x%x\r\n\r\n")
TEXT("NOTE: Your display must be configured for 32-bit color depth\r\n")
TEXT("for this sample to display alpha-blended text."), hr);
// Select the initial objects back into our device context
DeleteObject(SelectObject(hdcBmp, hbmOld));
SelectObject(hdc, hOldFont);
// Clean up resources
DeleteObject(hbm);
DeleteDC(hdcBmp);
return hr;} |
Partager