• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

image as dialog's background

I urgently need detailed sample code w/ instructions on how to set an image (bmp or whatever else) as a dialog's background.

Best regards
-Stavi-
0
mitrakis
Asked:
mitrakis
  • 9
  • 3
  • 2
  • +2
1 Solution
 
krispCommented:
Hi mitrakis,

Say u have a dialog class CMyDlg and a bitmap added to ur resource with the ID :IDB_BITMAP1 . In the OnPaint() function of the dialog class add the following code in the else part of IsIconic loop.

--------------------------------------
void CClpbrdDlg::OnPaint()
{
   if (IsIconic())
   {
   }
   else
   {
    CPaintDC dc(this);
    int width = 100, hieght = 100;
    HBITMAP hbitmap = ::LoadBitmap  (::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1));

   HDC hMemDC = ::CreateCompatibleDC (NULL);

   SelectObject(hMemDC, hbitmap);

   ::StretchBlt(dc.m_hDC, 0, 0, width, hieght, hMemDC, 0, 0, 250,250, SRCCOPY);

   ::DeleteDC(hMemDC);
   }
}

---------------------------------------
Here "width" and "hieght" can be varied to change the size of the bitmap displayed on the dialog.

Hope this will help u.

cheers

0
 
mitrakisAuthor Commented:
Compiled without any errors...though no bitmap displayed.

Do I have to modify the dialog's properties ? (transparency or something like that ?)

-Stavi-
0
 
chensuCommented:
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
mitrakisAuthor Commented:
chensu,

thanks for the hint at codeguru.
Though I don't want to add another 6 files to my project...
There's an alternative provided at codeguru which worked fine for me:
http://www.codeguru.com/mfc/comments/13528.shtml

Unfortunately, the dialog elements are hidden now.
Could you guys help me to solve this ?

I really appreciate the codeguru thing, but I don't want to add these files (at least not yet).

If there's no other way to manage it I'll get back to you chensu.

Thanks a lot
-Stavi-

P.S.:
Don't misunderstand me please.
Although I got an answer from chensu (which might work) I want to solve it with "a few lines" as started by krisp.
0
 
mitrakisAuthor Commented:
This what I got so far.
It works, except that my push buttons are not shown after using the bitmap on the dialog:

BOOL CRasDemoDlg::OnInitDialog()
{
      CDialog::OnInitDialog();
      CRect rect;
      GetClientRect(&rect);
      m_static.Create(NULL,WS_VISIBLE| SS_BITMAP| SS_CENTERIMAGE, rect, this, NULL);
      m_bmp.LoadBitmap(IDB_BITMAP2);
      m_static.SetBitmap(m_bmp);
      
      SetIcon(m_hIcon, TRUE);                  // Set big icon
      SetIcon(m_hIcon, FALSE);            // Set small icon
      
      m_pRas = new CRasClient;
      UpdateData(FALSE);

      if(!FillModemCombo())
            GetDlgItem(IDC_CONNECT_BTN)->EnableWindow(FALSE);

      GetDlgItem(IDC_DISCONN_BTN)->EnableWindow(FALSE);

      return TRUE;
}

Can I redraw/refresh all the dialog's elements after setting the background ?
0
 
chensuCommented:
The problem of this method is that the static control occupies the entire client area of the dialog box. All the other controls and the static control are the child windows of the dialog box.

You might want to try redraw the dialog box with

RedrawWindow(&rect, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);

But I doubt it will work.
0
 
mitrakisAuthor Commented:
chensu:
Already tried RedrawWindow but it didn't work (exception occured).

I want to keep this Q open to others for a short while to get some more suggestions.
If no alternative/solution will be provided, I'll check out chensu's suggestion where I have to add some classes offered at codeguru.

Best regards
-Stavi-
0
 
kishk91Commented:
Hi...
i just use WM_ERASEBKGND message (you have to add it manualy)

in H file:
afx_msg BOOL OnEraseBkgnd(CDC* pDC) ;

in CPP:
BEGIN_MESSAGE_MAP(CFXDialog, CDialog)
      //{{AFX_MSG_MAP(CFXDialog)
       //other claass wizard messages
      //}}AFX_MSG_MAP

      ON_WM_ERASEBKGND() //your manualy added messages
END_MESSAGE_MAP()

BOOL CYourDialog::OnEraseBkgnd(CDC* pDC) //Sets the bitmap on the background
{                                                                   //draw your bitmap here...
      //use the pDC to draw
       pDC->BitBlt(.............) ;                       
      
      return TRUE;
}

just as easy....
Good luck
kishk91
0
 
mitrakisAuthor Commented:
This is really annoying !
Added kishk's suggestion with the same result:
Bitmap is displayed correctly but other controls are hidden.

I'll appreciate any hints you can give me on this.
Or maybe I should choose chensu's way ?
-> Lost a lot of time while fiddling around with this f... bitmap ;(

Best regards
-Stavi-

<----snip---->
BOOL CRasDemoDlg::OnEraseBkgnd(CDC* pDC)
{
      pDC->BitBlt(0, 0, 0, 0, NULL, 0, 0, NULL);
      return TRUE;
}
<----snap---->
0
 
mitrakisAuthor Commented:
Aehm...my project is annoying me...not you guys !
Just to be sure you got me :-)
0
 
kkarunakarCommented:
hi there,
I am putting a bullshit idea to u..But it may work..but u have to do much things in design time...
Sorry if it looks funny..
Just put a bitmap on the dialog in design time. Put ur all controls...and make their property to transparent..if u can't see them use tab control to move and size it..it will work perfect in run time....
keshav
0
 
mitrakisAuthor Commented:
Well, it seems that the easiest solution is always the best.
It worked !
There was not "much to do".
Adding a bitmap in ressources, adding a bitmap element in dialog, voilá...everything is visible with the bitmap set as dialog background.

I must admit that I have no idea why I tried the complicated ways first instead of beginning with the easiest one ;)

Best regards
-Stavi-

P.S.:
Of course, the static text controls are displayed with a grey background now, but this is another story...
0
 
kkarunakarCommented:
Hi there..
Thanks for accepting my answer..I was knowing this will work.But I think u might have enjoyed the idea..
With regards..
keshav
0
 
mitrakisAuthor Commented:
BTW, welcome to EE keshav... :)
0
 
kkarunakarCommented:
what is BTW??
0
 
mitrakisAuthor Commented:
Uh, sorry:

BTW --> "By The Way"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 9
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now