1、在对话框上新建一个Picture Control ID为:IDC_STATIC_IMG
2、添加图片资源ID:IDB_BITMAP1(选中Bitmap点击导入,选择bmp图片资源)
实现:
CWindow wnd = this->GetDlgItem(IDC_STATIC_IMG);
CStatic *pImg = (CStatic*)&wnd;
pImg->ModifyStyle(0xF, SS_BITMAP | SS_CENTERIMAGE);
hBitmap = AtlLoadBitmap(MAKEINTRESOURCE(IDB_BITMAP1));
pImg->SetBitmap(hBitmap);
也可以使用CBitmap
CBitmap bitmap;
m_picture.Create("",WS_CHILD and WS_VISIBLE and SS_CENTERIMAGE and SS_BITMAP,
CRect(0,0,180,160),this,IDC_STATIC1);
bitmap.LoadBitmap(IDB_BITMAP1);
m_picture.SetBitmap(bitmap.Detach());
源码:
MainDlg.h
// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////
#pragma once
class CMainDlg : public CDialogImpl
{
public:
enum { IDD = IDD_MAINDLG };
BEGIN_MSG_MAP(CMainDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
END_MSG_MAP()
// Handler prototypes (uncomment arguments if needed):
// LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
// LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
// LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)
LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
private:
HBITMAP hBitmap;
};
MainDlg.cp
// MainDlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "MainDlg.h"
LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// center the dialog on the screen
CenterWindow();
// set icons
HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON));
SetIcon(hIcon, TRUE);
HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
SetIcon(hIconSmall, FALSE);
CWindow wnd = this->GetDlgItem(IDC_STATIC_IMG);
CStatic *pImg = (CStatic*)&wnd;
pImg->ModifyStyle(0xF, SS_BITMAP | SS_CENTERIMAGE);
hBitmap = AtlLoadBitmap(MAKEINTRESOURCE(IDB_BITMAP1));
pImg->SetBitmap(hBitmap);
return TRUE;
}
LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
DeleteObject(hBitmap);
EndDialog(wID);
return 0;
}
这里只显示bmp图片有点局限性,如何显示png,jpg图片呢?
CImage img;
CWindow wnd = this->GetDlgItem(IDC_STATIC_IMG);
CStatic *pImg = (CStatic*)&wnd;
pImg->ModifyStyle(0xF, SS_BITMAP | SS_CENTERIMAGE);
img.Load(TEXT("ing.png"));
pImg->SetBitmap(img.Detach());