(管理されていないC ++)デスクトップ上でドラッグできる透明なレイヤードウィンドウにPNGファイルを描画することに成功しましたが、問題は透明なレイヤードウィンドウにテキストを描画することです。
これが私のコードと中央にテキストを描画する試みです。WM_PAINTメッセージの画面ではなくscreenDCを使用していることに注意してください。
[編集]コメントの後にコードを更新しました。今度は、HBITMAPバージョンを取得する前に、ビットマップにテキストを書き込もうとしています。今回は、textout()がGDI +ではないため、DrawStringを使用しています。DrawStringを希望します。本当にGDI+lolはまだ機能しませんが、私が間違っているのは何ですか?
void Draw() // draws a frame on the layered window AND moves it based on x and y
{
HDC screenDC( NULL ); // grab screen
HDC sourceDC( CreateCompatibleDC(screenDC) );
POINT pos = {x,y}; // drawing location
POINT sourcePos = {0,0}; // top left of image
SIZE size = {100,100}; // 100x100 image
BLENDFUNCTION blendFunction = {0};
HBITMAP bufferBitmap = {0};
Bitmap* TheBitmap = crnimage; // crnimage was already loaded earlier
// ------------important part goes here, my attempt at drawing text ------------//
Gdiplus::Graphics Gx(TheBitmap);
// Font* myFont = new Font(sourceDC);
Font myFont(L"Arial", 16);
RectF therect;
therect.Height = 20;
therect.Width = 180;
therect.X = 0;
therect.Y = 0;
StringFormat format;
format.SetAlignment(StringAlignmentCenter);
format.GenericDefault();
Gdiplus::SolidBrush GxTextBrush(Gdiplus::Color(255, 255, 0,255));
WCHAR thetext[] = L"Sample Text";
int stats = Gx.DrawString(thetext, -1, &myFont, therect, &format, &GxTextBrush);
if(stats) // DrawString returns nonzero if there is an error
msgbox(stats);
stats = Gx.DrawRectangle(&Pen(Color::Red, 3), therect);
// the rectangle and text both draw fine now
// ------------important part goes here, my attempt at drawing text ------------//
TheBitmap->GetHBITMAP(0, &bufferBitmap);
HBITMAP oldBmpSelInDC;
oldBmpSelInDC = (HBITMAP)SelectObject(sourceDC, bufferBitmap);
// some alpha blending
blendFunction.BlendOp = AC_SRC_OVER;
blendFunction.SourceConstantAlpha = wndalpha;
blendFunction.AlphaFormat = AC_SRC_ALPHA;
COLORREF colorKey( RGB(255,0,255) );
DWORD flags( ULW_ALPHA);
UpdateLayeredWindow(hWnd, screenDC, &pos, & size, sourceDC, &sourcePos,
colorKey, &blendFunction, flags);
// release buffered image from memory
SelectObject(sourceDC, oldBmpSelInDC);
DeleteDC(sourceDC);
DeleteObject(bufferBitmap);
// finally release the screen
ReleaseDC(0, screenDC);
}
私は2日間、レイヤードウィンドウにテキストを書き込もうとしていますが、それらの試みから、これを行うにはいくつかの方法があることがわかります(残念ながら、正確にはわかりません)。
私が見る通常のオプションは、ビットマップにテキストを描画してから、ビットマップ自体をレンダリングすることです。
- Gdi+を使用してビットマップをロードする
- ビットマップからGraphicsオブジェクトを作成します
- DrawStringを使用してテキストをビットマップに書き込みます
- Graphicsオブジェクトを破棄します
- ビットマップのSaveメソッドを使用して、結果をファイルに保存します
どうやら、DCからグラフィックスオブジェクトを作成し、DCにテキストを描画することもできますが、これを行う方法についてはわかりません。