|
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!
|
|
|
|