www.pudn.com > LastTray_Jpeg.rar > TrayDlg.cpp


// TrayDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Tray.h" 
#include "TrayDlg.h" 
#include  
#include "Capture.h" 
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
#define WM_SHOWTASK WM_USER+200 
 
using namespace std; 
 
//void Screen(char filename[],CPoint p1,CPoint p2,int tag); 
 
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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CTrayDlg dialog 
 
CTrayDlg::CTrayDlg(CWnd* pParent /*=NULL*/) 
: CDialog(CTrayDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CTrayDlg) 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_IsShow = TRUE; 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CTrayDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CTrayDlg) 
	DDX_Control(pDX, IDC_BUTTON1, m_capture); 
	DDX_Control(pDX, IDC_EDIT4, m_bottomy); 
	DDX_Control(pDX, IDC_EDIT3, m_bottomx); 
	DDX_Control(pDX, IDC_EDIT2, m_upy); 
	DDX_Control(pDX, IDC_EDIT1, m_upx); 
	DDX_Control(pDX, IDOK, m_start); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CTrayDlg, CDialog) 
//{{AFX_MSG_MAP(CTrayDlg) 
ON_WM_SYSCOMMAND() 
ON_WM_PAINT() 
ON_WM_QUERYDRAGICON() 
ON_BN_CLICKED(IDC_HIDE, OnHide) 
ON_COMMAND(ID_MENU_MAIN, OnMenuMain) 
ON_MESSAGE(WM_SHOWTASK,OnShowTask) 
ON_BN_CLICKED(IDOK, OnCapture) 
ON_COMMAND(IDR_ABOUT, OnAbout) 
ON_BN_CLICKED(IDC_RADIO2, OnFullScreen) 
ON_BN_CLICKED(IDC_RADIO1, OnPartScreen) 
ON_WM_LBUTTONDOWN() 
ON_BN_CLICKED(IDC_BUTTON1, OnDefault) 
	ON_COMMAND(ID_MENU_EXIT, OnMenuExit) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CTrayDlg message handlers 
 
BOOL CTrayDlg::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 
	point = false; 
	mhook.StartHook(); 
	bHooked=true; 
	m_nMYMESSAGE = RegisterWindowMessage("my-message"); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CTrayDlg::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 CTrayDlg::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 CTrayDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CTrayDlg::OnHide()  
{ 
	// TODO: Add your control notification handler code here 
	 
	NOTIFYICONDATA nid; 
	 
	nid.cbSize=sizeof(NOTIFYICONDATA); 
	//nid.hWnd=this->m_hWnd; 
	nid.uFlags= NIF_MESSAGE | NIF_ICON | NIF_TIP; 
	nid.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	 
	strcpy(nid.szTip,"系统吐盘实验V1.0"); 
	nid.uCallbackMessage = WM_SHOWTASK; 
	nid.uID = IDR_MAINFRAME; 
	nid.hWnd =m_hWnd; 
	 
	Shell_NotifyIcon(NIM_ADD, &nid); 
	 
	ShowWindow(SW_HIDE); 
	m_IsShow = FALSE; 
} 
 
void CTrayDlg::OnMenuMain()  
{ 
	// TODO: Add your command handler code here 
	 
	CWnd * pFrame;					 //得到当前窗体指针 
	 
	pFrame=AfxGetApp()->GetMainWnd(); 
	if(!pFrame->IsWindowVisible())	 //是否进行窗体动画,窗体已显示则不进行 
	{ 
		pFrame->ShowWindow(SW_SHOW); 
	} 
    else							 //是否进行窗体动画,窗体已隐藏则不进行 
	{ 
        pFrame->ShowWindow(SW_HIDE); 
	}	 
} 
 
void CTrayDlg::OnMenuExit()  
{ 
	// TODO: Add your command handler code here 
	 
	SendMessage(WM_CLOSE); 
	OnOK(); 
} 
 
LRESULT CTrayDlg::OnShowTask(WPARAM wParam,LPARAM lParam) 
{ 
	//wParam接收的是图标的ID,而lParam接收的是鼠标的行为 
	if(wParam!=IDR_MAINFRAME) 
	{ 
		return 1; 
	} 
	switch(lParam) 
	{ 
	case WM_RBUTTONUP: //右键起来时弹出快捷菜单 
		{ 
			LPPOINT lpPoint=new tagPOINT; 
			::GetCursorPos(lpPoint); //得到鼠标位置 
			 
			CMenu menu; 
			menu.LoadMenu(IDR_MENU); 
			menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN,lpPoint->x,lpPoint->y,this); 
			 
			//资源回收 
			//HMENU hMenu=menu.DestroyMenu 
			menu.DestroyMenu(); 
			delete lpPoint; 
		} 
		break; 
		 
	case WM_LBUTTONDBLCLK: //双击左键 
		{ 
			this->ShowWindow(SW_SHOW); 
		} 
		break; 
	} 
	return 0; 
} 
 
void CTrayDlg::OnCapture()  
{ 
	int count = 0; 
	CString temp ; 
	pWnd = GetDlgItem(IDC_BUTTON1); 
	//停止取点 
	GetDlgItem(IDOK)->EnableWindow(FALSE); 
	CCapture* pthread = new CCapture(); 
	if (pthread == NULL) 
		MessageBox("error",NULL,MB_OK); 
	if (!pthread->CreateThread(CREATE_SUSPENDED)) 
	{ 
		//	delete pthread; 
		return; 
	} 
	VERIFY(pthread->SetThreadPriority(THREAD_PRIORITY_IDLE)); 
	pthread->ResumeThread(); 
	//写入默认信息 
	temp.Format("%d",p1.x); 
	WritePrivateProfileString("Left top point X","leftX",temp,".\\Tray.ini"); 
	temp.Format("%d",p1.y); 
	WritePrivateProfileString("Left top point Y","leftY",temp,".\\Tray.ini"); 
	temp.Format("%d",p2.x); 
	WritePrivateProfileString("Right button point X","rightX",temp,".\\Tray.ini"); 
	temp.Format("%d",p2.y); 
	WritePrivateProfileString("Right button point Y","rightY",temp,".\\Tray.ini"); 
} 
 
 
 
void CTrayDlg::OnAbout()  
{ 
	// TODO: Add your command handler code here 
	CAboutDlg AboutDlg;	 
	AboutDlg.DoModal(); 
} 
 
void getPoint() 
{ 
	CDC *pDC; //屏幕DC 
	pDC=CDC::FromHandle(GetDC(NULL)); 
	p1.x = 0; 
	p1.y = 0; 
	p2.x = pDC->GetDeviceCaps(HORZRES); 
	p2.y = pDC->GetDeviceCaps(VERTRES); 
 
} 
void CTrayDlg::OnFullScreen()  
{ 
	// TODO: Add your control notification handler code here 
	tag = 1; 
	CString temp; 
	getPoint(); 
 
	m_upx.EnableWindow(FALSE); 
	temp.Format("%d",p1.x); 
	m_upx.SetWindowText(temp); 
	m_upy.EnableWindow(FALSE); 
	temp.Format("%d",p1.y); 
	m_upy.SetWindowText(temp); 
	m_bottomx.EnableWindow(FALSE); 
	temp.Format("%d",p2.x); 
	m_bottomx.SetWindowText(temp); 
	m_bottomy.EnableWindow(FALSE); 
	temp.Format("%d",p2.y); 
	m_bottomy.SetWindowText(temp); 
 
 
	 
} 
 
void CTrayDlg::OnPartScreen()  
{ 
	// TODO: Add your control notification handler code here 
	tag = 0; 
	m_upx.EnableWindow(TRUE); 
	m_upx.SetWindowText(NULL); 
	m_upy.EnableWindow(TRUE); 
	m_upy.SetWindowText(NULL); 
	m_bottomx.EnableWindow(TRUE); 
	m_bottomx.SetWindowText(NULL); 
	m_bottomy.EnableWindow(TRUE); 
	m_bottomy.SetWindowText(NULL); 
	MessageBox("请选择屏幕坐标",NULL,MB_OK); 
} 
 
LRESULT CTrayDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	CString s; 
	if (message==m_nMYMESSAGE) 
	{ 
		if (wParam)	//定义右击信息 
		{ 
			point = !point; 
			CPoint pNow = (CPoint)lParam; 
			if(point) 
			{ 
				p1.x = pNow.x;  
				p1.y = pNow.y;  
				s.Format("%d",pNow.x); 
				m_upx.SetWindowText(s); 
				s.Format("%d",pNow.y); 
				m_upy.SetWindowText(s); 
			} 
			else 
			{ 
				p2.x = pNow.x; 
				p2.y = pNow.y; 
				s.Format("%d",pNow.x); 
				m_bottomx.SetWindowText(s); 
				s.Format("%d",pNow.y); 
				m_bottomy.SetWindowText(s); 
			} 
			UpdateData(false);			 
		} 
	} 
	 
	return CDialog::WindowProc(message, wParam, lParam); 
} 
 
 
void CTrayDlg::OnBUTTONhookNew()  
{ 
	// TODO: Add your control notification handler code here 
	if(bHooked) 
		mhook.StopHook(); 
} 
 
void CTrayDlg::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 
	CDialog::OnLButtonDown(nFlags, point); 
} 
 
void CTrayDlg::OnDefault()  
{ 
	// TODO: Add your control notification handler code here 
	CString temp; 
	int temp_point; 
	char *end; 
	GetPrivateProfileString("Left top point X","leftX","0",temp.GetBuffer(MAX_PATH),MAX_PATH,".\\Tray.ini"); 
	m_upx.SetWindowText(temp); 
	temp_point = strtol(temp,&end,10); 
	p1.x = temp_point; 
	GetPrivateProfileString("Left top point Y","leftY","0",temp.GetBuffer(MAX_PATH),MAX_PATH,".\\Tray.ini"); 
	m_upy.SetWindowText(temp); 
	temp_point = strtol(temp,&end,10); 
	p1.y = temp_point;	 
	GetPrivateProfileString("Right button point X","rightX","0",temp.GetBuffer(MAX_PATH),MAX_PATH,".\\Tray.ini"); 
	m_bottomx.SetWindowText(temp); 
	temp_point = strtol(temp,&end,10); 
	p2.x = temp_point; 
	GetPrivateProfileString("Right button point Y","rightY","0",temp.GetBuffer(MAX_PATH),MAX_PATH,".\\Tray.ini"); 
	m_bottomy.SetWindowText(temp); 
	temp_point = strtol(temp,&end,10); 
	p2.y = temp_point;	 
}