我最近在做毕设,有一个例子是先打开图片的时候不显示图片,要再点击显示原图才可以.我现在要做的是把这两个过程合起来,也就是在打开的同时就显示图片.可不可以拜托你帮下忙,看看要怎么实现.
打开文件的代码是:
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,我把整个工程打包传给你看看

解决方案 »

  1.   

    void CDSplitDoc::OnFileopen() 
    {
    // TODO: Add your command handler code here
    CFileDialog dlg(TRUE,_T("BMP"),_T("*.BMP"),OFN_HIDEREADONLY &brvbarOFN_OVERWRITEPROMPT,_T("位图文件(*.BMP) ¦*.BMP ¦"));
        if(IDOK==dlg.DoModal ())
        filename = dlg.GetPathName();    
    CDib.LoadFile(filename);
        CDibNew.LoadFile(filename);
        //这里,把图画出来,并通知view更新
        //UpdateAllViews
      

  2.   

    LZ应该是不知道怎么才能得到视图类的指针。用文档类的GetFirstViewPosition、GetNextView函数。
      

  3.   

    //这里,把图画出来,并通知view更新 
        //UpdateAllViews 
    这两行是什么功能?VIEW怎么更新?我是菜鸟,所以可能要麻烦你说详细点.嘿嘿
      

  4.   

    void CDSplitView::OnYuantu() 
    {
    CDSplitDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
        filename=pDoc->filename;
    state1=1;
    Invalidate();
    } 这里有用的代码就是文件名和刷新
    在doc里,也可以通知view刷新,参考2楼的方法
      

  5.   

    这个不难,在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,代替即可。
      

  6.   

    我晕,看错了,你打开的时候就要显示的话更简单了,OnDraw时候不用加上判断state1那句就可以了。参考:
    http://www.functionx.com/visualc/views/DisplayBitmap.htm