⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testdlg.cpp

📁 Hook API 实现文件名自动变换。并提供相关的测试程序。
💻 CPP
字号:
// testDlg.cpp : implementation file
//

#include "stdafx.h"
#include "test.h"
#include "testDlg.h"

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

//---------------------------------------------------------------------------
BOOL		g_bStart = FALSE;
HINSTANCE	g_hInst = NULL;

//注册系统热键
#define ID_A	1501
#define ID_B	1502
#define ID_C	1503
#define ID_D	1504
#define ID_E	1505
//---------------------------------------------------------------------------
#define MsgInf(str) ::MessageBox(0,TEXT(str),TEXT("信息"),MB_OK | MB_ICONINFORMATION);
#define MsgErr(str) ::MessageBox(0,TEXT(str),TEXT("错误"),MB_OK | MB_ICONERROR);
BOOL MsgYN(char* str)	
{
	if( (::MessageBox(0,TEXT(str),TEXT("提示信息"),MB_YESNO | 
		MB_ICONINFORMATION | MB_DEFBUTTON2) == IDNO ) )
	{
		return FALSE;
	}
	return TRUE;
}
//---------------------------------------------------------------------------
//判断字符是否浮点型
BOOL IsFloat(char* source)
{
	int iLen,i;

	if(strlen(source) <= 0)
		return FALSE;

	iLen = strlen(source);
	for(i = 0; i < iLen; i++)
	{
		if((source[i] >= '0') && (source[i] <= '9') || (source[i] == '.'))
		{
			;
		}
		else
		{
			return FALSE;
		}
	}

	return TRUE;
	
}
//判断fNum是否在f1~f2范围之间
BOOL IsArea(float fNum,float f1,float f2)
{
	if( (fNum < f1) || (fNum > f2) )
		return FALSE;
	else 
		return TRUE;
}
//---------------------------------------------------------------------------
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()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_COMMAND(IDM_APP_EXIT, OnAppExit)
	ON_COMMAND(IDM_APP_SHOW, OnAppShow)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BTN_MSG, OnBtnMsg)
	//}}AFX_MSG_MAP
	ON_MESSAGE(MYWM_NOTIFYICON,OnMyNotifyIcon)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

BOOL CTestDlg::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
	
	//------------------------------------------------------------
	//此程序只能运行一次,用互斥量来判断程序是否已运行
    HANDLE m_hMutex=CreateMutex(NULL,TRUE, "钩你没商量"); 
    if(GetLastError() == ERROR_ALREADY_EXISTS) 
	{ 
		//MessageBox("此工程只允许运行一次!",TITLE,MB_OK|MB_ICONWARNING);
		CDialog::OnCancel(); 
	}	
	//------------------------------------------------------------
	LPCTSTR iniFile = ".\\config.ini";		// “.\\”表示当前路径		
	char sTmp[256] = {'\0'}; 
	GetPrivateProfileString("NUM","FNUM",NULL,sTmp,256,iniFile);
	::SetWindowText(GetDlgItem(IDC_EDIT1)->GetSafeHwnd(),sTmp);
	//------------------------------------------------------------
	TaskBarAddIcon(1);

	RegSysHotkey();


	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


//-------------------------------------------------------------------------
//添加系统栏图标
int CTestDlg::TaskBarAddIcon(int iFlag)
{
	BOOL res;
	NOTIFYICONDATA tnid;
	HICON hicon;

	if(iFlag == 2)
		hicon = LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON2));
	else
		hicon = LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON1));
	
	tnid.cbSize = sizeof(NOTIFYICONDATA);
	tnid.hWnd = m_hWnd;
	tnid.uID = MYICON;
	tnid.uCallbackMessage = MYWM_NOTIFYICON;
	tnid.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
	tnid.hIcon = hicon;
	lstrcpyn(tnid.szTip,MYTIP,sizeof(MYTIP));
	res = Shell_NotifyIcon(NIM_ADD,&tnid);
	if (hicon)
		DestroyIcon(hicon);
	return res;

}
//-------------------------------------------------------------------------
//删除系统栏图标
int CTestDlg::TaskBarDeleteIcon()
{
	BOOL res;
	NOTIFYICONDATA tnid;

	tnid.cbSize = sizeof(NOTIFYICONDATA);
	tnid.hWnd = m_hWnd;
	tnid.uID = MYICON;
	res = Shell_NotifyIcon(NIM_DELETE,&tnid);
	return res;
}
//-------------------------------------------------------------------------
//消息截获
LRESULT CTestDlg::OnMyNotifyIcon(WPARAM wParam,LPARAM lParam)
{
	switch(lParam)
	{
		case WM_LBUTTONDOWN:
			if (wParam==MYICON)
			{
				ShowWindow(SW_SHOW);
				SetForegroundWindow();
			}
			break;
		case WM_RBUTTONDOWN:
			if (wParam==MYICON)
			{
				CMenu *pt,t;
				CPoint pp;

				t.LoadMenu(IDM_POP_MENU1);
				pt=t.GetSubMenu(0);
				GetCursorPos(&pp);
				pt->TrackPopupMenu(TPM_RIGHTALIGN|TPM_LEFTBUTTON,pp.x,pp.y,this);
					

			}
			break;
	}
	return 0L;
}

//-------------------------------------------------------------------------
//开始钩
void CTestDlg::HookStart()
{
	if(NULL != g_hInst)	
	{
		MsgErr("something wrong!");
		return;
	}

	g_hInst = LoadLibrary("Hookyou.dll");
	if(g_hInst)
	{
		typedef BOOL (WINAPI *SETNHW32)();
		SETNHW32 SetNHW32 = NULL;
		SetNHW32 = GetProcAddress(g_hInst, "SetNHW32");
		if(SetNHW32)
		{
			if(!SetNHW32())
			{
				MsgErr("Unable to Set nhw32!");
			}
		}
		else
		{
			MsgErr("wrong!");
		}
	}


}

//-------------------------------------------------------------------------
//停止
void CTestDlg::HookStop()
{
	try
	{
		typedef BOOL (WINAPI *RESETNHW32)();
		RESETNHW32 ResetNHW32 = NULL;
		ResetNHW32 = GetProcAddress(g_hInst, "ResetNHW32");
		if(ResetNHW32)
		{
			ResetNHW32();
		}

		FreeLibrary(g_hInst);	
		g_hInst = NULL;
	}
	catch(...)
	{
		FreeLibrary(g_hInst);	
		g_hInst = NULL;
	}
}

//-------------------------------------------------------------------------
//退出程序
void CTestDlg::ExitApp()
{
	HookStop();
	TaskBarDeleteIcon();	
	
}

//-------------------------------------------------------------------------
//彻底隐藏,连系统栏图标都没有
//必须以热键呼叫才能显示界面
void CTestDlg::HideMe()
{
	ShowWindow(SW_HIDE);
	TaskBarDeleteIcon();

}

//-------------------------------------------------------------------------
//保存基数
void CTestDlg::OnButtonSave() 
{
	UpdateData();
	char sTmp[20] = {'\0'}; 
	::GetWindowText(GetDlgItem(IDC_EDIT1)->GetSafeHwnd(),sTmp,21);
	
	if( IsFloat(sTmp) && IsArea(atof(sTmp),0,1) )
	{
			WritePrivateProfileString("NUM","FNUM",sTmp,".\\config.ini");
			MsgInf("保存配置信息成功!");
	}
	else
	{
		MsgErr("输入的基数必须是0与1之间,保留两位少数点!\n保存配置文件失败。");
		return;
	}
	
}

void CTestDlg::OnButton1() 
{
	ShowWindow(SW_HIDE);
}

void CTestDlg::OnButton2() 
{
	if(FALSE == g_bStart)
	{
		g_bStart = TRUE;
		::EnableWindow(GetDlgItem(IDC_BUTTON2)->GetSafeHwnd(),FALSE);
		::EnableWindow(GetDlgItem(IDC_BUTTON3)->GetSafeHwnd(),TRUE);
		TaskBarDeleteIcon();
		TaskBarAddIcon(2);
		
		ShowWindow(SW_HIDE);
		HookStart();
	}

}

void CTestDlg::OnButton3() 
{
	if(TRUE == g_bStart)
	{
		g_bStart = FALSE;
		::EnableWindow(GetDlgItem(IDC_BUTTON2)->GetSafeHwnd(),TRUE);
		::EnableWindow(GetDlgItem(IDC_BUTTON3)->GetSafeHwnd(),FALSE);
		TaskBarDeleteIcon();
		TaskBarAddIcon(1);
		
		HookStop();
	}
}

void CTestDlg::OnButton4() 
{
	OnAppExit();		
}

void CTestDlg::OnAppExit() 
{
	ExitApp();		
	PostQuitMessage(0);
	//CDialog::OnCancel();
}

void CTestDlg::OnAppShow() 
{
	ShowWindow(SW_SHOW);
	TaskBarDeleteIcon();
	
	if(FALSE == g_bStart)
	{
		TaskBarAddIcon(1);
	}
	else
	{
		TaskBarAddIcon(2);	
	}
}

void CTestDlg::OnDestroy() 
{
	
	ExitApp();

	CDialog::OnDestroy();
	
}

//-------------------------------------------------------------------------
//注册系统热键
void CTestDlg::RegSysHotkey()
{
	HWND hWnd = this->m_hWnd;
	RegisterHotKey(hWnd, ID_A, MOD_ALT, 65);	//Alt + A
	RegisterHotKey(hWnd, ID_B, MOD_ALT, 66);	//Alt + B
	RegisterHotKey(hWnd, ID_C, MOD_ALT, 67);
	RegisterHotKey(hWnd, ID_D, MOD_ALT, 68);
	RegisterHotKey(hWnd, ID_E, MOD_ALT, 69);

}


BOOL CTestDlg::PreTranslateMessage(MSG* pMsg) 
{
	switch (pMsg->message)
	{
	//处理系统热键WM_HOTKEY消息
	case WM_HOTKEY:
		switch(pMsg->wParam)
		{
		case ID_A:
			OnAppShow();
			break;
		case ID_B:	//彻底隐藏
			HideMe();
			break;
		case ID_C:
			OnButton2();
			break;
		case ID_D:
			OnButton3();
			break;
		case ID_E:
			ExitApp();
			PostQuitMessage(0);
			break;
		}
		
		break;

	case WM_SYSCOMMAND:
		//switch(pMsg->wParam)
		//{
		case SC_CLOSE:
			ShowWindow(SW_MINIMIZE);
			break;
		//}

		break;
	
	}

	return CDialog::PreTranslateMessage(pMsg);
}

void CTestDlg::OnOK() 
{
	//屏蔽Enter	
	//CDialog::OnOK();
	ShowWindow(SW_MINIMIZE);
}

void CTestDlg::OnCancel() 
{
	//屏蔽Escape	
	//CDialog::OnCancel();
	ShowWindow(SW_HIDE);
}



void CTestDlg::OnBtnMsg() 
{
	MessageBox("这是测试框,如果你看到标题显示的是【独钓寒江雪】,表示已经开始HOOK了。");	
}

⌨️ 快捷键说明

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