关于图象显示的问题

huangsiwei_0816 2008-04-17 04:40:55
我最近在做毕设,有一个例子是先打开图片的时候不显示图片,要再点击显示原图才可以.我现在要做的是把这两个过程合起来,也就是在打开的同时就显示图片.可不可以拜托你帮下忙,看看要怎么实现.
打开文件的代码是:
void CDSplitDoc::OnFileopen()
{
// TODO: Add your command handler code here
CFileDialog dlg(TRUE,_T("BMP"),_T("*.BMP"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("位图文件(*.BMP)|*.BMP|"));
if(IDOK==dlg.DoModal ())
filename.Format ("%s",dlg.GetPathName() );
CDib.LoadFile(filename);
CDibNew.LoadFile(filename);
statedoc=1;
}

显示图片的代码是:
void CDSplitView::OnYuantu()
{
CDSplitDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
filename=pDoc->filename;
state1=1;
Invalidate();
}
或者你给我你QQ,我把整个工程打包传给你看看
...全文
116 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangsiwei_0816 2008-04-17
  • 打赏
  • 举报
回复
问题解决啦,谢谢两位高手哦,我不好偏心啦,所以一人一半哦
iyranly 2008-04-17
  • 打赏
  • 举报
回复
我晕,看错了,你打开的时候就要显示的话更简单了,OnDraw时候不用加上判断state1那句就可以了。

参考:
http://www.functionx.com/visualc/views/DisplayBitmap.htm


iyranly 2008-04-17
  • 打赏
  • 举报
回复
这个不难,在View创建是给state1一个默认值为0,点击显示时候设置该值为1,
关键在于这里:在OnDraw时候,判断当前state1的值是否为1,为1则显示,为0则不显示,代码可以这样来写:

void CDSplitView::OnDraw(CDC* pDC)
{
CDSplitDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
if (!state)
return;

// TODO: add draw code for native data here
if( strFilename != "" )
{
BITMAP bmpProperties;

// Create a bitmap handle using the name of the file
HBITMAP bmpHandle = (HBITMAP)LoadImage(NULL,
strFilename,
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE);

CBitmap bmpPicture;
CDC mdcPicture;

// Convert the Win32 bitmap handle into an MFC bitmap object
CBitmap *bmpFromHandle = bmpPicture.FromHandle(bmpHandle);
bmpPicture.Attach(bmpHandle);

// Call the Win32 GetObject() function to get the properties
// of the bitmap and store them in a BITMAP structure
::GetObject(bmpPicture,
sizeof(bmpProperties),
&bmpProperties);

// Create a compatible device context
mdcPicture.CreateCompatibleDC(pDC);
// Select the bitmap into the device context
CBitmap * bmpPrevious =
mdcPicture.SelectObject(bmpFromHandle);

// Using the dimensions store in the BITMAP object,
// display the picture
pDC->BitBlt(0, 0,
bmpProperties.bmWidth, bmpProperties.bmHeight,
&mdcPicture, 0, 0, SRCCOPY);

// Get the dimensions of the new picture
SIZE sizeTotal;
sizeTotal.cx = bmpProperties.bmWidth;
sizeTotal.cy = bmpProperties.bmHeight;
// Change the scrolling area/dimensions of the view
SetScrollSizes(MM_TEXT, sizeTotal);

// Restore the bitmap
pDC->SelectObject(bmpPrevious);
// Delete the Win32 bitmap handle
DeleteObject(bmpHandle);
}
}


其中strFilename 变量为你程序的filename,代替即可。
ouyh12345 2008-04-17
  • 打赏
  • 举报
回复
void CDSplitView::OnYuantu()
{
CDSplitDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
filename=pDoc->filename;
state1=1;
Invalidate();
}

这里有用的代码就是文件名和刷新
在doc里,也可以通知view刷新,参考2楼的方法
huangsiwei_0816 2008-04-17
  • 打赏
  • 举报
回复
确实不知道呀,这要怎么用啊
huangsiwei_0816 2008-04-17
  • 打赏
  • 举报
回复
//这里,把图画出来,并通知view更新
//UpdateAllViews
这两行是什么功能?VIEW怎么更新?我是菜鸟,所以可能要麻烦你说详细点.嘿嘿
cnzdgs 2008-04-17
  • 打赏
  • 举报
回复
LZ应该是不知道怎么才能得到视图类的指针。用文档类的GetFirstViewPosition、GetNextView函数。
ouyh12345 2008-04-17
  • 打赏
  • 举报
回复
void CDSplitDoc::OnFileopen()
{
// TODO: Add your command handler code here
CFileDialog dlg(TRUE,_T("BMP"),_T("*.BMP"),OFN_HIDEREADONLY ¦OFN_OVERWRITEPROMPT,_T("位图文件(*.BMP) ¦*.BMP ¦"));
if(IDOK==dlg.DoModal ())
filename = dlg.GetPathName();
CDib.LoadFile(filename);
CDibNew.LoadFile(filename);
//这里,把图画出来,并通知view更新
//UpdateAllViews
}

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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