终于强到了沙发///////////////////////////////////////////////////////////////////////////// // CDragPicDlg dialogCDragPicDlg::CDragPicDlg(CWnd* pParent /*=NULL*/) : CDialog(CDragPicDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDragPicDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); }void CDragPicDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDragPicDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CDragPicDlg, CDialog) //{{AFX_MSG_MAP(CDragPicDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_CREATE() ON_WM_SIZE() ON_WM_LBUTTONDOWN() ON_WM_ERASEBKGND() //}}AFX_MSG_MAP END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CDragPicDlg message handlersBOOL CDragPicDlg::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 SetWindowPos(NULL,0,0,bm.bmWidth,bm.bmHeight,0); return TRUE; // return TRUE unless you set the focus to a control }void CDragPicDlg::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 CDragPicDlg::OnPaint() { CPaintDC dc(this); // device context for painting //dc.SetBkMode(TRANSPARENT); if (IsIconic()) { 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); } else { CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap m_bkbitmap; m_bkbitmap.CreateCompatibleBitmap(&dc,rc.right,rc.bottom); memDC.SelectObject(&m_bkbitmap); CBrush m_FillmemBrush; m_FillmemBrush.CreateSolidBrush(RGB(255,0,255)); CRect rc; GetClientRect(&rc); memDC.FillRect(&rc,&m_FillmemBrush); memDC.SetBkColor(RGB(255,0,255)); DrawRangeImage(&memDC,rc);
//dc.BitBlt(0,0,rc.right,rc.bottom,&memDC,0,0,SRCCOPY); TransparentBlt(&dc,0,0,rc.Width(),rc.Height(),&memDC,0,0,RGB(255,0,255)); m_FillmemBrush.DeleteObject(); m_bkbitmap.DeleteObject(); memDC.DeleteDC(); ReleaseDC(&dc); CDialog::OnPaint(); } }// The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDragPicDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } BOOL CDragPicDlg::LoadBitmap(LPCTSTR pFilename) { HBITMAP pBitmap; pBitmap = (HBITMAP)::LoadImage(::AfxGetInstanceHandle(),pFilename,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE); if(pBitmap) { m_Bitmap.Attach(pBitmap); return TRUE; } else // throw CException("read image file error"); return FALSE; }int CDragPicDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDialog::OnCreate(lpCreateStruct) == -1) return -1;
// CDragPicDlg dialogCDragPicDlg::CDragPicDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDragPicDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDragPicDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CDragPicDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDragPicDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CDragPicDlg, CDialog)
//{{AFX_MSG_MAP(CDragPicDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_LBUTTONDOWN()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CDragPicDlg message handlersBOOL CDragPicDlg::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
SetWindowPos(NULL,0,0,bm.bmWidth,bm.bmHeight,0);
return TRUE; // return TRUE unless you set the focus to a control
}void CDragPicDlg::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 CDragPicDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
//dc.SetBkMode(TRANSPARENT);
if (IsIconic())
{
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);
}
else
{
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap m_bkbitmap;
m_bkbitmap.CreateCompatibleBitmap(&dc,rc.right,rc.bottom);
memDC.SelectObject(&m_bkbitmap);
CBrush m_FillmemBrush;
m_FillmemBrush.CreateSolidBrush(RGB(255,0,255));
CRect rc;
GetClientRect(&rc);
memDC.FillRect(&rc,&m_FillmemBrush);
memDC.SetBkColor(RGB(255,0,255));
DrawRangeImage(&memDC,rc);
//dc.BitBlt(0,0,rc.right,rc.bottom,&memDC,0,0,SRCCOPY);
TransparentBlt(&dc,0,0,rc.Width(),rc.Height(),&memDC,0,0,RGB(255,0,255));
m_FillmemBrush.DeleteObject();
m_bkbitmap.DeleteObject();
memDC.DeleteDC();
ReleaseDC(&dc);
CDialog::OnPaint();
}
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDragPicDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CDragPicDlg::LoadBitmap(LPCTSTR pFilename)
{
HBITMAP pBitmap; pBitmap = (HBITMAP)::LoadImage(::AfxGetInstanceHandle(),pFilename,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
if(pBitmap)
{
m_Bitmap.Attach(pBitmap);
return TRUE;
}
else
// throw CException("read image file error");
return FALSE;
}int CDragPicDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
if(!LoadBitmap("res\\Background.bmp"))
{
TRACE("load image failed!\n");
return -1;
}
m_Bitmap.GetObject(sizeof(BITMAP),&bm);
rc.left = 0;
rc.top = 0;
rc.right = bm.bmWidth;
rc.bottom = bm.bmHeight;
return 0;
}void CDragPicDlg::DrawRangeImage(CDC *pDC, CRect rc,BOOL bTransparent,COLORREF MaskColour)
{
COLORREF crOldBack = RGB(255, 255, 255); //White
COLORREF crOldText = RGB(0, 0, 0); // Black
CDC dcImage, dcTrans;
int nWidth = bm.bmWidth;
int nHeight = bm.bmHeight;
dcImage.CreateCompatibleDC(pDC);
dcTrans.CreateCompatibleDC(pDC); CBitmap* pOldBitmapImage = dcImage.SelectObject(&m_Bitmap);
CBitmap bitmapTrans;
bitmapTrans.CreateBitmap(nWidth, nHeight, 1, 1, NULL);
CBitmap* pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans); dcImage.SetBkColor(MaskColour); int x=rc.left;
int y=rc.top; while (y < (rc.Height()+rc.top))
{
while(x < (rc.Width()+rc.left))
{
dcTrans.BitBlt(0, 0, nWidth, nHeight, &dcImage, 0, 0, SRCCOPY);
pDC->BitBlt(x, y, nWidth, nHeight, &dcImage, 0, 0, SRCINVERT);
pDC->BitBlt(x, y, nWidth, nHeight, &dcTrans, 0, 0, SRCAND);
pDC->BitBlt(x, y, nWidth, nHeight, &dcImage, 0, 0, SRCINVERT);
x += nWidth;
}
x = rc.left;
y += nHeight;
} dcImage.SelectObject(pOldBitmapImage);
dcTrans.SelectObject(pOldBitmapTrans);
pDC->SetBkColor(crOldBack);
pDC->SetTextColor(crOldText);
}void CDragPicDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GetClientRect(&rc);
}void CDragPicDlg::TransparentBlt(CDC *destDC,int x,int y,int iWidth,int iHeight,CDC *srcDC,int xSrc, int ySrc,UINT transparentCol)
{
COLORREF crOldBack = RGB(255, 255, 255); //White
COLORREF crOldText = RGB(0, 0, 0); // Black
CDC dcTrans;
dcTrans.CreateCompatibleDC(destDC);
CBitmap bitmapTrans;
bitmapTrans.CreateBitmap(iWidth, iHeight, 1, 1, NULL);
CBitmap* pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans);
srcDC->SetBkColor(transparentCol); dcTrans.BitBlt(x, y, iWidth, iHeight, srcDC, xSrc, ySrc, SRCCOPY);
destDC->BitBlt(x, y, iWidth, iHeight, srcDC, xSrc, ySrc, SRCINVERT);
destDC->BitBlt(x, y, iWidth, iHeight, &dcTrans, xSrc, ySrc, SRCAND);
destDC->BitBlt(x, y, iWidth, iHeight, srcDC, xSrc, ySrc, SRCINVERT); dcTrans.SelectObject(pOldBitmapTrans);
destDC->SetBkColor(crOldBack);
destDC->SetTextColor(crOldText);
dcTrans.DeleteDC();
bitmapTrans.DeleteObject();
}void CDragPicDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x, point.y));
CDialog::OnLButtonDown(nFlags, point);
}BOOL CDragPicDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
return CDialog::OnEraseBkgnd(pDC);
}
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); return hbr;
}
方法一:调用CWinApp类的成员函数SetDialogBkColor来实现。
---- 其中函数的第一个参数指定了背景颜色,第二个参数指定了文本颜色。下面的例子是将应用程序对话框设置为蓝色背景和红色文本,步骤如下:
---- ① 新建一个基于Dialog的MFC AppWizard应用程序ExampleDlg。
---- ② 在CExampleDlgApp ::InitInstance()中添加如下代码:
BOOL CExampleDlgApp: : InitInstance ( )
{
… CExampleDlgDlg dlg;
m_pMainWnd = &dlg;
//先于DoModal()调用,将对话框设置为蓝色背景、红色文本
SetDialogBkColor(RGB(0,0,255),RGB(255,0,0));
int nResponse = dlg.DoModal();
…}
---- 编译并运行,此时对话框的背景色和文本色已发生了改变。值得注意的是:在调用DoModal()之前必须先调用SetDialogBkColor,且此方法是将改变应用程序中所有的对话框颜色,并不能针对某一个指定的对话框。