#include "stdafx.h"
#include "resource.h"
void DrawTransparentBitmap(HDC hdc,HBITMAP hBitmap, int xStart, int yStart,
int xBitmap, int yBitmap, int xWidth, int yHeight,
COLORREF cTransparentColor)
{
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld, bmTempOld;
HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
POINT ptSize;
if(NULL == hdc || NULL == hBitmap)
return;
hdcTemp = CreateCompatibleDC(hdc);
bmTempOld = (HBITMAP)SelectObject(hdcTemp, hBitmap); // Select the bitmap
ptSize.x = xWidth; // Get width of bitmap
ptSize.y = yHeight; // Get height of bitmap
DPtoLP(hdcTemp, &ptSize, 1); // Convert from device
// to logical points
hdcBack = CreateCompatibleDC(hdc);
hdcObject = CreateCompatibleDC(hdc);
hdcMem = CreateCompatibleDC(hdc);
hdcSave = CreateCompatibleDC(hdc);
// Create a bitmap for each DC. DCs are required for a number of
// GDI functions.
// Monochrome DC
bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
// Monochrome DC
bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
// Each DC must select a bitmap object to store pixel data.
bmBackOld = (HBITMAP)SelectObject(hdcBack, bmAndBack);
bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
bmMemOld = (HBITMAP)SelectObject(hdcMem, bmAndMem);
bmSaveOld = (HBITMAP)SelectObject(hdcSave, bmSave);
// Set proper mapping mode.
SetMapMode(hdcTemp, GetMapMode(hdc));
// Save the bitmap sent here, because it will be overwritten.
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, xBitmap, yBitmap, SRCCOPY);
// Set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = SetBkColor(hdcTemp, cTransparentColor);
// Create the object mask for the bitmap by performing a BitBlt()
// from the source bitmap to a monochrome bitmap.
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, xBitmap, yBitmap,
SRCCOPY);
// Set the background color of the source DC back to the original
// color.
SetBkColor(hdcTemp, cColor);
// Create the inverse of the object mask.
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
NOTSRCCOPY);
// Copy the background of the main DC to the destination.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
SRCCOPY);
// Mask out the places where the bitmap will be placed.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
// Mask out the transparent colored pixels on the bitmap.
BitBlt(hdcTemp, xBitmap, yBitmap, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
// XOR the bitmap with the background on the destination DC.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, xBitmap, yBitmap, SRCPAINT);
// Copy the destination to the screen.
BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcTemp, xBitmap, yBitmap, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
// Delete the memory bitmaps.
SelectObject(hdcTemp, bmTempOld);
DeleteObject(SelectObject(hdcBack, bmBackOld));
DeleteObject(SelectObject(hdcObject, bmObjectOld));
DeleteObject(SelectObject(hdcMem, bmMemOld));
DeleteObject(SelectObject(hdcSave, bmSaveOld));
// Delete the memory DCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}
class CModify : public CDialog
{
DECLARE_MESSAGE_MAP()
public:
CModify(CWnd *pParent = NULL);
enum { IDD = IDD_MODIFY};
public:
virtual BOOL OnInitDialog();
virtual void OnOK();
virtual void OnCancel();
protected:
afx_msg void OnPaint();
public:
CString m_strOldPwd;
private:
CString m_strNewPwd;
CString m_strReNewPwd;
private:
CBitmap m_bitmap;
};
BEGIN_MESSAGE_MAP(CModify,CDialog)
ON_WM_PAINT()
END_MESSAGE_MAP()
CModify::CModify(CWnd *pParent) : CDialog(CModify::IDD)
{
}
BOOL CModify::OnInitDialog()
{
if(!CDialog::OnInitDialog())
{
return FALSE;
}
m_bitmap.LoadBitmap(IDB_BITMAP_MODIFY);
return TRUE;
}
void CModify::OnOK()
{
AfxMessageBox(this->m_strOldPwd);
CDialog::OnOK();
}
void CModify::OnCancel()
{
CDialog::OnCancel();
}
void CModify::OnPaint()
{
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
if(m_bitmap.GetSafeHandle())
{
DrawTransparentBitmap( dc.GetSafeHdc(), (HBITMAP)m_bitmap.GetSafeHandle(), 0, 0, 0, 0,
rect.Width(), rect.Height(), RGB(0,255,0) );
}
}
class CLogin : public CDialog
{
DECLARE_MESSAGE_MAP()
public:
CLogin(CWnd *pParent = NULL);
enum{ IDD = IDD_LOGIN };
public:
virtual BOOL OnInitDialog();
virtual void DoDataExchange(CDataExchange* pDX);
virtual void OnOK();
virtual void OnCancel();
protected:
afx_msg void OnPaint();
afx_msg HBRUSH OnCtlColor(CDC *pDC,CWnd *pWnd,UINT nCtlColor);
afx_msg void OnLButtonDown(UINT nFlags,CPoint point);
private:
CBitmap m_bitmap;
private:
CString m_strName;
CString m_strPwd;
CButton m_wndSave;
CButton m_wndAuto;
UINT nSave;
UINT nAuto;
private:
_ConnectionPtr m_pConection;
_RecordsetPtr m_pRecordset;
};
BEGIN_MESSAGE_MAP(CLogin,CDialog)
ON_WM_PAINT()
ON_WM_CTLCOLOR()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
CLogin::CLogin(CWnd *pParent) : CDialog(CLogin::IDD),m_strName(""),m_strPwd(""),nSave(0),nAuto(0)
{
}
BOOL CLogin::OnInitDialog()
{
if(!CDialog::OnInitDialog())
{
return FALSE;
}
try
{
m_pConection.CreateInstance(__uuidof(Connection));
_bstr_t strCon = "Provider=MSDASQL.1;Data Source=stock";
m_pConection->Open(strCon,"","",adModeUnknown);
}
catch(_com_error e)
{
CString strError;
strError.Format("%s",e.ErrorMessage());
MessageBox(strError,"错误提示",MB_OK);
}
m_bitmap.LoadBitmap(IDB_BITMAP_LOGIN);
return TRUE;
}
void CLogin::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX,IDC_NAME,m_strName);
DDX_Text(pDX,IDC_PWD,m_strPwd);
}
HBRUSH CLogin::OnCtlColor(CDC *pDC,CWnd *pWnd,UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
if(nCtlColor == CTLCOLOR_STATIC)
{
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkColor(RGB(255,255,255));
}
return hbr;
}
void CLogin::OnLButtonDown(UINT nFlags,CPoint point)
{
UpdateData(TRUE);
CRect rect;
CWnd *pWnd = GetDlgItem(IDC_MODIFY);
pWnd->GetWindowRect(&rect);
ScreenToClient(&rect);
if(point.x > rect.left && point.x < rect.right && point.y > rect.top && point.y < rect.bottom)
{
CModify dlg;
dlg.m_strOldPwd = this->m_strPwd;
dlg.DoModal();
this->m_strPwd = dlg.m_strOldPwd;
}
}
void CLogin::OnPaint()
{
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
if( m_bitmap.GetSafeHandle() )
{
DrawTransparentBitmap( dc.GetSafeHdc(), (HBITMAP)m_bitmap.GetSafeHandle(), 0, 0, 0, 0,
rect.Width(), rect.Height(), RGB(0,255,0) );
}
}
void CLogin::OnOK()
{
UpdateData(TRUE);
_bstr_t vName,vPwd;
try
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
_bstr_t strSql = "select * from user";
m_pRecordset->Open(strSql,(IDispatch*)m_pConection,adOpenStatic,adLockOptimistic,adCmdText);
while(!m_pRecordset->adoEOF)
{
vName = m_pRecordset->Fields->GetItem("name")->Value;
vPwd = m_pRecordset->Fields->GetItem("pwd")->Value;
if(vName == m_strName && vPwd == m_strPwd){
AfxMessageBox("success");
break;
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
catch(_com_error e)
{
CString strError;
strError.Format("%s",e.ErrorMessage());
MessageBox(strError,"错误提示",MB_OK);
m_pRecordset->Close();
}
CDialog::OnOK();
}
void CLogin::OnCancel()
{
CDialog::OnCancel();
}
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
};
CMyApp theApp;
BOOL CMyApp::InitInstance()
{
AfxOleInit();
CLogin dlg;
dlg.DoModal();
return TRUE;
}
int CMyApp::ExitInstance()
{
AfxOleTerm(FALSE);
return CWinApp::ExitInstance();
}