• WTL Picture Control显示图片


    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<CMainDlg>
    {
    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());


    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    str_pad 和 filter_var
    phpstorm主题下载地址
    php二维数组的排序
    wx.request出现400 bad request的问题
    php里的闭包函数
    关于宝塔下的项目中的php不能访问的问题
    字体大小适配宽度
    递归复制&查看文件夹下的指定后缀的文件
    find_in_set
    给动态ajax添加的元素添加click事件
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616372.html
Copyright © 2020-2023  润新知