想用下面的代码设置对话框窗体背景、按纽、文本框和标题背景前景色,
Button的属性需要设置为:样式-->所有者绘制,
但是感觉效果不理想,请大家帮忙提下改善建议//////////////////////////////////////////////////////
// skinDlg.h : header file
class CSkinDlg : public CDialog
{
protected:
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);public:
CBrush m_MeaningBrush; //背景颜色画刷
void DrawTitleBar(CDC *pDC);
void OnNcPaint();
BOOL OnEraseBkgnd(CDC* pDC);
void ShowBitmap(CDC *pDC, int x, int y, int nW, int nH, CBitmap &m_bitmap); afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
}//////////////////////////////////////////////////////
// skinDlg.cpp : implementation file
CSkinDlg::CSkinDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSkinDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//创建为红色背景
m_MeaningBrush.CreateSolidBrush(RGB(255,0,0));
}BEGIN_MESSAGE_MAP(CSkinDlg, CDialog)
//{{AFX_MSG_MAP(CSkinDlg)
ON_WM_PAINT()
ON_WM_DRAWITEM()
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()//修改窗体背景颜色
void CSkinDlg::OnPaint()
{
if (IsIconic())
{ }
else
{
CRect rect;
CPaintDC dc(this);
GetClientRect(rect);
//设置为白色背景
dc.FillSolidRect(rect,RGB(255,255,255));
CDialog::OnPaint();
}
}//修改Button背景色和字体颜色
//Button的属性需要设置为:样式-->所有者绘制
void CSkinDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if(nIDCtl==IDC_BUTTON1) //checking for the button
{
//Button属性为自画
CDC dc;
RECT rect;
dc.Attach(lpDrawItemStruct ->hDC); // Get the Button DC to CDC
rect = lpDrawItemStruct->rcItem; //Store the Button rect to our local rect.
dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));
dc.FillSolidRect(&rect,RGB(100,100,255));//Here you can define the required color to appear on the Button.
UINT state=lpDrawItemStruct->itemState; //This defines the state of the Push button either pressed or not.
if((state & ODS_SELECTED))
{
dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);
}
else
{
dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
}
dc.SetBkColor(RGB(100,100,255)); //Setting the Text Background color
dc.SetTextColor(RGB(255,0,0)); //Setting the Text Color
TCHAR buffer[MAX_PATH]; //To store the Caption of the button.
ZeroMemory(buffer,MAX_PATH ); //Intializing the buffer to zero
::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH); //Get the Caption of Button Window
dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);//Redraw the Caption of Button Window
dc.Detach(); // Detach the Button DC
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}//修改文本框背景色和字体颜色
HBRUSH CSkinDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_EDIT1) //编辑框的名称ID
{
pDC->SetTextColor(RGB(255,255,255));//编辑框类的字体设置为白色;
pDC->SetBkMode(TRANSPARENT);//设置为透明不影响字体颜色
return (HBRUSH)m_MeaningBrush.GetSafeHandle();//返回当前编辑框的背景画刷
}
if (pWnd->GetDlgCtrlID() ==IDC_STATIC1) //编辑框的名称ID
{
pDC->SetTextColor(RGB(255,255,255));//编辑框类的字体设置为白色;
pDC->SetBkMode(TRANSPARENT);//设置为透明不影响字体颜色
return (HBRUSH)m_MeaningBrush.GetSafeHandle();//返回当前编辑框的背景画刷
}
// TODO: Return a different brush if the default is not desired
return hbr;
}//自绘窗体的标题
void CSkinDlg::DrawTitleBar(CDC *pDC)
{
CDC memDC;
CBitmap bmp;
CRect rect, rtTitle;
CPoint point;
GetClientRect(rect);
//获取标题栏大小
rtTitle.left = GetSystemMetrics(SM_CXFRAME);
rtTitle.top = GetSystemMetrics(SM_CYFRAME);
rtTitle.right = rect.right - rect.left - GetSystemMetrics(SM_CXFRAME);
rtTitle.bottom = rtTitle.top + GetSystemMetrics(SM_CYSIZE);
point.x = rect.right + GetSystemMetrics(SM_CXFRAME);
point.y = 30;
memDC.CreateCompatibleDC(NULL);
bmp.CreateCompatibleBitmap(pDC,point.x,point.y);
CBitmap *pOldBit=memDC.SelectObject(&bmp);
//背景颜色
memDC.FillSolidRect(0,0,point.x,point.y,RGB(125,0,125));
////贴图
//CBitmap pic;
//pic.LoadBitmap(IDB_BITMAP1);
//ShowBitmap(&memDC,0,0, rtTitle.right, 30, pic);
pDC->BitBlt(0,0,point.x,point.y,&memDC,0,0,SRCCOPY);
pOldBit->DeleteObject();
memDC.DeleteDC();
bmp.DeleteObject();
}//回调函数
LRESULT CSkinDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
//return CDialog::DefWindowProc(message, wParam, lParam);
LRESULT lrst=CDialog::DefWindowProc(message, wParam, lParam);
if (message==WM_MOVE||message==WM_PAINT||message==WM_NCPAINT||message==WM_NCACTIVATE ||message == WM_NOTIFY)
{
CDC* pWinDC = GetWindowDC();
if (pWinDC)
DrawTitleBar(pWinDC);
ReleaseDC(pWinDC);
}
//return lrst;
return CDialog::DefWindowProc(message, wParam, lParam);
}void CSkinDlg::OnNcPaint()
{
//CDialog::OnNcPaint();
}BOOL CSkinDlg::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}void CSkinDlg::ShowBitmap(CDC *pDC, int x, int y, int nW, int nH, CBitmap &m_bitmap)
{
CDC memDc, mdc;
memDc.CreateCompatibleDC(NULL);
mdc.CreateCompatibleDC(NULL);
CBitmap memBitmap;
memBitmap.CreateCompatibleBitmap(pDC,nW,nH);
CBitmap *OldBmp = memDc.SelectObject(&m_bitmap);
mdc.SelectObject(&m_bitmap);
memDc.BitBlt(x,y,nW,nH,&mdc,0,0,SRCCOPY);
BITMAP bm;
m_bitmap.GetBitmap( &bm );
//贴图
//pDC->BitBlt(x, y,
// x + bm.bmWidth,y + bm.bmHeight,
// &MemDc,
// 0, 0,
// SRCCOPY);
//拉伸
pDC->StretchBlt(x,y, nW, nH ,&memDc,0,0,
bm.bmWidth,bm.bmHeight,SRCCOPY);
memDc.SelectObject(OldBmp);
memDc.DeleteDC();
}
Button的属性需要设置为:样式-->所有者绘制,
但是感觉效果不理想,请大家帮忙提下改善建议//////////////////////////////////////////////////////
// skinDlg.h : header file
class CSkinDlg : public CDialog
{
protected:
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);public:
CBrush m_MeaningBrush; //背景颜色画刷
void DrawTitleBar(CDC *pDC);
void OnNcPaint();
BOOL OnEraseBkgnd(CDC* pDC);
void ShowBitmap(CDC *pDC, int x, int y, int nW, int nH, CBitmap &m_bitmap); afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
}//////////////////////////////////////////////////////
// skinDlg.cpp : implementation file
CSkinDlg::CSkinDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSkinDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//创建为红色背景
m_MeaningBrush.CreateSolidBrush(RGB(255,0,0));
}BEGIN_MESSAGE_MAP(CSkinDlg, CDialog)
//{{AFX_MSG_MAP(CSkinDlg)
ON_WM_PAINT()
ON_WM_DRAWITEM()
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()//修改窗体背景颜色
void CSkinDlg::OnPaint()
{
if (IsIconic())
{ }
else
{
CRect rect;
CPaintDC dc(this);
GetClientRect(rect);
//设置为白色背景
dc.FillSolidRect(rect,RGB(255,255,255));
CDialog::OnPaint();
}
}//修改Button背景色和字体颜色
//Button的属性需要设置为:样式-->所有者绘制
void CSkinDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if(nIDCtl==IDC_BUTTON1) //checking for the button
{
//Button属性为自画
CDC dc;
RECT rect;
dc.Attach(lpDrawItemStruct ->hDC); // Get the Button DC to CDC
rect = lpDrawItemStruct->rcItem; //Store the Button rect to our local rect.
dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));
dc.FillSolidRect(&rect,RGB(100,100,255));//Here you can define the required color to appear on the Button.
UINT state=lpDrawItemStruct->itemState; //This defines the state of the Push button either pressed or not.
if((state & ODS_SELECTED))
{
dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);
}
else
{
dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
}
dc.SetBkColor(RGB(100,100,255)); //Setting the Text Background color
dc.SetTextColor(RGB(255,0,0)); //Setting the Text Color
TCHAR buffer[MAX_PATH]; //To store the Caption of the button.
ZeroMemory(buffer,MAX_PATH ); //Intializing the buffer to zero
::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH); //Get the Caption of Button Window
dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);//Redraw the Caption of Button Window
dc.Detach(); // Detach the Button DC
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}//修改文本框背景色和字体颜色
HBRUSH CSkinDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_EDIT1) //编辑框的名称ID
{
pDC->SetTextColor(RGB(255,255,255));//编辑框类的字体设置为白色;
pDC->SetBkMode(TRANSPARENT);//设置为透明不影响字体颜色
return (HBRUSH)m_MeaningBrush.GetSafeHandle();//返回当前编辑框的背景画刷
}
if (pWnd->GetDlgCtrlID() ==IDC_STATIC1) //编辑框的名称ID
{
pDC->SetTextColor(RGB(255,255,255));//编辑框类的字体设置为白色;
pDC->SetBkMode(TRANSPARENT);//设置为透明不影响字体颜色
return (HBRUSH)m_MeaningBrush.GetSafeHandle();//返回当前编辑框的背景画刷
}
// TODO: Return a different brush if the default is not desired
return hbr;
}//自绘窗体的标题
void CSkinDlg::DrawTitleBar(CDC *pDC)
{
CDC memDC;
CBitmap bmp;
CRect rect, rtTitle;
CPoint point;
GetClientRect(rect);
//获取标题栏大小
rtTitle.left = GetSystemMetrics(SM_CXFRAME);
rtTitle.top = GetSystemMetrics(SM_CYFRAME);
rtTitle.right = rect.right - rect.left - GetSystemMetrics(SM_CXFRAME);
rtTitle.bottom = rtTitle.top + GetSystemMetrics(SM_CYSIZE);
point.x = rect.right + GetSystemMetrics(SM_CXFRAME);
point.y = 30;
memDC.CreateCompatibleDC(NULL);
bmp.CreateCompatibleBitmap(pDC,point.x,point.y);
CBitmap *pOldBit=memDC.SelectObject(&bmp);
//背景颜色
memDC.FillSolidRect(0,0,point.x,point.y,RGB(125,0,125));
////贴图
//CBitmap pic;
//pic.LoadBitmap(IDB_BITMAP1);
//ShowBitmap(&memDC,0,0, rtTitle.right, 30, pic);
pDC->BitBlt(0,0,point.x,point.y,&memDC,0,0,SRCCOPY);
pOldBit->DeleteObject();
memDC.DeleteDC();
bmp.DeleteObject();
}//回调函数
LRESULT CSkinDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
//return CDialog::DefWindowProc(message, wParam, lParam);
LRESULT lrst=CDialog::DefWindowProc(message, wParam, lParam);
if (message==WM_MOVE||message==WM_PAINT||message==WM_NCPAINT||message==WM_NCACTIVATE ||message == WM_NOTIFY)
{
CDC* pWinDC = GetWindowDC();
if (pWinDC)
DrawTitleBar(pWinDC);
ReleaseDC(pWinDC);
}
//return lrst;
return CDialog::DefWindowProc(message, wParam, lParam);
}void CSkinDlg::OnNcPaint()
{
//CDialog::OnNcPaint();
}BOOL CSkinDlg::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}void CSkinDlg::ShowBitmap(CDC *pDC, int x, int y, int nW, int nH, CBitmap &m_bitmap)
{
CDC memDc, mdc;
memDc.CreateCompatibleDC(NULL);
mdc.CreateCompatibleDC(NULL);
CBitmap memBitmap;
memBitmap.CreateCompatibleBitmap(pDC,nW,nH);
CBitmap *OldBmp = memDc.SelectObject(&m_bitmap);
mdc.SelectObject(&m_bitmap);
memDc.BitBlt(x,y,nW,nH,&mdc,0,0,SRCCOPY);
BITMAP bm;
m_bitmap.GetBitmap( &bm );
//贴图
//pDC->BitBlt(x, y,
// x + bm.bmWidth,y + bm.bmHeight,
// &MemDc,
// 0, 0,
// SRCCOPY);
//拉伸
pDC->StretchBlt(x,y, nW, nH ,&memDc,0,0,
bm.bmWidth,bm.bmHeight,SRCCOPY);
memDc.SelectObject(OldBmp);
memDc.DeleteDC();
}
只修改下背景和前景颜色就可以了么?还需要处理别的细节问题么?,比如重绘后的图片拖动,防止程序死掉等问题