zoukankan      html  css  js  c++  java
  • 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
    知识共享,欢迎转载。
  • 相关阅读:
    【免费赠书】前端程序员修炼之路:积土而为山,积水而为海
    开源网站流量统计系统Piwik源码分析——后台处理(二)
    开源网站流量统计系统Piwik源码分析——参数统计(一)
    数学思维修炼
    《代码大全》中的变量
    linux dmesg命令参数及用法详解(linux显示开机信息命令)
    dd命令使用详解
    Linux就这个范儿 第10章 生死与共的兄弟
    Linux就这个范儿 第9章 特种文件系统
    Bash中的shopt选项
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616372.html
Copyright © 2011-2022 走看看