screencopyv1dlg.cpp

来自「1: 注册系统热键(还有自定义消息函数) 2:托盘图标的简单建立 3:选择文」· C++ 代码 · 共 549 行

CPP
549
字号
// ScreenCopyV1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "ScreenCopyV1.h"
#include "ScreenCopyV1Dlg.h"
#include "Data.h"
#include "fstream.h"
#include "dialog1.h"
#include "TLHELP32.H"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CScreenCopyV1Dlg dialog

CScreenCopyV1Dlg::CScreenCopyV1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CScreenCopyV1Dlg::IDD, pParent)
{  
    ShowOrHide=false;
	 str="配置.ini";
//	ReadFile(str,data1);
ReadFile(str,dialog.logdata);
	//{{AFX_DATA_INIT(CScreenCopyV1Dlg)
	m_time = _T("");
	m_FileName = _T("");
	m_statiFileName = _T("");
	m_staticTime = _T("");
	m_staticture = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);


	m_staticTime="每隔 "+dialog.logdata.timer+" 秒抓次图";
	m_statiFileName=dialog.logdata.filename+" 中";
	int bl;
        bl=atoi(dialog.logdata.bl);
	if(bl)//data1.bl)
	{m_staticture="该程序下次会自动启动";}
		else{m_staticture="该程序下次不会自动启动";}
//dialog.logdata=data1;
}

void CScreenCopyV1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CScreenCopyV1Dlg)
	DDX_Text(pDX, IDC_STATICFileName, m_statiFileName);
	DDX_Text(pDX, IDC_STATICtime, m_staticTime);
	DDX_Text(pDX, IDC_STATICTURE, m_staticture);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CScreenCopyV1Dlg, CDialog)
	//{{AFX_MSG_MAP(CScreenCopyV1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_CREATE()
	ON_BN_CLICKED(IDC_STATR, OnStatr)
	ON_MESSAGE(WM_HOTKEY,OnHotKey)       //自定义系统热键消息涵数
	ON_BN_CLICKED(IDC_END, OnEnd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScreenCopyV1Dlg message handlers

BOOL CScreenCopyV1Dlg::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
       m_bOpen=TRUE;
		AddIcon();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CScreenCopyV1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		if (nID==SC_MINIMIZE)
		{
			m_bOpen=!m_bOpen;
			ModifyIcon();
			ShowWindow(SW_HIDE);
		}

		CDialog::OnSysCommand(nID, lParam);
		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 CScreenCopyV1Dlg::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 CScreenCopyV1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//********************************************************************************
// 名称:ReadFile
//功能:读取str文件 ,并且将读去到的信息放入放入类Data对象中
///********************************************************************************

void CScreenCopyV1Dlg::ReadFile(CString str, Data &data)
{
	fstream Rfile(str,ios::in | ios::out);
	char a[80];
	Rfile >> a;
	data.filename.Format("%s",a);
	Rfile >> a;
	data.timer.Format("%s",a);
	Rfile >> a;
	data.bl.Format("%s",a);
	Rfile >> a;
/*	data.rect.left.Format("%s",a);
		Rfile >> a;
	data.rect.right.Format("%s",a);
		Rfile >> a;
	data.rect.bottom.Format("%s",a);
		Rfile >> a;
	data.rect.top.Format("%s",a); */
	Rfile.close();

}



void CScreenCopyV1Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
dialog.DoModal();

//ReadFile(str,dialog.logdata);
//if(dialog)
	 str="配置.ini";
//	ReadFile(str,data1);
ReadFile(str,dialog.logdata);
	m_staticTime="每隔"+dialog.logdata.timer+"秒抓次图";
	m_statiFileName=dialog.logdata.filename+" 中";
	int bl;
        bl=atoi(dialog.logdata.bl);
	if(bl)//data1.bl)
	{m_staticture="该程序下次会自动启动";}
		else{m_staticture="该程序下次不会自动启动";}
		UpdateData(false);
}
//*********************************8
//窗口销毁函数、增加注销系统热键<Ctrl+Alt+F3键>和销毁托盘图标功能
//*******************************888
BOOL CScreenCopyV1Dlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	//注销系统热键,<Ctrl+Alt+F3键>
	UnregisterHotKey(GetSafeHwnd(),1000);    
   //销毁托盘图标
	DeleteIcon(); 

	return CDialog::DestroyWindow();
}



//********************************************************************************
//* 名称:OnHotKey
//* 功能:<Ctrl+Alt+F3键>显示或者隐藏程序界面
//********************************************************************************
LRESULT CScreenCopyV1Dlg::OnHotKey(WPARAM wParam, LPARAM lParam)
{ 

	if(wParam==1000&&ShowOrHide)
	{  
	//***************
	//得到主框架函数的句柄,设置为显示或者隐藏程序
	//************
		AfxGetMainWnd ( )->ShowWindow(SW_SHOW);
//增加托盘图标
		AddIcon();
    
    }
	else
	{   
	//***************
	//得到主框架函数的句柄,设置为显示或者隐藏程序
	//************
		AfxGetMainWnd ( )->ShowWindow(SW_HIDE);
        //删初托盘图标 
		DeleteIcon();
	}
 	ShowOrHide=!ShowOrHide;
    return 0;


}

int CScreenCopyV1Dlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	
//注册系统热键,<Ctrl+Alt+F3键>
    
//	RegisterHotKey(GetSafeHwnd(),WM_SHOWHOTKEY,MOD_ALT|MOD_CONTROL,VK_F3);
	RegisterHotKey(GetSafeHwnd(),1000,MOD_ALT|MOD_CONTROL,VK_F3);

	return 0;
}
// ---------------------------------------------------------
//	名称: PreTranslateMessage
//	功能: 截获ESC和回车键,避免按下此键时关闭对话框
//	变量: pMsg -- 消息
//	返回: 成功返回TRUE,否则返回FALSE
//	
// ---------------------------------------------------------
/*BOOL CAboutDlg::PreTranslateMessage(MSG* pMsg) 
{
	//截获ESC和回车键,避免按下此键时关闭对话框
	if (pMsg->message == WM_KEYDOWN)
    { 
      if(pMsg->wParam==VK_ESCAPE)
		  return true; 
	  if(pMsg->wParam==VK_RETURN) 
		  return true; 
    } 
	
	return false;		//更换成XP风格系统菜单
}
*/

void CScreenCopyV1Dlg::OnStatr() 
{
//*******
	//判断该程序是否已经运行 如果已经运行先关闭 在启动
	HWND hwnd=::FindWindow(NULL,"无标题 - 屏幕截取");

	if(hwnd)
	{
	//	MessageBox("find");
	::SendMessage(hwnd,WM_CLOSE,0,0);
	//	MessageBox("close");
	}
	else
	{
	//	MessageBox("程序没有启动");
	}
//**************************
	//********
	//先找到应用程序路径  在打开应用程序
	//
CString sPath,lpszFileName;
int nPos;
CFileFind fFind;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer(); 
nPos=sPath.ReverseFind('\\');
sPath=sPath.Left(nPos);
lpszFileName = sPath + "\\屏幕截取.EXE"; 
  
if(fFind.FindFile(lpszFileName)) 
{ 
	//如果找到了 应用程序 打开程序
   // ShellExecute(NULL,NULL,_T("屏幕截取.exe"),NULL,sPath,NULL); 
		//// the return value is greater than 31.
	if(	WinExec(lpszFileName,0)<=31)
		{
			
         MessageBox("出现未知错误","提示信息");
		  exit(0); //退出程序
		}
	else
	{
	MessageBox("启动程序成功","提示信息");

	}
	
}
else
{
MessageBox("没有找到应用程序,请重新安装该程序可能能修复问题","提示信息");
		  exit(0); //退出程序
}
	//******************************************************
//这是以进程的方式打开程序
	//PROCESS_INFORMATION pro_info;
 //       STARTUPINFO startupinfo;
        
    /*  startupinfo.cb=sizeof(startupinfo);
        startupinfo.wShowWindow=SW_HIDE;
        startupinfo.lpReserved=NULL;
        startupinfo.lpDesktop=NULL;
        startupinfo.lpTitle=NULL;
        startupinfo.dwX=0;
        startupinfo.dwY=0;
        startupinfo.dwXSize=200;
        startupinfo.dwYSize=300;
        startupinfo.dwXCountChars=500;
        startupinfo.dwYCountChars=500;
        startupinfo.dwFlags=STARTF_USESHOWWINDOW;
        startupinfo.wShowWindow=SW_HIDE;
        startupinfo.cbReserved2=0;
        startupinfo.lpReserved2=NULL;
        startupinfo.hStdInput=stdin;
        startupinfo.hStdOutput=stdout;
        startupinfo.hStdError=stderr;  */
 /*       CString str;
        str="屏幕截取.exe";
        if(!CreateProcess(NULL,(LPTSTR)(LPCTSTR)str,NULL,NULL,FALSE,CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&startupinfo,&pro_info))
        {     

              return;
        }
*/
}

void CScreenCopyV1Dlg::OnEnd() 
{
	// TODO: Add your control notification handler code here
/*	 if(!CreateProcess(NULL,(LPTSTR)(LPCTSTR)str,NULL,NULL,FALSE,CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&startupinfo,&pro_info))
        {
              return;
        }
		*/
	 //	TerminateProcess(pro_info.hProcess,NULL);
//CloseHandle(pro_info.hThread);
 //CloseHandle(pro_info.hProcess);
	// 查找已经存在的要关闭的进程进行关闭
 /*   DWORD ProcessID[50];
    CString kkk[50];
    HANDLE SnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 
    PROCESSENTRY32* info=new PROCESSENTRY32;
    PROCESSENTRY32 ProcessInfo;//声明进程信息变量
    ProcessInfo.dwSize=sizeof(ProcessInfo);//设置ProcessInfo的大小
    //返回系统中第一个进程的信息
    BOOL Status=Process32First(SnapShot,&ProcessInfo);
    int m_nProcess=0;
    /////////////////////////////////////////////////////// 
    while(Status)
    {
        CString s,str1,str2;
        s.Format("%d",ProcessInfo.cntThreads);
        str1.Format("%s",ProcessInfo.szExeFile);
        str1=ProcessInfo.szExeFile;
        kkk[m_nProcess]=ProcessInfo.szExeFile; 
        ProcessID[m_nProcess]=ProcessInfo.th32ProcessID; 
        //////////////////////////////////////////////////////
        if(str1=="屏幕截取")
        {
            HANDLE ProcessHandle;
            ProcessHandle=OpenProcess (PROCESS_ALL_ACCESS,FALSE,ProcessID[m_nProcess]);
            TerminateProcess(ProcessHandle,0);    
        }
        Status=Process32Next(SnapShot,&ProcessInfo);
        m_nProcess++;
    } 
    /////////////////////////// 
//最后记得加入头文件
#include "TLHELP32.H"//一定要加如这个头文件 
	*/
	HWND hwnd=::FindWindow(NULL,"无标题 - 屏幕截取");

	if(hwnd)
	{
	//	MessageBox("find");
	::SendMessage(hwnd,WM_CLOSE,0,0);
	//	MessageBox("close");
		MessageBox("关闭程序成功","提示信息");
	}
	else
	{
		MessageBox("程序没有启动","提示信息");
	}
}


//托盘部分/////////////////////////////////////////////
void  CScreenCopyV1Dlg::ModifyIcon()
{
	m_notify.hIcon =(HICON) LoadImage(AfxGetInstanceHandle(),
		MAKEINTRESOURCE(m_bOpen?IDI_ICON2:IDI_ICON1),
									  IMAGE_ICON,
									  16,
									  16,
									  NULL);
	lstrcpy(m_notify.szTip ,m_bOpen?"程序打开":"程序隐藏");
	Shell_NotifyIcon(NIM_MODIFY,&m_notify);
}

void  CScreenCopyV1Dlg::DeleteIcon()
{
	m_notify.uFlags =NULL;
	Shell_NotifyIcon(NIM_DELETE,&m_notify);

}

void  CScreenCopyV1Dlg::AddIcon()
{
	
	m_notify.cbSize =sizeof(NOTIFYICONDATA);
	m_notify.hWnd =GetSafeHwnd();
	m_notify.uID =0;
	m_notify.uFlags =NIF_ICON|NIF_MESSAGE|NIF_TIP;
	m_notify.uCallbackMessage =WM_COMMAND;
	m_notify.hIcon =(HICON) LoadImage(AfxGetInstanceHandle(),
									  MAKEINTRESOURCE(IDI_ICON2),
									  IMAGE_ICON,
									  16,
									  16,
									  NULL);
    lstrcpy(m_notify.szTip ,"程序打开");
	Shell_NotifyIcon(NIM_ADD,&m_notify);

}

BOOL  CScreenCopyV1Dlg::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (wParam==0)	{
		
		if (lParam ==WM_LBUTTONDOWN){
			m_bOpen=!m_bOpen;
			ModifyIcon();
			if (m_bOpen)
					ShowWindow(SW_SHOWNORMAL);
			else 
				ShowWindow(SW_HIDE);
		}
		return TRUE;
	}



	
	return CDialog::OnCommand(wParam, lParam);
}
/////////托盘部分结束///////////////////////////////////////


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?