www.pudn.com > heart.zip > HeartMouseDlg.cpp, change:1999-11-11,size:9081b


// HeartMouseDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "HeartMouse.h" 
#include "HeartMouseDlg.h" 
 
#include "../heart/heart.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
#define MYTIMEID	WM_USER+100 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CHeartMouseDlg dialog 
 
CHeartMouseDlg::CHeartMouseDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CHeartMouseDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CHeartMouseDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	for(int i = 0; i < HEARTNUM; i++) ha[i] = NULL; 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CHeartMouseDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CHeartMouseDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CHeartMouseDlg, CDialog) 
	//{{AFX_MSG_MAP(CHeartMouseDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_DESTROY() 
	ON_WM_TIMER() 
	ON_MESSAGE(WM_MYMOVE, OnMyMove) 
	ON_MESSAGE(WM_MY_NOTIFY, OnMyNotify) 
	ON_BN_CLICKED(IDC_HIDE, OnHide) 
	ON_COMMAND(IDM_SHOW, OnShow) 
	ON_BN_CLICKED(IDC_LOAD, OnLoad) 
	ON_COMMAND(IDM_EXIT, OnOK) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CHeartMouseDlg message handlers 
 
BOOL CHeartMouseDlg::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 
	for(int i = 0; i < HEARTNUM; i ++) ha[i] =NULL; 
	m_hIconbar = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_HEART)); 
	m_hBitmap = NULL; 
	Start(); 
	// TODO: Add extra initialization here 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CHeartMouseDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else if (nID == SC_MINIMIZE) { 
		ShowWindow(SW_HIDE); 
	} 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 CHeartMouseDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		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 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CHeartMouseDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CHeartMouseDlg::Start()  
{ 
	// TODO: Add your control notification handler code here 
	NOTIFYICONDATA nid; 
	nid.cbSize = sizeof(nid); 
	nid.hWnd = m_hWnd; 
	nid.uID = IDI_HEART; 
	nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP ; 
	nid.uCallbackMessage =	WM_MY_NOTIFY; 
	nid.hIcon = m_hIconbar; 
	strcpy(nid.szTip, "Heart Mouse"); 
	Shell_NotifyIcon(NIM_ADD, &nid); 
 
	SetTimer(1, 180, NULL); 
	for (int i = 0; i < POINTNUM; i++) points[i].x = points[i].y = 0; 
	head = tail = 0; 
	total = 0; 
	if (ha[0]) return; 
	for (i = HEARTNUM - 1; i >=0; i--) { 
		CRect r; 
		r.top = r.left = 0; 
		r.right = (i+2) * 5; 
		r.bottom = int((i+2) * 4.5); 
		if (ha[i]) return; 
		ha[i] = new HeartWindow(); 
		ha[i]->Create(r, m_hBitmap); 
	} 
	extern void SetHook(); 
	SetHook(); 
	extern void SetWindowsHandle(HWND); 
	SetWindowsHandle(m_hWnd); 
	OnHide(); 
} 
 
void CHeartMouseDlg::Stop()  
{ 
	// TODO: Add your control notification handler code here 
	KillTimer(1); 
	extern void UnHook(); 
	UnHook(); 
	for(int i = 0; i < HEARTNUM; i ++) { 
		if (ha[i]) 
		{ 
			delete ha[i]; 
			ha[i] = NULL; 
		} 
	} 
	NOTIFYICONDATA nid; 
	nid.cbSize = sizeof(nid); 
	nid.hWnd = m_hWnd; 
	nid.uID = IDI_HEART; 
	nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP ; 
	nid.uCallbackMessage =	WM_MY_NOTIFY; 
	nid.hIcon = m_hIconbar; 
	strcpy(nid.szTip, "Heart Mouse"); 
	Shell_NotifyIcon(NIM_DELETE, &nid); 
	if(m_hBitmap) ::DeleteObject(m_hBitmap); 
	m_hBitmap = NULL; 
} 
 
void CHeartMouseDlg::OnDestroy()  
{ 
	CDialog::OnDestroy(); 
	 
	// TODO: Add your message handler code here 
} 
 
void CHeartMouseDlg::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if (nIDEvent != 1) return; 
	total = head - tail; 
	if (total == 0) return; 
	if (total < 0) total += POINTNUM; 
	int mi = max(total/(HEARTNUM + 2),1); 
	int movepos = POINTNUM; 
	for ( int i = 0; i < HEARTNUM; i++) { 
		int ind = head - (total - mi) * (i + 1) / (HEARTNUM + 1) ; 
		while (ind < 0 ) ind += POINTNUM; 
		if (head > tail) { 
			if (ind < tail) ind = tail + 1; 
			if (ind > head) ind = head; 
			if (movepos > ind - tail) movepos = ind - tail; 
		} else { 
			if (ind < tail && ind > head) ind = head; 
			int j = ind - tail; 
			if ( j < 0) j += POINTNUM; 
			if (movepos > j) movepos = j; 
		} 
		int px = points[ind].x; 
		int py = points[ind].y; 
		ha[i]->SetWindowPos(NULL,px +5 + HEARTNUM - i , py + 5 + HEARTNUM - i, 0, 0, SWP_NOSIZE|SWP_NOZORDER|SWP_SHOWWINDOW|SWP_NOACTIVATE); 
		if ( i == HEARTNUM -1) tail = ind; 
	} 
	CDialog::OnTimer(nIDEvent); 
} 
void CHeartMouseDlg::OnMyMove(WPARAM w, LPARAM l) 
{ 
	head ++; 
	head = head % POINTNUM; 
	if (head == tail) { 
		tail ++; 
		tail = tail % POINTNUM; 
	} 
	points[head].x = w; 
	points[head].y = l; 
} 
 
void CHeartMouseDlg::OnOK()  
{ 
	// TODO: Add extra validation here 
	Stop(); 
	CDialog::OnOK(); 
} 
 
void CHeartMouseDlg::OnMyNotify(WPARAM w, LPARAM l) 
{ 
	switch (l) { 
	case WM_LBUTTONDBLCLK: 
		ShowWindow(SW_SHOW); 
		break; 
	case WM_RBUTTONDOWN: 
		{ 
			CMenu MyMenu; 
			MyMenu.LoadMenu(IDR_MENU1); 
			POINT curPos; 
			GetCursorPos(& curPos); 
			MyMenu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON, curPos.x, curPos.y, this); 
			break; 
		} 
	default: 
		return; 
	} 
} 
 
void CHeartMouseDlg::OnHide()  
{ 
	// TODO: Add your control notification handler code here 
	ShowWindow(SW_HIDE);	 
} 
 
void CHeartMouseDlg::OnShow()  
{ 
	// TODO: Add your control notification handler code here 
	ShowWindow(SW_SHOW);	 
} 
 
void CHeartMouseDlg::OnLoad()  
{ 
	// TODO: Add your control notification handler code here 
	CFileDialog f(TRUE, "bmp", "*.bmp", OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_PATHMUSTEXIST , 
		NULL, this); 
	f.DoModal(); 
	CString s = f.GetFileName(); 
	for(int i = 0; i < HEARTNUM; i ++) { 
		if (ha[i]) 
		{ 
			delete ha[i]; 
			ha[i] = NULL; 
		} 
	} 
	m_hBitmap = (HBITMAP)::LoadImage(NULL, s, IMAGE_BITMAP, 0,0, LR_DEFAULTSIZE | LR_LOADFROMFILE | LR_CREATEDIBSECTION); 
	for (i = HEARTNUM - 1; i >=0; i--) { 
		CRect r; 
		r.top = r.left = 0; 
		r.right = (i+2) * 5; 
		r.bottom = int((i+2) * 4.5); 
		if (ha[i]) return; 
		ha[i] = new HeartWindow(); 
		ha[i]->Create(r, m_hBitmap); 
	} 
 
} 
 
void CHeartMouseDlg::OnCancel()  
{ 
	// TODO: Add extra cleanup here 
	Stop(); 
	CDialog::OnCancel(); 
}