WIN32程序GDI+画图和绘文字

lb451035525 2010-09-16 04:44:50
用drawimage画图
用drawstring画文字
在一个函数里完成,
用户按一次键盘触发一次函数。
怎么样让图片和文字不闪烁???
...全文
545 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lb451035525 2010-09-17
  • 打赏
  • 举报
回复
谢谢你们大家了。问题解决了,
向立天 2010-09-17
  • 打赏
  • 举报
回复
这段代码你可以参考一下
	HDC hdcTemp, hdcMemory;
hdcTemp = GetDC()->m_hDC;
hdcMemory = CreateCompatibleDC(hdcTemp);

//使内存DC存放位图
HBITMAP hBitMap = CreateCompatibleBitmap(hdcTemp, m_nWidth, m_nHeigth);
SelectObject(hdcMemory, hBitMap);

//设置Alpha值
m_blendFunc.SourceConstantAlpha = int(255*nTransparent/100);

//获取本窗口DC
HDC hdcScreen = ::GetDC(m_hWnd);

//将PNG图画入内存
Graphics graph(hdcMemory);

Point points[] ={
Point(0, 0),
Point(m_nWidth, 0),
Point(0, m_nHeigth)
};

graph.DrawImage(m_pImageTip, points, 3);

//绘制标题
DrawTitle(&graph);

SolidBrush* pBrush = new SolidBrush(Color::White);
graph.FillRectangle(pBrush, 20, 40, m_nWidth-40, m_nHeigth-80);
delete pBrush;

其中DrawTitle(Graphics* pGraph)
	FontFamily	fontFamily(L"宋体");
Font font(&fontFamily, 12, FontStyleBold, UnitPoint);
SolidBrush solidBrush(Color(255, 255, 255, 255));
PointF pointF(20, 15);
PWSTR pWideCharStr;
int nLenOfWideCharStr;

nLenOfWideCharStr = MultiByteToWideChar(CP_ACP, 0, LPCSTR(m_strTitle), -1, NULL, 0);
pWideCharStr = (PWSTR)HeapAlloc(GetProcessHeap(), 0, nLenOfWideCharStr*sizeof(wchar_t));
if(pWideCharStr!=NULL)
{
MultiByteToWideChar(CP_ACP, 0, LPCSTR(m_strTitle), m_strTitle.GetLength()+1, pWideCharStr, nLenOfWideCharStr);
pGraph->DrawString(pWideCharStr, nLenOfWideCharStr, &font, pointF, &solidBrush);
HeapFree(GetProcessHeap(), 0, pWideCharStr);
}
lb451035525 2010-09-17
  • 打赏
  • 举报
回复
双缓冲用了,图片不闪了,就是文字闪,因为设备上下文是HDC的,是指针类型的,小弟是个初学者,不知道怎么将文字在hdc中的图片上输出,然后再输出图片。
望给个小例子。我的图片是image*类型的png图片。带透明。
用Drawimage打印图片。
雪域迷影 2010-09-16
  • 打赏
  • 举报
回复
对,是双缓冲的问题,上学期我用MFC做了
个推箱子游戏,就是用这个解决的!
npuhuxl 2010-09-16
  • 打赏
  • 举报
回复
双缓冲!并处理WM_ERASEBK
lb451035525 2010-09-16
  • 打赏
  • 举报
回复
BOOL UpdateDisplay(WNDINFO &wndinfo, DWORD dwButton)
{
BOOL bRet = FALSE;
InitResources();
//m_Blend是结构体BLENDFUNCTION的对象,用于指定两个DC(画图设备)的融合方式。
m_Blend.BlendOp = 0; //theonlyBlendOpdefinedinWindows2000仅仅融合在WINDOUWS2000平台下定义
m_Blend.BlendFlags = 0; //nothingelseisspecial...
m_Blend.AlphaFormat = 1; //...透明度
m_Blend.SourceConstantAlpha = 255;//AC_SRC_ALPHA

//确定透明度
if(Transparent<0||Transparent>100) Transparent = 100;
m_Blend.SourceConstantAlpha = int(Transparent*2.55);//1~255


m_BakWidth = m_pStatusImg->GetWidth();
m_BakHeight = m_pStatusImg->GetHeight();

::SetWindowPos(wndinfo.hWnd, NULL, wndinfo.ptWnd.x, wndinfo.ptWnd.y, m_BakWidth, m_BakHeight,SWP_NOACTIVATE);

HDC hdcTemp, m_hdcMemory, hdcScreen;
//创建画图设备环境
hdcTemp = GetDC(wndinfo.hWnd);
HBITMAP hBitMap = CreateCompatibleBitmap(hdcTemp, m_BakWidth, m_BakHeight);
m_hdcMemory = CreateCompatibleDC(hdcTemp);//创建与显示设备相关的内存设备上下文
SelectObject(m_hdcMemory, hBitMap);
hdcScreen = GetDC (wndinfo.hWnd);

//得到窗体的区域
RECT rct;
GetWindowRect(wndinfo.hWnd,&rct);
POINT ptWinPos = {rct.left,rct.top};
Graphics graph(m_hdcMemory);


Point points[] = { Point(0, 0),Point(m_BakWidth, 0),Point(0, m_BakHeight)};
graph.DrawImage(m_pCCWndImg, points, 3);
//输出字符串
FontFamily fontFamily(_T("Arial"));
Gdiplus::Font font(&fontFamily, 14, FontStyleRegular, UnitPixel);
PointF pointFComp(wndinfo.ptStrComp.x, wndinfo.ptStrComp.y);
PointF pointFCand(wndinfo.ptStrCand.x, wndinfo.ptStrCand.y);
SolidBrush solidBrush(Color(255, 255, 255, 255));
graph.SetTextRenderingHint(TextRenderingHintAntiAlias);
graph.DrawString(stringTowstring(wndinfo.strResultComp).c_str(), -1, &font, pointFComp, &solidBrush);
graph.DrawString(stringTowstring(wndinfo.strResultCand).c_str(), -1, &font, pointFCand, &solidBrush);


#pragma endregion
SIZE sizeWindow = {m_BakWidth, m_BakHeight};
POINT ptSrc = {0, 0};

DWORD dwExStyle = GetWindowLong(wndinfo.hWnd, GWL_EXSTYLE);//GWL_EXSTYLE;获得扩展窗口风格。

if((dwExStyle & 0x80000) != 0x80000)//dwExStyle&0x80000显示alpha透明通道,如为0x80000窗体为透明,否则设置为透明
SetWindowLong(wndinfo.hWnd, GWL_EXSTYLE, dwExStyle^0x80000);

bRet = UpdateLayeredWindow(wndinfo.hWnd, hdcScreen, &ptWinPos,
&sizeWindow, m_hdcMemory, &ptSrc, 0, &m_Blend, 2);

graph.ReleaseHDC(m_hdcMemory);
DeleteObject(hBitMap);
DeleteDC(hdcScreen);
DeleteDC(hdcTemp);
DeleteDC(m_hdcMemory);

hdcScreen = NULL;
hdcTemp = NULL;
m_hdcMemory = NULL;
return bRet;

}

这是我的代码,背景倒是不闪了,就是每按一次按键,文字就会闪。
冻结 2010-09-16
  • 打赏
  • 举报
回复
结帖率:0.00%
楼主要结贴啊!
xingzhe2001 2010-09-16
  • 打赏
  • 举报
回复
用双缓存就不闪烁了
冻结 2010-09-16
  • 打赏
  • 举报
回复
Google “双缓冲”

65,189

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧