Owlguru
asked on
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(WHI TE_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().Heigh t();
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_P AINT);
}
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.
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(WHI
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().Heigh
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_P
}
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.
.. Sorry, you have to make statics transparent also. You can do it the way I have described earlier.
Just combine them.
Just combine them.
Owlguru still there. Have a comment.
ASKER
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.
ASKER
Is there a way to dynamically create a mask bitmap for another bitmap using a certain colour as the transparent colour?
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.
here is some stuff not so different from ekinee 's.
example :
BmpDlg::BmpDlg(TWindow *parent, TResId resId)
: TDialog(parent, resId)
{
bitmap = new TBitmap(GetApplication()-> GetInstanc e(), 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);
}
}
example :
BmpDlg::BmpDlg(TWindow *parent, TResId resId)
: TDialog(parent, resId)
{
bitmap = new TBitmap(GetApplication()->
}
//------------------------
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);
}
}
ASKER
Hey! Ekinee never told me about EvEraseBkgnd()!
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
but before, when registering your window class, use a NULL brush
WNDCLASS wc;
wc.hbrBackground = NULL; // for the background
Jean-Paul
also, have a look to the TPictureWindow class and the example
examples/owl/pictwind
Jean-Paul
examples/owl/pictwind
Jean-Paul
ASKER
I need a method that does not screw up icons and bitmaps on the dialog box.
ASKER
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).
ASKER
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...
Ask the expert exchange people to share the points.
ASKER
SirHenry: How do I do that?
Click on "Help Desk" in the top menu and look for "Community Support". There are already some other "split points" topics.
ASKER
Comment accepted as answer
ASKER
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.
ASKER
JPM, look for the split-points question (For JPM -- Q10350206)
ASKER
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.