📄 mouse_server_2dlg.cpp
字号:
// mouse_server_2Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "mouse_server_2.h"
#include "mouse_server_2Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CEvent threadBegin;
CEvent threadEnd;
HWND CMouse_server_2Dlg::ms_hWndTarget=NULL;
HWND CMouse_server_2Dlg::ms_hWndSelf=NULL;
int CMouse_server_2Dlg::ms_nCount=NULL;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CMouse_server_2Dlg dialog
CMouse_server_2Dlg::CMouse_server_2Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMouse_server_2Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMouse_server_2Dlg)
m_nInterval = 1;
m_nCount = -1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bBegin=FALSE;
m_bRecord=FALSE;
m_bWndShow=TRUE;
m_pMouseEvents=NULL;
m_hMapObject=NULL;
}
void CMouse_server_2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMouse_server_2Dlg)
DDX_Control(pDX, IDC_LIST, m_procList);
DDX_Text(pDX, IDC_EDIT1, m_nInterval);
DDV_MinMaxUInt(pDX, m_nInterval, 1, 3600);
DDX_Text(pDX, IDC_EDIT2, m_nCount);
DDV_MinMaxInt(pDX, m_nCount, -1, 2147483647);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMouse_server_2Dlg, CDialog)
//{{AFX_MSG_MAP(CMouse_server_2Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_MIN, OnMin)
ON_BN_CLICKED(IDC_REFLESHPROCESS, OnRefleshprocess)
ON_COMMAND(IDM_RECORD, OnRecord)
ON_COMMAND(IDM_BEGIN, OnBegin)
ON_COMMAND(IDM_END, OnEnd)
ON_COMMAND(IDM_CANCEL, OnCancel)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMouse_server_2Dlg message handlers
BOOL CMouse_server_2Dlg::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_menu.LoadMenu(IDR_MENU);
m_mousehook.Start();
m_mousehook.SetCount(0);
m_hMapObject=::OpenFileMapping(FILE_MAP_READ,FALSE,_T("Mouse DLL Share Memory"));
ASSERT(m_hMapObject);
m_pMouseEvents=(MouseEvent*)::MapViewOfFile(m_hMapObject,FILE_MAP_READ,0,0,0);
ASSERT(m_pMouseEvents);
//录制
RegisterHotKey(this->GetSafeHwnd(),1,MOD_ALT,'1');
//开始点击
RegisterHotKey(this->GetSafeHwnd(),2,MOD_ALT,'2');
//结束点击
RegisterHotKey(this->GetSafeHwnd(),3,MOD_ALT,'3');
//退出
RegisterHotKey(this->GetSafeHwnd(),4,MOD_ALT,'4');
return TRUE; // return TRUE unless you set the focus to a control
}
void CMouse_server_2Dlg::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 CMouse_server_2Dlg::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 CMouse_server_2Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LRESULT CMouse_server_2Dlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch(message)
{
case WM_USER+10 :
if((lParam==WM_LBUTTONDOWN)&&(!m_bRecord)&&(!m_bBegin))
{
if(!m_bWndShow)
{
::ShowWindow(m_hWnd,SW_RESTORE);
::BringWindowToTop(m_hWnd);
m_bWndShow=TRUE;
}
}
if(lParam==WM_RBUTTONUP)
{
CPoint pt;
GetCursorPos(&pt);
if(!m_bRecord){
if(!m_bBegin){
m_menu.GetSubMenu(0)->EnableMenuItem(0,MF_BYPOSITION|MF_ENABLED);
m_menu.GetSubMenu(0)->EnableMenuItem(1,MF_BYPOSITION|MF_GRAYED);
m_menu.GetSubMenu(0)->ModifyMenu(3,MF_BYPOSITION|MF_GRAYED,IDM_RECORD,"录制鼠标");
m_menu.GetSubMenu(0)->EnableMenuItem(3,MF_BYPOSITION|MF_ENABLED);
}else{
m_menu.GetSubMenu(0)->EnableMenuItem(0,MF_BYPOSITION|MF_GRAYED);
m_menu.GetSubMenu(0)->EnableMenuItem(1,MF_BYPOSITION|MF_ENABLED);
m_menu.GetSubMenu(0)->ModifyMenu(3,MF_BYPOSITION|MF_GRAYED,IDM_RECORD,"录制鼠标");
m_menu.GetSubMenu(0)->EnableMenuItem(3,MF_BYPOSITION|MF_GRAYED);
}
}else{
m_menu.GetSubMenu(0)->EnableMenuItem(0,MF_BYPOSITION|MF_GRAYED);
m_menu.GetSubMenu(0)->EnableMenuItem(1,MF_BYPOSITION|MF_GRAYED);
m_menu.GetSubMenu(0)->ModifyMenu(3,MF_BYPOSITION|MF_GRAYED,IDM_RECORD,"结束录制");
m_menu.GetSubMenu(0)->EnableMenuItem(3,MF_BYPOSITION|MF_ENABLED);
}
m_menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTBUTTON|TPM_RIGHTALIGN,pt.x,pt.y,this);
}
break;
case WM_HOTKEY:
switch (wParam)
{
case 1:
this->OnRecord();
break;
case 2:
this->OnBegin();
break;
case 3:
this->OnEnd();
break;
case 4:
this->OnCancel();
break;
}
}
return CDialog::WindowProc(message, wParam, lParam);
}
void CMouse_server_2Dlg::OnMin()
{
if(m_procList.GetCurSel()==LB_ERR)
{
AfxMessageBox("请选择一个程序");
return;
}
NOTIFYICONDATA nid;
nid.cbSize = sizeof(nid);
nid.uFlags = NIF_ICON |NIF_MESSAGE | NIF_TIP;
nid.hIcon = ::AfxGetApp()->LoadIcon(IDI_ICON);
strcpy(nid.szTip,"自动点击");
nid.uCallbackMessage = WM_USER+10;
nid.uID = 10;
nid.hWnd = m_hWnd;
if(!::Shell_NotifyIcon(NIM_ADD,&nid))
::Shell_NotifyIcon(NIM_MODIFY,&nid);
::ShowWindow(m_hWnd,SW_HIDE);
m_bWndShow = FALSE;
}
BOOL CMouse_server_2Dlg::DestroyWindow()
{
threadEnd.SetEvent();
NOTIFYICONDATA nid;
nid.cbSize=sizeof(nid);
nid.uID=10;
nid.hWnd=m_hWnd;
::Shell_NotifyIcon(NIM_DELETE,&nid);
m_menu.DestroyMenu( );
::UnmapViewOfFile(m_pMouseEvents);
m_pMouseEvents=NULL;
::CloseHandle(m_hMapObject);
m_hMapObject=NULL;
UnregisterHotKey(this->GetSafeHwnd(),1);
UnregisterHotKey(this->GetSafeHwnd(),2);
UnregisterHotKey(this->GetSafeHwnd(),3);
return CDialog::DestroyWindow();
}
void CMouse_server_2Dlg::OnRefleshprocess()
{
m_procList.ResetContent();
m_eventVector.erase(m_eventVector.begin(),m_eventVector.end());
HWND pWnd=::GetWindow(::GetDesktopWindow(),GW_CHILD);
char strText[126];
int iCur;
while(pWnd!=NULL)
{
if(::IsWindowVisible(pWnd)&&
::GetWindowTextLength(pWnd)&&!::GetParent(pWnd)&&pWnd!=(HWND)0x130)
{
::GetWindowText(pWnd,strText,128);
if((strcmp(strText,"Program Manager")!=0)&&(strcmp(strText,"自动鼠标点击")!=0)){
iCur=m_procList.AddString(strText);
m_procList.SetItemData(iCur,(DWORD)pWnd);
}
}
pWnd=::GetWindow(pWnd,GW_HWNDNEXT);
}
}
void CMouse_server_2Dlg::OnRecord()
{
if(!m_bRecord){
m_mousehook.SetTargetWindow((HWND)m_procList.GetItemData(m_procList.GetCurSel()));
m_mousehook.Continue();
}else{
int nCount;
m_eventVector.erase(m_eventVector.begin(),m_eventVector.end());
nCount=m_pMouseEvents[0].point.x;
m_mousehook.Continue(FALSE);
m_eventVector.erase(m_eventVector.begin(),m_eventVector.end());
if(nCount!=0){
for(int i=1;i<=nCount;i++){
m_eventVector.push_back(m_pMouseEvents[i]);
}
MouseEvent event;
event.message=152;
event.time=m_pMouseEvents[nCount].time+m_nInterval*1000;
m_eventVector.push_back(event);
}
}
m_bRecord=!m_bRecord;
}
void CMouse_server_2Dlg::OnCancel()
{
this->OnOK();
}
void CMouse_server_2Dlg::OnBegin()
{
// TODO: Add your command handler code here
if(m_eventVector.empty()){
MessageBox("没有任何鼠标点击的记录!\n请先录制鼠标的点击过程。","咦,好像有错",MB_ICONEXCLAMATION|MB_OK );
return ;
}
UpdateData();
this->ms_hWndTarget=(HWND)m_procList.GetItemData(m_procList.GetCurSel());
this->ms_hWndSelf=this->GetSafeHwnd();
this->ms_nCount=this->m_nCount;
::AfxBeginThread(SendMouseClick,&m_eventVector,THREAD_PRIORITY_LOWEST);
m_bBegin=TRUE;
threadEnd.ResetEvent();
threadBegin.SetEvent();
NOTIFYICONDATA nid;
nid.cbSize = sizeof(nid);
nid.uFlags = NIF_ICON |NIF_MESSAGE | NIF_TIP;
nid.hIcon = ::AfxGetApp()->LoadIcon(IDI_SMILE);
strcpy(nid.szTip,"自动点击");
nid.uCallbackMessage = WM_USER+10;
nid.uID = 10;
nid.hWnd = m_hWnd;
if(!::Shell_NotifyIcon(NIM_ADD,&nid))
::Shell_NotifyIcon(NIM_MODIFY,&nid);
}
UINT CMouse_server_2Dlg::SendMouseClick(LPVOID pParam)
{
vector<MouseEvent> * pEventsVector=(vector<MouseEvent> *)pParam;
vector<MouseEvent>::iterator theIt;
::WaitForSingleObject(threadBegin,INFINITE);
register int i;
int nCount=(ms_nCount==-1)?100:ms_nCount;
for( i=0;i<nCount;i++)
{
if(ms_nCount==-1)
i--;
if(::WaitForSingleObject(threadEnd,0)==WAIT_OBJECT_0)
break;
for(theIt=pEventsVector->begin();(theIt+1)!=pEventsVector->end();theIt++){
POINT pt;
pt.x= theIt->point.x;
pt.y= theIt->point.y;
::SendMessage(theIt->hWnd,theIt->message,0,MAKELPARAM(pt.x,pt.y));
DWORD sleeptime=(theIt+1)->time-theIt->time;
//afxDump<<sleeptime<<"\n";
Sleep(sleeptime);
}
}
::SendMessage(ms_hWndSelf,WM_COMMAND,IDM_END,0);
return 0;
}
void CMouse_server_2Dlg::OnEnd()
{
threadEnd.SetEvent();
threadBegin.ResetEvent();
m_bBegin=FALSE;
NOTIFYICONDATA nid;
nid.cbSize = sizeof(nid);
nid.uFlags = NIF_ICON |NIF_MESSAGE | NIF_TIP;
nid.hIcon = ::AfxGetApp()->LoadIcon(IDI_ICON);
strcpy(nid.szTip,"自动点击");
nid.uCallbackMessage = WM_USER+10;
nid.uID = 10;
nid.hWnd = m_hWnd;
if(!::Shell_NotifyIcon(NIM_ADD,&nid))
::Shell_NotifyIcon(NIM_MODIFY,&nid);
}
void CMouse_server_2Dlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CAboutDlg dlg;
dlg.DoModal();
CDialog::OnClose();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -