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());
手机扫一扫
移动阅读更方便
你可能感兴趣的文章