Solved

Painting dialog background

Posted on 2000-05-14
21
435 Views
Last Modified: 2013-12-26
Hi, I want to know the code that I should use to fill the background of a dialog with a tiled bitmap. I want the static text's background to be transparent, and I want the dialog to paint itself without flaws. Since I can't use Paint, what is the code that I should use to paint a dialog box's background with a bitmap?
0
Comment
Question by:Owlguru
  • 11
  • 4
  • 3
  • +1
21 Comments
 

Accepted Solution

by:
ekinee earned 15 total points
ID: 2810245
Why don't you use EV_WM_PAINT ...
  void EvPaint();

You get the HDC by TClientDC(*this);
Then work with your bitmaps.

To get your statics transparent you may use EV_WM_CTLCOLOR message like...

::EvCtlColor(HDC hDc,HWND hWndChild,uint ctlType)
{
   switch(ctlType) {
      case CTLCOLOR_STATIC:
          SetBkMode(hDc,TRANSPARENT);
          return (HBRUSH)GetStockObject(NULL_BRUSH);
      default:
          return TDialog::EvCtlColor(hDc,hWndChild,ctlType);
   }
}

Hope it helps to get the results.
0
 

Author Comment

by:Owlguru
ID: 2812282
ekinee:
The suggestion for EvCtlColor was good, but I'd also like to know what is the best way of displaying a bitmapped background on a dialog box.
0
 

Expert Comment

by:ekinee
ID: 2816367
OK. Here is on way to do it.
I don't know what is exactly the best way.

::EvCtlColor(HDC hDC, HWND hWndChild, uint ctlType)
{
    switch(ctlType) {
       case CTLCOLOR_DLG:
           SetBkMode(hDC, TRANSPARENT);
           return (HBRUSH)GetStockObject(WHITE_BRUSH);
       default:
               return TDialog::EvCtlColor(hDC, hWndChild, ctlType);
    }
}

::EvPaint()
{
    TBitmap bitmap1(*GetModule(), "IDB_BITMAP1");
    TMemoryDC MemDC;
    MemDC.SelectObject(bitmap1);
    TClientDC hdc(*this);
    int startX;
    int startY=0;
    int bitmapX = bitmap1.Width();
    int bitmapY = bitmap1.Height();
    int endX=GetClientRect().Width();
    int endY=GetClientRect().Height();
    while(startY<endY) {
        startX=0;
         while(startX<endX) {
          hdc.BitBlt(startX, startY, bitmapX,bitmapY, MemDC, 0, 0,SRCAND);
            startX+=bitmapX;
        }
        startY+=bitmapY;
     }
     // Send All Your Controls WM_PAINT
     static->HandleMessage(WM_PAINT);
}

NOTE that by doing this way, you don't have to do anything special with your statics. Background is set to white because if it is not, bitmaps will lose their intensity. This is because of SRCAND.
0
 

Expert Comment

by:ekinee
ID: 2816392
.. Sorry, you have to make statics transparent also. You can do it the way I have described earlier.

Just combine them.
0
 

Expert Comment

by:ekinee
ID: 2857298
Owlguru still there. Have a comment.
0
 

Author Comment

by:Owlguru
ID: 2859879
This question is still open. You can place any comments you like. By the way, the answer you gave me does not work with my dialogs, so I'm rejecting your answer. It does weird things with bitmaps, etc.
0
 

Author Comment

by:Owlguru
ID: 2859882
Is there a way to dynamically create a mask bitmap for another bitmap using a certain colour as the transparent colour?
0
 

Expert Comment

by:SirHenry
ID: 2861038
I am convinced it works the way ekinee has pointed out. If you get weird results either there is a small oversight in the example or you have mistyped something. The principle is correct.
0
 
LVL 3

Expert Comment

by:JPM
ID: 2861635
here is some stuff not so different from ekinee 's.
example :

BmpDlg::BmpDlg(TWindow *parent, TResId resId)
      : TDialog(parent, resId)
{
  bitmap = new TBitmap(GetApplication()->GetInstance(), BITMAP_1);
}
//----------------------------------------------------------
bool BmpDlg::EvEraseBkgnd(HDC hDC)
{
//  TDC dc(hDC);
//  TBrush black(TColor::White);
//  dc.SelectObject(white);
//  dc.PatBlt(0, 0, TWindow::Attr.W, TWindow::Attr.H);
  PaintBmp();
  return true;
}
//-----------------------------------------------------
void BmpDlg::EvPaint()
{
  TWindow::EvPaint();
}
//-----------------------------------------------
void BmpDlg::PaintBmp()
{
   TClientDC dc(HWindow);
  TMemoryDC memdc(dc);
  memdc.SelectObject(*bitmap);
  // BitBlt if the sizes of the bmp are well-known
  // StrechBlt if not
  dc.BitBlt(
   0, 0,                       //destination x, y
   BITMAP_W, BITMAP_H,            //bitmap width & height
   memdc,                        //source device context
   0, 0,                         //source X,Y
   SRCCOPY);                     //raster display operation
 
  memdc.RestoreBitmap();
}
//----------------------------------------------
void BmpDlg::Paint(TDC &dc, BOOL erase, TRect& rect)
{
  TWindow::Paint(dc, erase, rect);      
}
//--------------------------------------------------------------
HBRUSH BmpDlg::EvCtlColor (HDC hdc, HWND hwndChild, UINT ctlType)
{
 TDC dc(hdc);
 switch (ctlType)
 {
   case CTLCOLOR_STATIC :
           SetBkColor ( dc , COLOR1 );
       SetTextColor( dc , TColor::White );
       SetBkMode( dc , TRANSPARENT );
      return (HBRUSH) GetStockObject( NULL_BRUSH );
   case CTLCOLOR_BTN :
           SetBkColor ( dc , COLOR1 );
           SetTextColor( dc , TColor::White );
           SetBkMode(dc , TRANSPARENT);
           return TBrush( COLOR2 );
  default :
         return TDialog::EvCtlColor( hdc, hwndChild, ctlType);
 }
}
0
 

Author Comment

by:Owlguru
ID: 2864985
Hey! Ekinee never told me about EvEraseBkgnd()!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:JPM
ID: 3038410
I confrm you should manage WM_ERASEBKGND to draw the bitmap.
but before, when registering your window class, use a NULL brush
WNDCLASS wc;
wc.hbrBackground = NULL; // for the background

Jean-Paul




0
 
LVL 3

Expert Comment

by:JPM
ID: 3039210
also, have a look to the TPictureWindow class and the example
examples/owl/pictwind

Jean-Paul
0
 

Author Comment

by:Owlguru
ID: 3333954
I need a method that does not screw up icons and bitmaps on the dialog box.
0
 

Author Comment

by:Owlguru
ID: 3384810
Sorry guys, I found the answer myself! It works beautifully. All you have to do is implement EvCtlColor like in the comments and suggested answer and implement EvEraseBkgnd but NOT call the default TDialog::EvEraseBkgnd as you normally would. If you leave EvEraseBkgnd blank and returns TRUE, the window's background will be transparent(using the things in the background upon creation) and no background will be painted. This leaves you with your option of painting the background. I found this out after trying out all kinds of methods (almost desperately).
0
 

Author Comment

by:Owlguru
ID: 3384830
Now I need to give someone credit... Who will it be? JPM, you've contributed with the EvEraseBkgnd. On the other hand, ekinee has also contributed with EvCtlColor... I'm waiting for your suggestions...
0
 

Expert Comment

by:SirHenry
ID: 3392433
Ask the expert exchange people to share the points.
0
 

Author Comment

by:Owlguru
ID: 3408517
SirHenry: How do I do that?
0
 

Expert Comment

by:SirHenry
ID: 3412891
Click on "Help Desk" in the top menu and look for "Community Support". There are already some other "split points" topics.
0
 

Author Comment

by:Owlguru
ID: 3418514
Comment accepted as answer
0
 

Author Comment

by:Owlguru
ID: 3418515
I have awarded you 15 points but also I'm awarding JPM 15 points (I'm splitting the points). This is just to let you know.
0
 

Author Comment

by:Owlguru
ID: 3418675
JPM, look for the split-points question (For JPM -- Q10350206)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now