按ctrl+W , 选择C**View类 ,再选择ID_FILE_OPEN就可以看到右边有两个messages,
具体是什么我也不记得了,好象一个是command , 一个是updateCommandUI
选择前一个可以建立消息响应
至于载入图象,可以用CFileDialog选取路径,用其他库比如CxImage之类的东西装载图象就行了
可以在资源界面的menu菜单中查看到你的"打开"窗口的ID->用ctrl+W进入CLASSWARD界面->建立消息响应->在生成的函数里用CFileDialog得到选择的文件名->CFILE用于打开这个文件
然后用文件指针读入图象的头文件->文件指针读入图象的信息头文件和调色版->文件指针读入图象的数据->最后在onpait(ondraw)显示图片(将调色版读如设备上下文SelectPalette->设置显示模式SetStretchBltMode->显示::StretchDIBits)
- Start Microsoft Visual C++ and create an MFC Application named ShowPicture1
- Create it as a Single Document
- Base the view class on CScrollView and click Finish
- In the Class View, right-click the name of the view class -> Add -> Add Variable...
- Create a private CString variable named strFilename and click Finish
- In the Resource View, double-click the IDR_MAINFRAME menu
- Right the Open menu item and click Add Event Handler...
- In the Class List, select the view class
- Click Add and Edit
- Implement the event as follows:
void CShowPicture1View::OnFileOpen() { // TODO: Add your command handler code here TCHAR strFilter[] = { TEXT("Picture Files (*.bmp)|*.bmp||") }; CFileDialog dlg(TRUE, TEXT(".bmp"), NULL, 0, strFilter); if( dlg.DoModal() == IDOK ) { strFilename = dlg.GetFileName(); Invalidate(); } }
- Access the OnDraw event (of the view class) and change its as follows:
void CShowPicture1View::OnDraw(CDC* pDC) { CShowPicture1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) 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); } }
- Execute the application
- Close the application