How do I set a background picture in a dialog box ? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How do I set a background picture in a dialog box ?

chris666chris666 Posts: 71Member
All is in the subject. I have a dialog-based MFC application and I can't figure out how to set a background picture in a box, I tried the picture control, but all other controls remain under the picture, and are invisible. Thanks in advance for those who have a solution.

Comments

  • Liam27Liam27 Posts: 5Member
    : All is in the subject. I have a dialog-based MFC application and I can't figure out how to set a background picture in a box, I tried the picture control, but all other controls remain under the picture, and are invisible. Thanks in advance for those who have a solution.
    :

    There are a few steps in setting a .bmp as a background for a window (works for frame windows too btw). You have to load the .bmp into a device context then draw that .bmp to the window. The problem is that you need the paint device context of the window (CPaintDC) and you can't load a bitmap to it. What you have to do is make a 'memory DC' based on the CDC class, then copy one to the other.

    Here is the code that you can put in your "OnPaint" function:

    void CDialogNameDlg::OnPaint()
    {
    CBitmap bitmap; //bitmap object to hold your bitmap
    bitmap.LoadBitmap(IDB_BITMAPID); // IDB_BITMAPID is the id of bmp

    CPaintDC dc(this); //device context of dialog box
    CDC mem_dc; // memory device context

    mem_dc.CreateCompatibleDC(&dc); // makes compatible with CPaintDC
    mem_dc.SelectObject(bitmap); // Selects bitmap into CDC
    dc.BitBlt(0,0,dlg_len,dlg_wid, &mem_dc, 0, 0, SRCCOPY);
    // Above code copies bmp to dialog box bit by bit
    }

    Hope this helps :)

  • chris666chris666 Posts: 71Member
    Thanks Liam,
    Your code works fine, where did you learn to do such a thing ? that's cool. But the problem is, some controls, like Static text, are not "transparent" and they leave a big gray ugly rect. Do you have an idea to solve this ?

    Chris
  • Elias_AEElias_AE Posts: 6Member
    To make a control transparent, use the OnCtlColor to trap the WM_CTLCOLOR message. In OnCtlColor you can change the DeviceContext, (one of the arguments) so the control will use a transparent background. Use the SetBkMode memberfunction of the CDC.[code]
    pDC->SetBkMode(TRANSPARENT);
    [/code]You can get more info in this article from Microsoft:
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;q117778
Sign In or Register to comment.