且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在基于Dialog的应用程序中加载PNG文件

更新时间:2021-11-22 04:19:59

使用向导创建基于对话框的应用程序,添加静态图片控件(样式 SS_BITMAP )到对话框模板并使用 CStatic 成员变量。



Use the wizards to create a dialog based application, add a static picture control (style SS_BITMAP) to the dialog template and use a CStatic member variable.

// CMyDialog.h 
class CMyDialog : CDialog
{
public:
    CMyDialog(CWnd* pParent = NULL);
protected:
    virtual void DoDataExchange(CDataExchange* pDX);
    void LoadPicture(LPCTSTR lpszFileName);
    CStatic m_Picture;
}

// CMyDialog.cpp
#include <atlimage.h>

void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_PICT_STATIC, m_Picture);
}

void CMyDialog::LoadPicture(LPCTSTR lpszFileName)
{
    if (lpszFileName && *lpszFileName)
    {
        CImage Image;
        // Load the image as bitmap (supports PNG files)
        if (SUCCEEDED(Image.Load(lpszFileName)))
        {
            // Pass image to static picture control
            HBITMAP hOld = m_Picture.SetBitmap(Image.Detach());
            // Release old bitmap if there was one
            if (hOld)
                VERIFY(::DeleteObject(hOld));
        }
    }
}


Hey Sowmya,



将png文件添加到文件夹中的解决方案,将其命名为Images。编辑png文件的属性为:启用复制到输出目录选项。进一步为Images文件夹启用它。



然后添加以下代码或类似代码。它应该适合你。



Hey Sowmya,

Add the png file to the solution in a folder, name it as Images. Edit the properties of the png file as: Enable the Copy to Output Directory option. Further enable it for the Images folder too.

then add the below code or similar. It should work for you.

PictureBox picture = new PictureBox
        {
            Name = "pictureBox",
            Size = new Size(50, 50),
            Location = new Point(10,10),
            Visible=true
        };

        pic.ImageLocation = "..\Images\picture.jpg";
        Form1.Controls.Add(pic);





希望它有所帮助。



-anurag



hope it helps.

-anurag


参见此响应 [ ^ ]。