|
Can you add a video of a step-by-step solution? Thank you very much.
-obliviga
|
|
|
|
|
You should add a video. Thanks.
|
|
|
|
|
How I change the background color of a Static Control (on the dialog) to the color of the bitmap (it's a gradient one, so i can't set static control background to bitmap color. Transparent background take dialog color, not image one)?
10x
Sandro
|
|
|
|
|
Hi Nishant,
Very nice article. I am new to VC++ and wanted to apply dialog a stretch background image. Your article resolved it. More to that I want to apply stretch background image to button. How can I achieve this?
Thanks,
|
|
|
|
|
Hi,
How can we implement image background in win32 (dailog based) application ?
|
|
|
|
|
Hi,
Is it possible to change the bitmap?
|
|
|
|
|
Why I got this error message when try to build my project? Did I miss some step?
Sorry, my mistake... it is really helpful. great job.
Thanks a lot.
modified on Thursday, April 3, 2008 8:54 PM
|
|
|
|
|
Please Help i really need to do this but when i try to do what think is right it highlight's #include<bdialog.h> pleaase help me please i use Dev cpp
|
|
|
|
|
This code is wonderful for a beginner like me. However, I have Static Text in my dialog, and it seems that the text will not show up when I use this code. Could anybody please help?
|
|
|
|
|
try BringWindowToTop(); with the static text control.
|
|
|
|
|
Hello
I am facing the same problem. Can you please help me that how can i solve this?
I tried BringWindowToTop() but it not worked.
|
|
|
|
|
Simply needed to drop something in to handle a background image on a splash screen, and this worked perfectly. Thx.
David
|
|
|
|
|
PS: I added a header block and fully referenced you - give credit where due!
|
|
|
|
|
add a bitmap as a resource to your project:
Make a 256 color bitmap, save it in the 'res' folder of your project.
In vc++, add the file to your projects resource folder in vc++ (click on 'file view' in left frame, right click on resources folder, click add file)
Click on the 'Insert' menu (at the top of vc++), click 'resource', add your bitmap.
Click on 'ResourceView' in the left frame of vc++, you should see IDB_BITMAP1 in the 'bitmap' folder.
|
|
|
|
|
Hi thanks for the code, while I'm trying to change the background inside my code I get a Debug Assertion fault while loading the new bitmap. If this error ignored on a release version after 30 changes of the bitmap the application ignores tottaly the command. I solved this problem by changing your implementation from static CBitmap to CBitmap* more specificaly:
BDialog.h
<br />
#if !defined(AFX_BDIALOG_H__A303185C_4254_4F5F_B5A8_462531A59B83__INCLUDED_)<br />
#define AFX_BDIALOG_H__A303185C_4254_4F5F_B5A8_462531A59B83__INCLUDED_<br />
<br />
#if _MSC_VER > 1000<br />
#pragma once<br />
#endif // _MSC_VER > 1000<br />
<br />
<br />
#define StyleTile 0<br />
#define StyleCenter 1<br />
#define StyleStretch 2<br />
<br />
class CBDialog : public CDialog<br />
{<br />
public:<br />
int SetBitmap(UINT nIDResource);<br />
void SetBitmapStyle(int style);<br />
<br />
CBDialog(UINT nIDTemplate, CWnd* pParent = NULL);
<br />
<br />
<br />
protected:<br />
<br />
protected:<br />
<br />
afx_msg BOOL OnEraseBkgnd(CDC* pDC);<br />
DECLARE_MESSAGE_MAP()<br />
private:<br />
CBitmap* m_bitmap;<br />
int m_style;<br />
};<br />
<br />
<br />
#endif // !defined(AFX_BDIALOG_H__A303185C_4254_4F5F_B5A8_462531A59B83__INCLUDED_)<br />
<br />
BDialog.cpp
<br />
<br />
#include "stdafx.h"<br />
#include "BDialog.h"<br />
<br />
#ifdef _DEBUG<br />
#define new DEBUG_NEW<br />
#undef THIS_FILE<br />
static char THIS_FILE[] = __FILE__;<br />
#endif<br />
<br />
<br />
<br />
CBDialog::CBDialog(UINT nIDTemplate, CWnd* pParent )<br />
: CDialog(nIDTemplate, pParent)<br />
{<br />
m_style=StyleTile;<br />
m_bitmap=new CBitmap();<br />
}<br />
<br />
<br />
BEGIN_MESSAGE_MAP(CBDialog, CDialog)<br />
ON_WM_ERASEBKGND()<br />
END_MESSAGE_MAP()<br />
<br />
<br />
BOOL CBDialog::OnEraseBkgnd(CDC* pDC) <br />
{<br />
CDialog::OnEraseBkgnd(pDC); <br />
if(!m_bitmap->m_hObject)<br />
return true;<br />
<br />
CRect rect;<br />
GetClientRect(&rect);<br />
CDC dc;<br />
dc.CreateCompatibleDC(pDC);<br />
CBitmap* pOldBitmap = dc.SelectObject(m_bitmap);<br />
int bmw, bmh ;<br />
BITMAP bmap;<br />
m_bitmap->GetBitmap(&bmap);<br />
bmw = bmap.bmWidth;<br />
bmh = bmap.bmHeight;<br />
int xo=0, yo=0;<br />
<br />
if(m_style == StyleTile)<br />
{<br />
for (yo = 0; yo < rect.Height(); yo += bmh)<br />
{<br />
for (xo = 0; xo < rect.Width(); xo += bmw)<br />
{<br />
pDC->BitBlt (xo, yo, rect.Width(),<br />
rect.Height(), &dc,<br />
0, 0, SRCCOPY);<br />
}<br />
}<br />
<br />
}<br />
<br />
if(m_style == StyleCenter)<br />
{<br />
if(bmw < rect.Width())<br />
xo = (rect.Width() - bmw)/2;<br />
else <br />
xo=0;<br />
if(bmh < rect.Height())<br />
yo = (rect.Height()-bmh)/2;<br />
else<br />
yo=0;<br />
pDC->BitBlt (xo, yo, rect.Width(),<br />
rect.Height(), &dc,<br />
0, 0, SRCCOPY);<br />
}<br />
<br />
if(m_style == StyleStretch)<br />
{<br />
pDC->StretchBlt(xo, yo, rect.Width(),<br />
rect.Height(), &dc,<br />
0, 0,bmw,bmh, SRCCOPY);<br />
}<br />
<br />
<br />
dc.SelectObject(pOldBitmap);<br />
<br />
<br />
return true;<br />
}<br />
<br />
void CBDialog::SetBitmapStyle(int style)<br />
{<br />
if((style==StyleTile)||<br />
(style==StyleCenter)||<br />
(style==StyleStretch))<br />
{ <br />
m_style = style;<br />
}<br />
<br />
}<br />
<br />
int CBDialog::SetBitmap(UINT nIDResource)<br />
{<br />
delete m_bitmap;<br />
m_bitmap=new CBitmap();<br />
if(m_bitmap->LoadBitmap(nIDResource))<br />
return 0;<br />
else<br />
return 1;
}<br />
Enjoy and again thanks for your code
|
|
|
|
|
Hello Nishanth,
That was nice article . If I want to set the image (what you are doing with dialog) to other window controls like Menu and status bar,How can I do that?
Best Regards
Sudhakar Chavali Sharma
;)
|
|
|
|
|
When DPI is changed then the dialog size also changed...how could it possible to keep it size fix when DPI is changed..for examnple when i create a dialog in 96 dpi then its size will not increase in 120 dpi environment..thanks...Ashif
|
|
|
|
|
Hi! This works really well for a dialog background. However, in the app on which I am working (as a not-very experienced programmer), I need to use static text, radio buttons, check boxes and a menu bar. If I want the dialog background to be a simple colour, then the override for OnCtlColor() works fine. However, I really want to use a textured background, so that the text is set against that and not a background colour. Is there a way to set the background of, say, a radio button to the same texture as that of the main dialog?
brian
|
|
|
|
|
I'm not using a bitmap as a background but I'm doing a FillSolidRect and rounded buttons have a white line around them where the button rect should be. Any clue on how I could fix it?
"It is better to remain silent and be thought a fool than to open one's mouth and remove all doubt." - Abraham Lincoln
|
|
|
|
|
I wanted more than the 256 colors from a resource bitmap so I modified the code to load the bitmap from a file, including higher resolutions bitmaps (I've tested it w/ a 24-bit color bitmap).
Modifications are as follows:
CBDialog.h
1. Change CBitmap m_bitmap; to CBitmap *m_bitmap;
2. Change the int SetBitmap(UINT nIDResource); fucntion def to
int SetBitmap(const char *cszBitmapFile);
CBDialog.cpp
1. Throughout the whole file, change any reference of &m_bitmap or
m_bitmap. to m_bitmap or m_bitmap-> (correspondingly)
2. In the constructor, add m_bitmap = 0; somewhere
3. In OnEraseBkgnd(), change the line if(!m_bitmap.m_hObject) to
if(!m_bitmap || !m_bitmap->m_hObject)
4. Change the whole SetBitmap() function to:
int CBDialog::SetBitmap(const char *cszBitmapFile)
{
HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL, cszBitmapFile, IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_CREATEDIBSECTION);
if(!hBitmap)
return 1; // error; cszBitmapFile probably points to a non-existent file
m_bitmap = CBitmap::FromHandle(hBitmap);
if(m_bitmap)
return 0;
else
return 2;//error
}
|
|
|
|
|
I have modify the code so that it can also implement the transparent background to the dialog ---- So you can watermark your company logo on the background.
Create the bitmap you want to display and also create the mask. (In the mask, whatever is white need to be converted to black and all other color needed to convert the black)
Modify BDialog.h
#define StyleTile 0
#define StyleCenter 1
#define StyleStretch 2
class CBDialog : public CDialog
{
// Construction
public:
int SetBitmap(UINT nIDResource,UINT nIDResourcemask=0);
void SetBitmapStyle(int style);
CBDialog(UINT nIDTemplate, CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CBDialog)
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBDialog)
protected:
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CBDialog)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CBitmap m_bitmap;
CBitmap m_bitmap_mask;
int m_style;
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_BDIALOG_H__A303185C_4254_4F5F_B5A8_462531A59B83__INCLUDED_)
Modify the BDialog.cpp
// BDialog.cpp : implementation file
//
#include "stdafx.h"
#include "BDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBDialog dialog
CBDialog::CBDialog(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
: CDialog(nIDTemplate, pParent)
{
//{{AFX_DATA_INIT(CBDialog)
// NOTE: the ClassWizard will add member initialization here
m_style=StyleTile;
//}}AFX_DATA_INIT
}
BEGIN_MESSAGE_MAP(CBDialog, CDialog)
//{{AFX_MSG_MAP(CBDialog)
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBDialog message handlers
BOOL CBDialog::OnEraseBkgnd(CDC* pDC)
{
CDialog::OnEraseBkgnd(pDC);
if(!m_bitmap.m_hObject && !m_bitmap_mask.m_hObject)
return true;
if (!m_bitmap_mask.m_hObject){ //old way
CBitmap* pOldBitmap = NULL;
CRect rect;
CDC dc;
int bmw, bmh ;
BITMAP bmap;
int xo=0, yo=0;
GetClientRect(&rect);
dc.CreateCompatibleDC(pDC);
pOldBitmap = dc.SelectObject(&m_bitmap);
m_bitmap.GetBitmap(&bmap);
bmw = bmap.bmWidth;
bmh = bmap.bmHeight;
if(m_style == StyleTile){
for (yo = 0; yo < rect.Height(); yo += bmh){
for (xo = 0; xo < rect.Width(); xo += bmw){
pDC->BitBlt (xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0, SRCCOPY);
}
}
}
if(m_style == StyleCenter){
if(bmw < rect.Width())
xo = (rect.Width() - bmw)/2;
else
xo=0;
if(bmh < rect.Height())
yo = (rect.Height()-bmh)/2;
else
yo=0;
pDC->BitBlt (xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0, SRCCOPY);
}
if(m_style == StyleStretch){
pDC->StretchBlt(xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0,bmw,bmh, SRCCOPY);
}
dc.SelectObject(pOldBitmap);
::DeleteObject(dc);
}
else {
// transparent background image
CRect rect;
int x0=0, y0=0;
int cx = 0, cy = 0;
GetClientRect(&rect);
int bmw, bmh ;
BITMAP bmap;
m_bitmap.GetBitmap(&bmap);
bmw = bmap.bmWidth;
bmh = bmap.bmHeight;
x0 =rect.left;
x0 =rect.top;
cx = rect.Width();
cy = rect.Height();
if(m_style == StyleCenter){
if(bmw < cx)
x0 = (cx - bmw)/2;
else
x0=0;
if(bmh < cy)
y0 = (cy-bmh)/2;
else
y0=0;
}
CDC dcBmp,dcMask;
dcBmp.CreateCompatibleDC(pDC);
dcBmp.SelectObject(&m_bitmap);
dcMask.CreateCompatibleDC(pDC);
dcMask.SelectObject(&m_bitmap_mask);
CDC hdcMem;
hdcMem.CreateCompatibleDC(pDC);
CBitmap hBitmap;
if(m_style == StyleCenter)
hBitmap.CreateCompatibleBitmap(pDC,bmw,bmh);
else if(m_style == StyleStretch || m_style == StyleTile)
hBitmap.CreateCompatibleBitmap(pDC,cx,cy);
else{
}
hdcMem.SelectObject(hBitmap);
hdcMem.BitBlt(0,0,bmw,bmh,pDC,0,0,SRCCOPY);
hdcMem.BitBlt(0,0,bmw,bmh,&dcBmp,0,0,SRCINVERT);
hdcMem.BitBlt(0,0,bmw,bmh,&dcMask,0,0,SRCAND);
hdcMem.BitBlt(0,0,bmw,bmh,&dcBmp,0,0,SRCINVERT);
if(m_style == StyleCenter)
pDC->BitBlt (x0,y0,bmw,bmh,&hdcMem,0,0,SRCCOPY);
else if(m_style == StyleStretch)
pDC->StretchBlt (0,0,cx,cy,&hdcMem,0,0,bmw,bmh,SRCCOPY);
else{
for (y0 = 0; y0 < rect.Height(); y0 += bmh){
for (x0 = 0; x0 < rect.Width(); x0 += bmw){
pDC->BitBlt (x0, y0, bmw, bmh, &hdcMem, 0, 0, SRCCOPY);
}
}
}
hdcMem.DeleteDC();
hBitmap.DeleteObject();
DeleteDC(dcMask);
DeleteDC(dcBmp);
}
return true;
}
void CBDialog::SetBitmapStyle(int style)
{
if((style==StyleTile)||
(style==StyleCenter)||
(style==StyleStretch))
{
m_style = style;
}
}
int CBDialog::SetBitmap(UINT nIDResource,UINT nIDResourcemask)
{
if(m_bitmap.LoadBitmap(nIDResource)){
if (nIDResourcemask > 0){
if(!m_bitmap_mask.LoadBitmap(nIDResource))
return 1;
}
return 0;
}
return 1;//error
}
Have fun!
|
|
|
|
|
Hi Nishant,
Please forgive my "newbiness" if you will, but is there another way to make this so that you don't have to do the whole CDialog thing? The reason I ask is because there's another class that I'm already using that requires this.
Thanks in advance,
Don
|
|
|
|
|
|
It's the well-known CResizableDialog
class from Paolo Messina. If not that one, one similar, but they too require that you find and replace all instances of "CDialog" in the header and source file.
I'm building a skinnable Winamp sort of application and it seems like every class I find that would be useful, without having to re-invent any wheels that is, requires the same thing.
I guess there's a way to bundle up all of these classes into one source, and just make sure that there's no duplicate function names/variables, etc., and have them all derived from the same base? I'm sure it's possible, but I'm new to C++, not programming, just this language, which seems to be a little like going from Pig Latin to Japanese.
Having said that, any further details/advice that you can give me on this endeavor will be tremendously appreciated.
Cheers,
Don
|
|
|
|
|
Thank you very much!
<marquee behavior="alternate">Hosam Aly Mahmoud
|
|
|
|
|