怎么在对话框上显示图像

wantK 2011-03-20 12:14:48
有一段代码是
OnDraw(CDC* pDC)
CPicDOC* pDoc = GetDocument();
这个CPicDOC是一个doc类,输出时也不是在对话框上输出,我想问一下,怎么对话框上输出图像.
...全文
121 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2011-03-20
  • 打赏
  • 举报
回复
可以在OnPaint中利用BitBlt/StrectchBlt贴图
向立天 2011-03-20
  • 打赏
  • 举报
回复
或许这篇文章中的例子会对你有帮助
http://blog.csdn.net/xianglitian/archive/2010/11/20/6023656.aspx
muzizongheng 2011-03-20
  • 打赏
  • 举报
回复
调用CDC的方法就可以 。 代码如下:
// This handler loads a bitmap from system resources,
// centers it in the view, and uses BitBlt() to paint the bitmap
// bits.
void CDCView::DrawBitmap(CDC* pDC)
{
// load IDB_BITMAP1 from our resources
CBitmap bmp;
if (bmp.LoadBitmap(IDB_BITMAP1))
{
// Get the size of the bitmap
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);

// Create an in-memory DC compatible with the
// display DC we're using to paint
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);

// Select the bitmap into the in-memory DC
CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);

// Find a centerpoint for the bitmap in the client area
CRect rect;
GetClientRect(&rect);
int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;

// Copy the bits from the in-memory DC into the on-
// screen DC to actually do the painting. Use the centerpoint
// we computed for the target offset.
pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
0, 0, SRCCOPY);

dcMemory.SelectObject(pOldBitmap);
}
else
{
TRACE0("ERROR: Where's IDB_BITMAP1?\n");
}
}



====================================论坛签名==================================

当您的问题得到解答后请及时结贴.

http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html

如何给分和结贴?
http://community.csdn.net/Help/HelpCenter.htm#结帖

访问我的blog
http://blog.csdn.net/muzizongheng

wmnmtm 2011-03-20
  • 打赏
  • 举报
回复
只要取到客户区的设备环境句柄,就可以调用GDI的绘图函数进行绘图,可以放在按钮响应里,或者OnInitDialog初始化对话框时,或者是OnPaint重绘消息处理,更高级的一个是内存绘图,就是在某一个函数里把需要进行的复杂绘制提前画到一个bitmap上,在OnPaint里,只简单地把已完成绘制的位图拷贝到屏幕上,防止屏幕闪烁。
wmnmtm 2011-03-20
  • 打赏
  • 举报
回复
以下代码从资源中载入一个bmp,并显示到对话框上
void CTest2Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
/*
从资源中载入一个位图,并将它拷贝到客户区

*/
CClientDC dc(this);;
dc.MoveTo(0,0);
dc.LineTo(100,100);

CDC memoryDC;
memoryDC.CreateCompatibleDC(&dc);

CBitmap bmp;
CBitmap* oldBitmap;
bmp.LoadBitmap(IDB_BITMAP2);
oldBitmap = memoryDC.SelectObject(&bmp);
memoryDC.MoveTo(0,0);
memoryDC.LineTo(100,100);
// dc.BitBlt(0,0,200,200,&memoryDC,0,0,SRCCOPY);
dc.StretchBlt(0,0,400,300,&memoryDC,0,0,1600,1200,SRCCOPY);


CString str;
str.Format("WORD类型的长度:%d",sizeof(WORD));
AfxMessageBox(str);

str.Format("DWORD类型的长度为:%d",sizeof(DWORD));
AfxMessageBox(str);
/*
BYTE A;
byte b;

#define WINAPI __stdcall

*/
}
wmnmtm 2011-03-20
  • 打赏
  • 举报
回复
以下代码,在对话框的一个按钮上画图

void CTest1Dlg::OnButton5()
{
// TODO: Add your control notification handler code here

CDC memdc; //用来绘图的那个内存dc
CBitmap bitmap; //位图

CBitmap *pOldBitmap; //CBitmap指针,用来存储旧的位图,

CRect rect; //矩形,用来存放客户区窗口

CDC * pDC = GetDlgItem(IDC_BUTTON4)->GetDC();// CDC* GetDC();


GetDlgItem(IDC_BUTTON4)->GetClientRect(&rect); //取到客户区矩形,建兼容位图时

memdc.CreateCompatibleDC(pDC); //以屏幕DC(实际上就是客户区) 建立内存DC

bitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());//以屏幕DC建立位图

pOldBitmap = memdc.SelectObject(&bitmap);//将上面创建的位图选入内存DC中,返回CBitmap的指针并赋给pOldBitmap



//下面可以在memdc上进行绘图操作

memdc.FillSolidRect(rect,RGB(255,255,0));

CString str;

str.Format("%d",2012);
// memdc.SetBkColor(RGB(0,0,255));
memdc.SetTextColor(RGB(255,0,0));
memdc.TextOut(rect.left+rect.Width()/2,rect.top+10,str);

memdc.MoveTo(10,10);
memdc.LineTo(100,10);

memdc.MoveTo(10,12);
memdc.LineTo(100,12);
memdc.TextOut(20,20,"同志们好!");

//进行大量的绘图
for (int i = 1; i < 2000; i++)
{

memdc.MoveTo(0,i);
memdc.LineTo(200,i);

memdc.MoveTo(i,0);
memdc.LineTo(i,200);

i++;

}




//绘图结束

pDC->BitBlt(0,0,rect.Width(),rect.Height(),&memdc,0,0,SRCCOPY); // 从内存DC复制到屏幕DC

memdc.SelectObject(pOldBitmap);

this->ReleaseDC(pDC);
}

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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