?? testdlg.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 + -