简体   繁体   English

将位图设置为 CStatic 对象

[英]Set bitmap into CStatic object

I'm trying to add one bitmap into a picture box.我正在尝试将一个位图添加到图片框中。 This is what I've tried:这是我尝试过的:

void DlgError::Define_Image()
{
    // I generate all requiered elements.
    CBitmap bitmap;

    // Depending on the type of the message, I define the image to show.
    switch (Message_Type)
    {
        case Error: bitmap.LoadBitmap(IDB_ERROR); break;
        case Warning: break;

        case Information:
        default: bitmap.LoadBitmap(IDB_INFORMATION); break;
    }

    // I set the new picture.
    m_picture.ModifyStyle(0, SS_BITMAP);
    m_picture.SetBitmap(bitmap);
}

I don't know why it's not working.我不知道为什么它不起作用。 I have the same code I've in some forums.我在某些论坛上有相同的代码。 Can anybody help me?有谁能够帮助我? I have to define some extra styles?我必须定义一些额外的样式?

The picture box has Bitmap as its type (Can be hanged in Picture control properties)图片框的类型为Bitmap (可以在图片控件属性中挂起)

SOLUTION解决方案

I must say that the solution is a combination of both answers.我必须说解决方案是两个答案的结合。 Finally, I define one local variable and I use the next code in order to do what I want.最后,我定义了一个局部变量,并使用下一个代码来做我想做的事。

void DlgError::Define_Image()
{
    // Depending on the type of the message, I define the image to show.
    switch (Message_Type)
    {
        case Error: bitmap.LoadBitmap(IDB_ERROR); break;
        case Warning: break;

        case Information:
        default: bitmap.LoadBitmap(IDB_INFORMATION); break;
    }

    // I set the new picture.
    m_picture.ModifyStyle(SS_ENHMETAFILE, SS_BITMAP);
    m_picture.SetBitmap(bitmap);
}

Thanks everyone who helped me!感谢所有帮助过我的人!

Are you sure that LoadBitmap succeeded?你确定 LoadBitmap 成功了吗?

Isn't m_picture already created with the matching style if not your code might fail.如果不是您的代码可能会失败,m_picture 是否已经使用匹配样式创建。

SS_BITMAP is a number. SS_BITMAP 是一个数字。 If you want to change the tsyle of the static control, you need to remove the old style:如果要更改静态控件的tsyle,则需要删除旧样式:

m_picture.ModifyStyle(SS_TYPEMASK,SS_BITMAP);

Otherwise you get side effects.否则你会产生副作用。 Check the style with Spy++.使用 Spy++ 检查样式。

You are using the CBitmap object as a local variable in the Define_Image function.您正在使用CBitmap对象作为Define_Image函数中的局部变量。 Once the function goes out of scope, the bitmap is destroyed by the destructor of CBitmap object (there are no ref-counts here).一旦函数超出范围,位图就会被CBitmap对象的析构函数销毁(这里没有引用计数)。

You need to detach the object and then pass the HBITMAP handle to SetBitmap of CStatic .您需要分离对象,然后将HBITMAP句柄传递给CStatic SetBitmap

Fortunatly, only a single line is required:幸运的是,只需要一行:

 m_picture.SetBitmap((HBITMAP)bitmap.Detach());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM