CBitmapDialog::CBitmapDialog()
{
CommonConstruct();
}
CBitmapDialog::CBitmapDialog(UINT uResource, CWnd* pParent /*=NULL*/)
: CDialog(uResource, pParent)
{
CommonConstruct();
}
CBitmapDialog::CBitmapDialog(LPCTSTR pszResource, CWnd* pParent /*=NULL*/)
: CDialog(pszResource, pParent)
{
CommonConstruct();
}
void CBitmapDialog::CommonConstruct()
{
m_nType = BITMAP_TILE;
VERIFY(m_HollowBrush.CreateStockObject(HOLLOW_BRUSH)); //{{AFX_DATA_INIT(CBitmapDialog)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
BOOL CBitmapDialog :: SetBitmap(UINT uResource, int nType /*BITMAP_TILE*/) {
m_nType = nType;
ASSERT(m_nType == BITMAP_TILE || m_nType == BITMAP_STRETCH || m_nType == BITMAP_CENTER);
return m_bmpBackground.LoadResource(uResource);
}
void CBitmapDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBitmapDialog)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBitmapDialog, CDialog)
//{{AFX_MSG_MAP(CBitmapDialog)
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
ON_WM_QUERYNEWPALETTE()
ON_WM_PALETTECHANGED()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CBitmapDialog message handlersBOOL CBitmapDialog::OnEraseBkgnd(CDC* pDC)
{
if(m_bmpBackground.GetPixelPtr() != 0) {
ASSERT(m_nType == BITMAP_TILE || m_nType == BITMAP_STRETCH || m_nType == BITMAP_CENTER);
CRect rc;
GetClientRect(rc);
int x = 0, y = 0; switch(m_nType) {
case BITMAP_CENTER:
CDialog::OnEraseBkgnd(pDC);
x = (rc.Width() - m_bmpBackground.GetWidth()) / 2;
y = (rc.Height() - m_bmpBackground.GetHeight()) / 2;
m_bmpBackground.DrawDIB(pDC, x, y);
break; case BITMAP_STRETCH:
m_bmpBackground.DrawDIB(pDC, 0, 0, rc.Width(), rc.Height());
break; default:
while(y < rc.Height()) {
while(x < rc.Width()) {
m_bmpBackground.DrawDIB(pDC, x, y);
x += m_bmpBackground.GetWidth();
}
x = 0;
y += m_bmpBackground.GetHeight();
}
break;
}
} else
return CDialog::OnEraseBkgnd(pDC); return TRUE;
}HBRUSH CBitmapDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if(m_bmpBackground.GetPixelPtr() != 0) {
switch(nCtlColor) {
case CTLCOLOR_STATIC:
TCHAR lpszClassName[255];
GetClassName(pWnd->m_hWnd, lpszClassName, 255);
if(_tcscmp(lpszClassName, TRACKBAR_CLASS) == 0)
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); case CTLCOLOR_BTN:
pDC->SetBkMode(TRANSPARENT);
return HBRUSH(m_HollowBrush); default:
break;
}
}
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}BOOL CBitmapDialog::OnQueryNewPalette()
{
CPalette * pPal = m_bmpBackground.GetPalette();
if( pPal != 0 && GetSafeHwnd() != 0 ) {
CClientDC dc(this);
CPalette * pOldPalette = dc.SelectPalette(pPal, FALSE);
UINT nChanged = dc.RealizePalette();
dc.SelectPalette(pOldPalette, TRUE); if (nChanged == 0)
return FALSE; Invalidate();
return TRUE;
}
return CDialog::OnQueryNewPalette();
}void CBitmapDialog::OnPaletteChanged(CWnd* pFocusWnd)
{
CPalette * pPal = m_bmpBackground.GetPalette();
if( pPal != 0 && GetSafeHwnd() != 0 && pFocusWnd != this && ! IsChild(pFocusWnd) ) {
CClientDC dc(this);
CPalette * pOldPalette = dc.SelectPalette(pPal, TRUE);
UINT nChanged = dc.RealizePalette();
dc.SelectPalette(pOldPalette, TRUE); if( nChanged )
Invalidate();
} else
CDialog::OnPaletteChanged(pFocusWnd);
}
{
CommonConstruct();
}
CBitmapDialog::CBitmapDialog(UINT uResource, CWnd* pParent /*=NULL*/)
: CDialog(uResource, pParent)
{
CommonConstruct();
}
CBitmapDialog::CBitmapDialog(LPCTSTR pszResource, CWnd* pParent /*=NULL*/)
: CDialog(pszResource, pParent)
{
CommonConstruct();
}
void CBitmapDialog::CommonConstruct()
{
m_nType = BITMAP_TILE;
VERIFY(m_HollowBrush.CreateStockObject(HOLLOW_BRUSH)); //{{AFX_DATA_INIT(CBitmapDialog)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
BOOL CBitmapDialog :: SetBitmap(UINT uResource, int nType /*BITMAP_TILE*/) {
m_nType = nType;
ASSERT(m_nType == BITMAP_TILE || m_nType == BITMAP_STRETCH || m_nType == BITMAP_CENTER);
return m_bmpBackground.LoadResource(uResource);
}
void CBitmapDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBitmapDialog)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBitmapDialog, CDialog)
//{{AFX_MSG_MAP(CBitmapDialog)
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
ON_WM_QUERYNEWPALETTE()
ON_WM_PALETTECHANGED()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CBitmapDialog message handlersBOOL CBitmapDialog::OnEraseBkgnd(CDC* pDC)
{
if(m_bmpBackground.GetPixelPtr() != 0) {
ASSERT(m_nType == BITMAP_TILE || m_nType == BITMAP_STRETCH || m_nType == BITMAP_CENTER);
CRect rc;
GetClientRect(rc);
int x = 0, y = 0; switch(m_nType) {
case BITMAP_CENTER:
CDialog::OnEraseBkgnd(pDC);
x = (rc.Width() - m_bmpBackground.GetWidth()) / 2;
y = (rc.Height() - m_bmpBackground.GetHeight()) / 2;
m_bmpBackground.DrawDIB(pDC, x, y);
break; case BITMAP_STRETCH:
m_bmpBackground.DrawDIB(pDC, 0, 0, rc.Width(), rc.Height());
break; default:
while(y < rc.Height()) {
while(x < rc.Width()) {
m_bmpBackground.DrawDIB(pDC, x, y);
x += m_bmpBackground.GetWidth();
}
x = 0;
y += m_bmpBackground.GetHeight();
}
break;
}
} else
return CDialog::OnEraseBkgnd(pDC); return TRUE;
}HBRUSH CBitmapDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if(m_bmpBackground.GetPixelPtr() != 0) {
switch(nCtlColor) {
case CTLCOLOR_STATIC:
TCHAR lpszClassName[255];
GetClassName(pWnd->m_hWnd, lpszClassName, 255);
if(_tcscmp(lpszClassName, TRACKBAR_CLASS) == 0)
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); case CTLCOLOR_BTN:
pDC->SetBkMode(TRANSPARENT);
return HBRUSH(m_HollowBrush); default:
break;
}
}
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}BOOL CBitmapDialog::OnQueryNewPalette()
{
CPalette * pPal = m_bmpBackground.GetPalette();
if( pPal != 0 && GetSafeHwnd() != 0 ) {
CClientDC dc(this);
CPalette * pOldPalette = dc.SelectPalette(pPal, FALSE);
UINT nChanged = dc.RealizePalette();
dc.SelectPalette(pOldPalette, TRUE); if (nChanged == 0)
return FALSE; Invalidate();
return TRUE;
}
return CDialog::OnQueryNewPalette();
}void CBitmapDialog::OnPaletteChanged(CWnd* pFocusWnd)
{
CPalette * pPal = m_bmpBackground.GetPalette();
if( pPal != 0 && GetSafeHwnd() != 0 && pFocusWnd != this && ! IsChild(pFocusWnd) ) {
CClientDC dc(this);
CPalette * pOldPalette = dc.SelectPalette(pPal, TRUE);
UINT nChanged = dc.RealizePalette();
dc.SelectPalette(pOldPalette, TRUE); if( nChanged )
Invalidate();
} else
CDialog::OnPaletteChanged(pFocusWnd);
}
解决方案 »
- CFileDialog的问题,求帮助
- 关于DWORD_PTR
- ACTIVEX调用其他dll遇到的问题!
- 我的程序包括listctrl和combobox,默认SetCurSel(0),然后我编程实现了在listctrl中回车自动切换到下一行,可是当我选择combobox的别的选
- GDI+ 1.1 版本 哪里下载?
- 请问如何将单文档窗体的客户区部分做成透明?
- 在DLL中输出类的成员函数后,在程序中调用是否要引用类的方式访问,还是直接访问?
- 还是大的PictureBox中放小的动态创建的PictureBox控件的问题,请再帮帮我!
- 很土的问题
- 手机短信息
- 有谁知道163帐号可不可以漫游和怎样漫游?
- shengzi 又来拉!我的具体问题在里面!请高手进去看看thanks!
应该用CClientDC。
它的使用方法和CDC几乎是一样的。
void CAboutDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
if(m_LogoText.IsEmpty())
return; CRect rectTabCtrl;
GetWindowRect(rectTabCtrl);
ScreenToClient(rectTabCtrl); CRect rectOk;
GetDlgItem(IDOK)->GetWindowRect(rectOk);
ScreenToClient(rectOk); dc.SetBkMode(TRANSPARENT); CRect rectText;
rectText.left = rectTabCtrl.left+24;
rectText.top = rectOk.top;
rectText.bottom = rectOk.bottom;
rectText.right = rectOk.left; CFont * OldFont = dc.SelectObject(&m_fontLogo); // draw text in DC
COLORREF OldColor = dc.SetTextColor( ::GetSysColor( COLOR_3DHILIGHT)); dc.DrawText( m_LogoText, rectText + CPoint(1,1), DT_SINGLELINE | DT_LEFT | DT_VCENTER);
dc.SetTextColor( ::GetSysColor( COLOR_3DSHADOW));
dc.DrawText( m_LogoText, rectText, DT_SINGLELINE | DT_LEFT | DT_VCENTER); // restore old text color
dc.SetTextColor( OldColor);
// restore old font
dc.SelectObject(OldFont);
// Do not call CDialog::OnPaint() for painting messages
}
witsoft2000 的方法我试过了,用在一般对话框上是对的,但用在有背景的对话框上却什么也没有,其他人的方法也大同小异。
如果不指定背景色
在XXX::OnPain()中
CClientDC dc(this);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
CDC dcMemory;
dcMemory.CreateCompatibleDC(&dc);
dcMemory.SelectObject(&bmp);
dc.BitBlt(0,0,132,32,&dcMemory,0,0,SRCCOPY);
dc.SetBkMode(TRANSPARENT);
dc.TextOut(0,0,"AAAAAAAAAAAAAAAAAAAAAAAAAA");
如果要指定背景色
再在OnCtlColor 中加入
HBRUSH hbr ;//= CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
CBrush br;
pDC->SetBkMode(TRANSPARENT);
br.CreateSysColorBrush(2);
hbr = (HBRUSH) br.m_hObject;
代码如下:
// test3Dlg.h : header file
//#if !defined(AFX_TEST3DLG_H__CA9C8898_6BAD_485E_B564_A8EC7C242AA7__INCLUDED_)
#define AFX_TEST3DLG_H__CA9C8898_6BAD_485E_B564_A8EC7C242AA7__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg dialogclass CTest3Dlg : public CDialog
{
// Construction
public:
CTest3Dlg(CWnd* pParent = NULL); // standard constructor void SetBitmap(UINT resid);// Dialog Data
//{{AFX_DATA(CTest3Dlg)
enum { IDD = IDD_TEST3_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTest3Dlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
HICON m_hIcon; RECT m_Rect;
CBitmap mBitmap;
CBrush mHollowBrush; // Generated message map functions
//{{AFX_MSG(CTest3Dlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
virtual void OnOK();
virtual void OnCancel();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_TEST3DLG_H__CA9C8898_6BAD_485E_B564_A8EC7C242AA7__INCLUDED_)
// test3Dlg.cpp : implementation file
//#include "stdafx.h"
#include "test3.h"
#include "test3Dlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg dialogCTest3Dlg::CTest3Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CTest3Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CTest3Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}BEGIN_MESSAGE_MAP(CTest3Dlg, CDialog)
//{{AFX_MSG_MAP(CTest3Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg message handlersBOOL CTest3Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
GetDlgItem(IDC_OUT)->GetWindowRect(&m_Rect);
ScreenToClient(&m_Rect);
return TRUE; // return TRUE unless you set the focus to a control
}void CTest3Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon); dc.TextOut(m_Rect.left,m_Rect.top,"XXXXXXXX");
}
else
{
CDialog::OnPaint();
}
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTest3Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CTest3Dlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}void CTest3Dlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}BOOL CTest3Dlg::OnEraseBkgnd(CDC* pDC)
{
if (mBitmap.m_hObject!=NULL)
{
CDC MemDC;
BITMAP bm;
CRect Rect;
int x=0,y=0;
GetClientRect(&Rect);
mBitmap.GetObject(sizeof(BITMAP),&bm);
MemDC.CreateCompatibleDC(pDC);
CBitmap *p01dBitmap=MemDC.SelectObject(&mBitmap); CDialog::OnEraseBkgnd(pDC);
x=(Rect.Width()-bm.bmWidth)/2;
y=(Rect.Height()-bm.bmHeight)/2;
pDC->BitBlt(x,y,bm.bmWidth,bm.bmHeight,&MemDC,0,0,SRCCOPY); MemDC.SelectObject(p01dBitmap);
return TRUE;
}
else
return CDialog::OnEraseBkgnd(pDC);
}HBRUSH CTest3Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if (mBitmap.GetSafeHandle()!=NULL)
{
if (nCtlColor==CTLCOLOR_STATIC)
{
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)mHollowBrush.m_hObject;
}
else
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
return hbr;
}
}
else
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
return hbr;
}
}void CTest3Dlg::SetBitmap(UINT resid)
{
mBitmap.LoadBitmap(resid);
}各位高手,请指教!
if ( IsIconic())
{
}
else
{
CDialog::OnPaint();
CClientDC dc(this);
dc.TextOut(0,0,"XXXXXXXXX");
}
if ( IsIconic())
{
}
else
{
CDialog::OnPaint();
CClientDC dc(this);
dc.TextOut(0,0,"XXXXXXXXX");
}