C ++ GDI+在透明分层窗口上绘制文本
题
(不受管理的C ++)我已经成功地将PNG文件绘制到可以拖动桌面的透明分层窗口,但是现在我的问题是在透明的分层窗口上绘制文本
这是我的代码和我在中间绘制文本的尝试,重要的是要注意我使用的是屏幕界,而不是在wm_paint消息中使用屏幕。
编辑]评论后更新代码,现在我只是在获得hbitmap版本之前尝试在位图上写文本,我需要使用drawstring,因为textout()不是gdi+,我希望绘图串。真的是gdi+大声笑仍然不起作用,想知道我在做什么错
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);
}
我一直在尝试在分层窗口上写文本两天,但是从这些尝试来看,我知道有几种方法可以做到这一点(不幸的是,我不知道到底是怎么做的)
我看到的通常选项是在位图上绘制文本,然后呈现位图本身
- 使用GDI+加载位图
- 从位图创建图形对象
- 使用牵引串将文本写入位图
- 处理图形对象
- 使用位图保存方法将结果保存到文件
显然,一个人也可以从DC制作图形对象,然后在DC上绘制文本,但我又一次不知道如何做到这一点
解决方案
总体方法看起来正确,但我认为您对 DrawString
称呼。查看文档(尤其是样本) MSDN.
Gx.DrawString(thetext, 4, NULL, therect, NULL, NULL)
可能需要指定第三,第五和第六参数(字体,格式和刷子)。该文档没有说它们是可选的。通过 NULL
因为这些可能导致gdi+将呼叫视为no-op。
第二个参数不应在字符串中包含终端L' 0'。如果您的字符串始终终止,使用-1可能是最安全的。
不隶属于 StackOverflow