// DialogTestMFCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DialogTestMFC.h"
#include "DialogTestMFCDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
LOGBRUSH logBrush ;
CBrush brush;
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CDialogTestMFCDlg dialog
CDialogTestMFCDlg::CDialogTestMFCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDialogTestMFCDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDialogTestMFCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CDialogTestMFCDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDCANCEL, &CDialogTestMFCDlg::OnBnClickedCancel)
ON_BN_CLICKED(IDC_CHANGE, &CDialogTestMFCDlg::OnBnClickedChange)
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
// CDialogTestMFCDlg message handlers
BOOL CDialogTestMFCDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
logBrush.lbStyle = BS_SOLID;
logBrush.lbHatch = HS_CROSS;
logBrush.lbColor = RGB(0, 255, 0);
// ... and initialize it with the LOGBRUSH.
brush.CreateBrushIndirect(&logBrush);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDialogTestMFCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CDialogTestMFCDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDialogTestMFCDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CDialogTestMFCDlg::OnBnClickedCancel()
{
// TODO: Add your control notification handler code here
OnCancel();
}
void CDialogTestMFCDlg::OnBnClickedChange()
{
// TODO: Add your control notification handler code here
logBrush.lbStyle = BS_SOLID;
logBrush.lbHatch = HS_CROSS;
logBrush.lbColor = RGB(255, 0, 0);
brush.DeleteObject();
brush.CreateBrushIndirect(&logBrush);
Invalidate(TRUE);
}
BOOL CDialogTestMFCDlg::OnEraseBkgnd(CDC* pDC)
{
CDialog::OnEraseBkgnd(pDC);
RECT rt;
GetClientRect(&rt);
pDC->FillRect(&rt,&brush);
return 1;
}
HBRUSH CDialogTestMFCDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
pDC->SetBkMode(TRANSPARENT);
hbr = (HBRUSH)brush.GetSafeHandle();
::OutputDebugStringW(_T("here are OnCtlColor/n"));
return hbr;
}
上面是本人实验的代码,本想通过OnEraseBkgnd和OnCtlColor来解决Trackbar Controls的背景实时改变问题,可是发现并不是如此简单,具体实现方法还在进一步思考。当然也有可能是common control实现上故意不让背景实时改变。
本人建立了基于对话框的MFC程序,然后在对话框中加入Trackbar Controls控件,再加入一个按钮,此按钮的作用是实时改变对话框的背景。此对话框上有static控件一个,按钮一个,Trackbar Controls控件一个。通过修改CDialogTestMFCDlg文件,即添加了全局的brush,还有几个消息响应函数。结果是启动对话框程序,对话框的背景在OnEraseBkgnd中实现,static控件和Trackbar Control控件的背景通过OnCtlColor实现,本人是一步一步调试得到。但是当点击按钮后,本想static控件和Trackbar Control控件的背景色会跟着改变(因为我在OnBnClickedChange函数中调用了Invalidate(TRUE);),可是只有static控件的背景改变了(在OnCtlColor中实现),而Trackbar Control控件执行了OnCtlColor,但其背景没有改变。当我将鼠标在Trackbar Control控件上点击一下的时候,其背景立马改变,变为和对话框还有static控件背景一样的颜色。我很奇怪??为什么要点击一下才改变背景(提醒一下:当我点击Trackbar Control控件时,此控件上的thumb没有改变位置)。
本人在调试过程中,将代码编辑界面缩的很小,为了能让我看到断点和执行程序的交互,本人在程序添加的消息响应函数中都添加了断点,上述结果是一步一步调试得到的。
如果有高人看到此文章,请多多指教。