步骤:
先在资源里Import一张图片,ID为IDB_BITMAP2
然后在对话框上添加一个picture控件,右键点击打开属性,
将type下拉框选择BITMAP,紧跟着下面就出现一个Image下拉框,
拉开就会看到所有已经载入好的图片,
选择你要的图片.运行程序即可看到.
同样如上,先载入一张图片,ID为IDB_BITMAP2
TestDlg.h中
CBrush m_brBk;//在public中定义
TestDlg.cpp中,在初始化函数OnInitDialog()中加入:
BOOL CTestDlg::OnInitDialog() { CDialog::OnInitDialog(); CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP2); m_brBk.CreatePatternBrush(&bmp); bmp.DeleteObject(); return TRUE; // return TRUE unless you set the focus to a control }
在打开类向导,找到WM_CTLCOLOR消息,重载得对应函数OnCtlColor(),添加如下:
HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); if (pWnd == this) { return m_brBk; } return hbr; }
1. 首先应该保证系统中有这个控件。注意,它不能单独使用,必须和其他几个控件(特别是Imgcmn.dll)一同使用。如果没有,从别的机器上copy过来即可。这几个文件是Imgadmin.ocx,Imgcmn.dll,Imgedit.ocx,Imgscan.ocx,Imgshl.dll,Imgthumb.ocx,Imgutil.dll,把它们copy到windows\system目录下,然后用regsvr32.exe将它们分别注册。
2. 打开工程,进入资源管理器,在对话框上单击右键,单击Insert Activex control… 选择Kodak图象编辑控件,大小任意。
3. 在对话框上选中该控件,为其添加变量:m_ctrlPicture。
4. 在BOOL CTestDlg::OnInitDialog()添加如下:
BOOL CTestDlg::OnInitDialog() { CDialog::OnInitDialog(); m_ctrlPicture.SetImage("aa.jpg"); //保证图像在工程目录下,也可以写绝对路径 m_ctrlPicture.Display(); return TRUE; // return TRUE unless you set the focus to a control }
编译运行就OK了,此种方法的好处就是可能针对多种图像格式.
首先在CTestDlg类中声明一个变量:
CBitmap m_bmp
然后我们在对话框中加入一个picture 标签,名为IDC_STATIC1,然后:
BOOL CDisplayPic::OnInitDialog() { CDialog::OnInitDialog(); if( m_bmp.m_hObject != NULL )//判断 m_bmp.DeleteObject(); //载入图片 HBITMAP hbmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), "c:\\aaa.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADFROMFILE); if( hbmp == NULL ) return FALSE; //该断程序用来取得加载的BMP的信息 m_bmp.Attach( hbmp ); DIBSECTION ds; }