www.pudn.com > GGBT.rar > DlgAfxMessage.cpp
// DlgAfxMessage.cpp : implementation file
//
#include "stdafx.h"
#include "testbt.h"
#include "DlgAfxMessage.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int AfxMessageBoxEx(LPCTSTR lpszText, UINT nType)
{
CDlgAfxMessage dlg(lpszText, nType);
return dlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CDlgAfxMessage dialog
CDlgAfxMessage::CDlgAfxMessage(LPCTSTR lpszText, UINT nType, CWnd* pParent /*=NULL*/)
: CDialog(CDlgAfxMessage::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgAfxMessage)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_strText = lpszText;
m_nType = nType;
m_cyIcon = m_cxIcon = 32;
m_hIcon = 0;
}
void CDlgAfxMessage::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgAfxMessage)
DDX_Control(pDX, IDNO, m_btnNo);
DDX_Control(pDX, IDCANCEL, m_btnCancel);
DDX_Control(pDX, IDOK, m_btnOK);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgAfxMessage, CDialog)
//{{AFX_MSG_MAP(CDlgAfxMessage)
ON_WM_PAINT()
ON_BN_CLICKED(IDNO, OnNo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgAfxMessage message handlers
BOOL CDlgAfxMessage::OnInitDialog()
{
CDialog::OnInitDialog();
m_font.CreateFont(-13, 0, 0, 0, 400, 0, 0, 0, 134, 0, 0, 0, 2, "宋体");
//
// Create BitmapList
//
HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP_DLG_ICONS),
IMAGE_BITMAP,
0,0, // cx,cy
LR_CREATEDIBSECTION); // | LR_LOADMAP3DCOLORS );
CBitmap bm;
bm.Attach(hbm);
m_ctlImage.Create(16,15, ILC_COLOR8|ILC_MASK, 3, 4);
m_ctlImage.SetBkColor(RGB(255, 255,255));
m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
//
// set ok cancel button image.
//
// m_btnBrowseDir.Create(m_ctlImage.ExtractIcon(2), true, "选择保存路径");
m_btnOK.Create(m_ctlImage.ExtractIcon(0));
m_btnCancel.Create(m_ctlImage.ExtractIcon(1));
m_btnNo.Create(m_ctlImage.ExtractIcon(1));
//
// Set dialog text
//
SetWindowText(AfxGetApp()->m_pszAppName);
//
// compute the size
//
int iButtonWidth = 100, iButtonHeight = 30;
int iImageWidth = m_cyIcon + 20;
int iEdge = 10;
int iWidth = 200, iHeight = 0;
const int iCaptionHeight = ::GetSystemMetrics(SM_CYCAPTION);
if ((m_nType&0xf) == MB_OK)
{
m_hIcon = AfxGetApp()->LoadStandardIcon(IDI_EXCLAMATION);
iWidth = iButtonWidth + iEdge * 10;
}
else if ((m_nType&0xf) == MB_OKCANCEL)
{
m_hIcon = AfxGetApp()->LoadStandardIcon(IDI_QUESTION);
iWidth = iButtonWidth * 2 + iEdge * 12;
}
else if ((m_nType&0xf) == MB_YESNO)
{
m_hIcon = AfxGetApp()->LoadStandardIcon(IDI_QUESTION);
iWidth = iButtonWidth * 2 + iEdge * 12;
}
else if ((m_nType&0xf) == MB_YESNOCANCEL)
{
m_hIcon = AfxGetApp()->LoadStandardIcon(IDI_QUESTION);
iWidth = iButtonWidth * 3 + iEdge * 13;
}
else
{
ASSERT(FALSE);
return FALSE;
}
//
// compute the image rect.
//
m_rcImage.top = iEdge;
m_rcImage.bottom = m_rcImage.top + m_cyIcon;
m_rcImage.left = iEdge;
m_rcImage.right = m_rcImage.left + m_cxIcon;
//
// compute the text rect.
//
CClientDC dc(this);
// select font.
CFont* poldFont = dc.SelectObject(&m_font);
int iTextWidth = 0;
CString strTemp = m_strText;
CSize sizeTextExtent = CSize(0, 0);
while (!strTemp.IsEmpty())
{
int iRet = strTemp.Find("\r\n");
if (iRet == -1)
{
sizeTextExtent = dc.GetTextExtent(strTemp);
if (iTextWidth < sizeTextExtent.cx)
iTextWidth = sizeTextExtent.cx;
break;
}
CString strLeft = strTemp.Left(iRet);
strTemp = strTemp.Right(strTemp.GetLength() - iRet - 2);
sizeTextExtent = dc.GetTextExtent(strLeft);
if (iTextWidth < sizeTextExtent.cx)
iTextWidth = sizeTextExtent.cx;
}
if (iTextWidth > (iWidth - iImageWidth - iEdge * 3))
iWidth = iTextWidth + iImageWidth + iEdge * 3;
ASSERT(iWidth > iTextWidth);
m_rcText.left = iImageWidth + iEdge * 2;
m_rcText.top = iEdge * 2;
m_rcText.right = m_rcText.left + iTextWidth;
m_rcText.bottom = m_rcText.top + sizeTextExtent.cy;
dc.DrawText(m_strText, m_rcText, DT_LEFT|DT_VCENTER|DT_CALCRECT);
// realease font.
dc.SelectObject(poldFont);
//
// compute button rect.
//
CRect rcButton;
rcButton.top = m_rcText.bottom + 20;
rcButton.bottom = rcButton.top + iButtonHeight;
if ((m_nType&0xf) == MB_OK)
{
rcButton.left = (iWidth - iButtonWidth) / 2 ;
rcButton.right = rcButton.left + iButtonWidth;
GetDlgItem(IDOK)->MoveWindow(rcButton);
GetDlgItem(IDCANCEL)->ShowWindow(false);
GetDlgItem(IDNO)->ShowWindow(false);
}
else if ((m_nType&0xf) == MB_YESNO)
{
rcButton.left = iWidth / 2 - iButtonWidth - iEdge;
rcButton.right = rcButton.left + iButtonWidth;
GetDlgItem(IDOK)->MoveWindow(rcButton);
GetDlgItem(IDOK)->SetWindowText("是");
rcButton.OffsetRect(iButtonWidth + iEdge*2, 0);
GetDlgItem(IDCANCEL)->MoveWindow(rcButton);
GetDlgItem(IDCANCEL)->SetWindowText("否");
GetDlgItem(IDNO)->ShowWindow(false);
}
else if ((m_nType&0xf) == MB_OKCANCEL)
{
rcButton.left = iWidth / 2 - iButtonWidth - iEdge;
rcButton.right = rcButton.left + iButtonWidth;
GetDlgItem(IDOK)->MoveWindow(rcButton);
rcButton.OffsetRect(iButtonWidth + iEdge*2, 0);
GetDlgItem(IDCANCEL)->MoveWindow(rcButton);
GetDlgItem(IDNO)->ShowWindow(false);
}
else if ((m_nType&0xf) == MB_YESNOCANCEL)
{
rcButton.left = iWidth / 2 - iButtonWidth - iEdge * 2 - iButtonWidth/2;
rcButton.right = rcButton.left + iButtonWidth;
GetDlgItem(IDOK)->MoveWindow(rcButton);
GetDlgItem(IDOK)->SetWindowText("是(&Y)");
rcButton.OffsetRect(iButtonWidth + iEdge*2, 0);
GetDlgItem(IDCANCEL)->MoveWindow(rcButton);
GetDlgItem(IDCANCEL)->SetWindowText("否(&N)");
rcButton.OffsetRect(iButtonWidth + iEdge*2, 0);
GetDlgItem(IDNO)->MoveWindow(rcButton);
GetDlgItem(IDNO)->SetWindowText("取消");
}
else
{
ASSERT(FALSE);
}
iHeight = rcButton.bottom + 20 + iCaptionHeight;
//
// position the window pos.
//
CPoint pt(0, 0);
if (AfxGetMainWnd())
{
CRect rc;
AfxGetMainWnd()->GetWindowRect(rc);
pt.x = rc.left + rc.Width()/2 - iWidth/2;
pt.y = rc.top + rc.Height()/2 - iHeight/2;
}
MoveWindow(pt.x, pt.y, iWidth, iHeight);
if (m_nType & MB_DEFBUTTON2 )
{
GetDlgItem(IDCANCEL)->SetFocus();
}
else
{
GetDlgItem(IDOK)->SetFocus();
}
return FALSE;
}
void CDlgAfxMessage::OnOK()
{
if (!UpdateData(TRUE))
{
TRACE0("UpdateData failed during dialog termination.\n");
// the UpdateData routine will set focus to correct item
return;
}
if ((m_nType&0xf) == MB_YESNO)
{
EndDialog(IDYES);
}
else if ((m_nType&0xf) == MB_YESNOCANCEL)
{
EndDialog(IDYES);
}
else
{
ASSERT(((m_nType&0xf) == MB_OK) || ((m_nType&0xf) == MB_OKCANCEL) );
EndDialog(IDOK);
}
}
void CDlgAfxMessage::OnCancel()
{
if ((m_nType&0xf) == MB_YESNO)
{
EndDialog(IDNO);
}
else if ((m_nType&0xf) == MB_YESNOCANCEL)
{
EndDialog(IDNO);
}
else
{
ASSERT((m_nType&0xf) == MB_OKCANCEL);
EndDialog(IDCANCEL);
}
}
void CDlgAfxMessage::OnNo()
{
ASSERT((m_nType&0xf) == MB_YESNOCANCEL);
EndDialog(IDCANCEL);
}
void CDlgAfxMessage::OnPaint()
{
if (!m_hIcon) return;
CPaintDC dc(this); // device context for painting
CFont* poldFont = dc.SelectObject(&m_font);
dc.SetBkMode(TRANSPARENT);
int iRet = dc.DrawText(m_strText, m_rcText, DT_LEFT | DT_VCENTER);
dc.SelectObject(poldFont);
DrawTheIcon(&dc, m_rcImage);
}
void CDlgAfxMessage::DrawTheIcon(CDC* pDC, RECT* rcItem)
{
if (!m_hIcon)
{
ASSERT(FALSE);
return;
}
CRect iconRect = rcItem;
iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
// Center the icon vertically
iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
iconRect.right = iconRect.left + m_cxIcon;
iconRect.bottom += iconRect.bottom + m_cyIcon;
pDC->DrawState(iconRect.TopLeft(),
iconRect.Size(),
m_hIcon,
DSS_NORMAL,
// (IsDisabled ? DSS_DISABLED : DSS_NORMAL),
(CBrush*)NULL);
} // End of DrawTheIcon