Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

Painting dialog background

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
Owlguru
Asked:
Owlguru
  • 11
  • 4
  • 3
  • +1
1 Solution
 
ekineeCommented:
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
 
OwlguruAuthor Commented:
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
 
ekineeCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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

Just combine them.
0
 
ekineeCommented:
Owlguru still there. Have a comment.
0
 
OwlguruAuthor Commented:
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
 
OwlguruAuthor Commented:
Is there a way to dynamically create a mask bitmap for another bitmap using a certain colour as the transparent colour?
0
 
SirHenryCommented:
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
 
JPMCommented:
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
 
OwlguruAuthor Commented:
Hey! Ekinee never told me about EvEraseBkgnd()!
0
 
JPMCommented:
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
 
JPMCommented:
also, have a look to the TPictureWindow class and the example
examples/owl/pictwind

Jean-Paul
0
 
OwlguruAuthor Commented:
I need a method that does not screw up icons and bitmaps on the dialog box.
0
 
OwlguruAuthor Commented:
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
 
OwlguruAuthor Commented:
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
 
SirHenryCommented:
Ask the expert exchange people to share the points.
0
 
OwlguruAuthor Commented:
SirHenry: How do I do that?
0
 
SirHenryCommented:
Click on "Help Desk" in the top menu and look for "Community Support". There are already some other "split points" topics.
0
 
OwlguruAuthor Commented:
Comment accepted as answer
0
 
OwlguruAuthor Commented:
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
 
OwlguruAuthor Commented:
JPM, look for the split-points question (For JPM -- Q10350206)
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now