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

📄 stickykeydlg.cpp

📁 程序自动完成向“我的文档“下shiyan.docx发送字符c
💻 CPP
字号:
// StickyKeyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "StickyKey.h"
#include "StickyKeyDlg.h"

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

#define WM_LIBEN WM_USER+1
#define WM_MYMESSAGE WM_USER+2

int i=0;

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

/////////////////////////////////////////////////////////////////////////////
// CStickyKeyDlg dialog

CStickyKeyDlg::CStickyKeyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CStickyKeyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStickyKeyDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CStickyKeyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStickyKeyDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CStickyKeyDlg, CDialog)
	//{{AFX_MSG_MAP(CStickyKeyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(IDM_EXIT, OnExit)
	ON_COMMAND(IDM_HIDE, OnHide)
	ON_COMMAND(IDM_SHOW, OnShow)
	ON_WM_CLOSE()
	ON_MESSAGE(WM_LIBEN,OnLiben)
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_MESSAGE(WM_MYMESSAGE,OnMyMessage)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStickyKeyDlg message handlers

BOOL CStickyKeyDlg::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
	NOTIFYICONDATA tnd;
	tnd.cbSize=sizeof(NOTIFYICONDATA);
	tnd.hWnd=AfxGetMainWnd()->m_hWnd;
	tnd.uID=IDR_MAINFRAME;
	tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
 	tnd.uCallbackMessage=WM_LIBEN;
	tnd.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	strcpy(tnd.szTip,"StickyKey");
	Shell_NotifyIcon(NIM_ADD,&tnd);

	m_bShow=true;

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

void CStickyKeyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else if(nID==SC_MINIMIZE)
	{
		CDialog::OnSysCommand(nID, lParam);
		m_bShow=false;
		AfxGetMainWnd()->ShowWindow(SW_HIDE);//隐藏窗口
	}
	else if(nID==SC_CLOSE)
	{
		AfxGetMainWnd()->PostMessage(WM_CLOSE);
	}
	else if(nID==SC_RESTORE)
	{
		AfxGetMainWnd()->ShowWindow(SW_SHOW);//显示窗口
		CDialog::OnSysCommand(nID, lParam);
		m_bShow=true;
	}
	else if(nID==SC_MAXIMIZE){;}
	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 CStickyKeyDlg::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 CStickyKeyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CStickyKeyDlg::OnLiben(WPARAM wParam,LPARAM lParam)
{
	UINT uMouseMsg;//鼠标动作
	uMouseMsg=(UINT) lParam;
	if(uMouseMsg==WM_LBUTTONDOWN)//如果是单击左键
	{
		if(m_bShow)
		{
			OnHide();
		}
		else
		{
			OnShow();
		}
	}
	if(uMouseMsg==WM_RBUTTONDOWN)//如果是单击右键
	{
		AfxGetMainWnd()->SetForegroundWindow(); //弹出Popup菜单
		CMenu menu;
		menu.LoadMenu(IDR_MENU1);
		CMenu* pPopup=menu.GetSubMenu(0);
		CPoint Point;
		GetCursorPos(&Point);
		pPopup->TrackPopupMenu(TPM_LEFTALIGN,
			Point.x,Point.y,AfxGetMainWnd(),NULL );
		AfxGetMainWnd()->PostMessage(WM_NULL, 0, 0); 
	}
}

void CStickyKeyDlg::OnExit() 
{
	// TODO: Add your command handler code here
	AfxGetMainWnd()->PostMessage(WM_CLOSE); //退出程序
}

void CStickyKeyDlg::OnHide() 
{
	// TODO: Add your command handler code here
	PostMessage(WM_SYSCOMMAND,SC_MINIMIZE,NULL);
}

void CStickyKeyDlg::OnShow() 
{
	// TODO: Add your command handler code here
	PostMessage(WM_SYSCOMMAND,SC_RESTORE,NULL);
}

void CStickyKeyDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	NOTIFYICONDATA tnid;
	tnid.cbSize=sizeof(NOTIFYICONDATA);
	tnid.hWnd=AfxGetMainWnd()->m_hWnd;
	tnid.uID=IDR_MAINFRAME;//保证删除的是我们的图标
	Shell_NotifyIcon(NIM_DELETE,&tnid);

	CDialog::OnClose();
}

int CStickyKeyDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetTimer(1,200,NULL);
	return 0;
}

void CStickyKeyDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	int times=20;

	if(i==times+2)
	{
	//	keybd_event(VK_MENU,0,0,0);
	//	keybd_event(VK_F4,0,0,0);
	//	keybd_event(VK_F4,0,KEYEVENTF_KEYUP,0);
	//	keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0); 

		AfxGetMainWnd()->PostMessage(WM_CLOSE);
	}
	else
	{
		i++;
		SendMessage(WM_MYMESSAGE);
		CDialog::OnTimer(nIDEvent);
	}
}

LRESULT CStickyKeyDlg::OnMyMessage(WPARAM wParam,LPARAM lParam)
{
	
	HWND pWnd;
	pWnd=::FindWindow(NULL,"shiyan.docx - Microsoft Word");

//	HANDLE hDir;
//	hDir=FindFirstChangeNotification("C:\\Documents and Settings\\Administrator\\My Documents\\shiyan\\shiyan.txt",
//		0,FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE);
	if(!pWnd)
	{
		i--;
	}
//	else if(hDir==INVALID_HANDLE_VALUE&&i>4)
//	{
//		i--;
//	}

	else
	{	
		AFX_MANAGE_STATE(AfxGetStaticModuleState());	//最前面位置
	
		if(i==1)
		{		
			::OpenIcon(pWnd);	
			::SetForegroundWindow(pWnd);	
		}
		else 
			if(i==2)								//不加这个条件可能会少输入一次
		{;}	
		else if(pWnd!=::GetForegroundWindow())
		{
			i--;
		}
		else
		{

			keybd_event('C',0,0,0);
			keybd_event('C',0,KEYEVENTF_KEYUP,0);

	//		keybd_event(VK_CONTROL,0,0,0);
	//		keybd_event('S',0,0,0);
	//		keybd_event('S',0,KEYEVENTF_KEYUP,0);
	//		keybd_event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);

		}
	}
	
	return 0;
}

void CStickyKeyDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	KillTimer(1);

}

⌨️ 快捷键说明

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