托盘,右键,windows全局快捷键,双击alt,自启动,ui隐藏

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_18218335/article/details/93193595

// autoselectDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "autoselect.h"
#include "autoselectDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CautoselectDlg 对话框




CautoselectDlg::CautoselectDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CautoselectDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bEnable = FALSE;
}

void CautoselectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}
#define WM_SHOWTASK			WM_USER + 0x100
#define WM_AUTO_RUN_FUNC	WM_USER + 0x100 + 1
#define WM_OPEN_FUNC		WM_USER + 0x100 + 2

#define IDH_HOT_KEY_ALT		WM_USER + 0x100 + 3

#define IDM_OPEN_FUNC	2000
#define IDM_AUTO_FUNC	2001

BEGIN_MESSAGE_MAP(CautoselectDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_CHECK_AUTO_RUN, &CautoselectDlg::OnBnClickedCheckAutoRun)
	ON_MESSAGE(WM_SHOWTASK,OnShowTask)
	ON_WM_SIZE()
	ON_WM_DESTROY()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_CHECK_ENALE_HOTKEY, &CautoselectDlg::OnBnClickedCheckEnaleHotkey)
	ON_WM_HOTKEY()
END_MESSAGE_MAP()


// CautoselectDlg 消息处理程序

BOOL CautoselectDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标
	
	// TODO: 在此添加额外的初始化代码
	((CButton*)GetDlgItem(IDC_CHECK_AUTO_RUN))->SetCheck(bCheckAutoRun() ? BST_CHECKED : BST_UNCHECKED);

	m_notify.cbSize = (DWORD)sizeof(m_notify);
	m_notify.hWnd = this->m_hWnd;
	m_notify.uID = IDR_MAINFRAME;
	m_notify.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
	m_notify.uCallbackMessage = WM_SHOWTASK;
	m_notify.hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON_TRAY));
	_tcscpy(m_notify.szTip,GetCurrentState());
	Shell_NotifyIcon(NIM_ADD,&m_notify);
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}
CString CautoselectDlg::GetCurrentState()
{
	// 返回当前的状态
	CString strRet = bCheckAutoRun() ? L"开机启动运行" : L"开机不自动运行";
	strRet += ",";
	strRet += m_bEnable ? L"开启快捷键" : L"关闭快捷键";
	return strRet;
}
void CautoselectDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CautoselectDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CautoselectDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}
#define SUB_KEY_NAME _T("autoselect")
#define TARGET_AUTO_RUN_KEY _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")
BOOL CautoselectDlg::bCheckAutoRun()
{
	HKEY hKey = NULL;
	TCHAR strExeFullDir[MAX_PATH] = {};
	GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
	TCHAR	strReadValue[MAX_PATH * 2] = {};
	DWORD	dwBuffSize = sizeof(strReadValue);
	DWORD dwType = 0;
	long result = SHGetValue(HKEY_CURRENT_USER,TARGET_AUTO_RUN_KEY,SUB_KEY_NAME,&dwType,strReadValue,&dwBuffSize);
	if(_tcscmp(strExeFullDir, strReadValue) == 0)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
BOOL CautoselectDlg::RegistryAutoRun(BOOL bEnable)
{
	BOOL bRet = FALSE;// 操作失败
	
	BOOL bNow = bCheckAutoRun();
	if(bNow == bEnable)
		return TRUE;
	else
	{
		if(!bNow)
		{
			TCHAR strExeFullDir[MAX_PATH];
			GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
			if(SHSetValue(HKEY_CURRENT_USER,TARGET_AUTO_RUN_KEY,SUB_KEY_NAME,REG_SZ,strExeFullDir,sizeof(strExeFullDir)) != ERROR_SUCCESS)
			{
				WRITE_LOG(L"添加注册表键失败 gle[%d]",GetLastError());
				return FALSE;
			}
		}
		else
		{
			// 需要删除
			if(SHDeleteValue(HKEY_CURRENT_USER,TARGET_AUTO_RUN_KEY,SUB_KEY_NAME) != ERROR_SUCCESS)
			{
				WRITE_LOG(L"删除注册表键失败 gle[%d]",GetLastError());
				return FALSE;
			}
		}
		UpdateTip();
	}
	return TRUE;
}
void CautoselectDlg::OnBnClickedCheckAutoRun()
{
	// TODO: Add your control notification handler code here
	RegistryAutoRun(!bCheckAutoRun());
	((CButton*)GetDlgItem(IDC_CHECK_AUTO_RUN))->SetCheck(bCheckAutoRun() ? BST_CHECKED : BST_UNCHECKED);
	PostMessage(WM_PAINT);
}

LRESULT CautoselectDlg::OnShowTask(WPARAM wParam,LPARAM lParam)
{
	if(wParam != IDR_MAINFRAME)
		return 1;
	
	switch(lParam)
	{
	case WM_RBUTTONUP:
		{
			tagPOINT point = {};
			::GetCursorPos(&point);
			HMENU	hMenu = CreatePopupMenu();
			AppendMenu(hMenu,MF_STRING,IDM_OPEN_FUNC,m_bEnable ? L"关闭快捷键" : L"打开快捷键");
			//AppendMenu(hMenu,MF_SEPARATOR,0,NULL);
			AppendMenu(hMenu,MF_STRING,IDM_AUTO_FUNC,bCheckAutoRun() ? L"取消启动运行" : L"开机自动运行");
			//AppendMenu(hMenu,MF_SEPARATOR,0,NULL);
			AppendMenu(hMenu,MF_STRING,WM_DESTROY,L"退出");
			//AppendMenu(hMenu,MF_SEPARATOR,0,NULL);
			SetForegroundWindow();
			int iId = TrackPopupMenu(hMenu,TPM_RIGHTBUTTON | TPM_RETURNCMD,point.x,point.y,NULL,m_hWnd,NULL);
			
			if(iId == 0)
				DestroyMenu(hMenu);
			else
			{
				switch(iId)
				{
				case WM_DESTROY:
					{
						PostMessage(WM_QUIT);
						break;
					}
				case IDM_OPEN_FUNC:
					{
						OnBnClickedCheckEnaleHotkey();
						break;
					}
				case IDM_AUTO_FUNC:
					{
						OnBnClickedCheckAutoRun();
						break;
					}
				default:
					WRITE_LOG(L"unknown ret [%d]",iId);
				}
			}
			break;
		}
	case WM_LBUTTONUP:
		{
			this->ShowWindow(SW_SHOWNORMAL);
			SetForegroundWindow();
			break;
		}
	}
	return 0;
}

void CautoselectDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);
	if(nType == SIZE_MINIMIZED)
	{
		ShowWindow(SW_HIDE);
	}
	// TODO: Add your message handler code here
}

void CautoselectDlg::OnDestroy()
{
	Shell_NotifyIcon(NIM_DELETE,&m_notify);
	CDialog::OnDestroy();
	// TODO: Add your message handler code here
}
void CautoselectDlg::UpdateTip()
{
	_tcscpy(m_notify.szTip,GetCurrentState());
	Shell_NotifyIcon(NIM_MODIFY,&m_notify);
}
void CautoselectDlg::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	ShowWindow(SW_HIDE);
	return;
	CDialog::OnClose();
}

void CautoselectDlg::RegisteHotKey()
{
	if(!m_bEnable)
	{
		if(!RegisterHotKey(m_hWnd,IDH_HOT_KEY_ALT,MOD_ALT,VK_MENU))
		{
			WRITE_LOG(L"RegisterHotKey Error gle[%d]",GetLastError());
			return;
		}
	}
	else
	{
		if(!UnregisterHotKey(m_hWnd,IDH_HOT_KEY_ALT))
		{
			WRITE_LOG(L"UnregisterHotKey Error gle[%d]",GetLastError());
			return;
		}
	}
	m_bEnable = !m_bEnable;
	UpdateTip();
}
void CautoselectDlg::OnBnClickedCheckEnaleHotkey()
{
	// TODO: Add your control notification handler code here
	RegisteHotKey();
	((CButton*)GetDlgItem(IDC_CHECK_ENALE_HOTKEY))->SetCheck(m_bEnable ? BST_CHECKED : BST_UNCHECKED);
	PostMessage(WM_PAINT);
}

void CautoselectDlg::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
{
	// TODO: Add your message handler code here and/or call default
	if(nHotKeyId == IDH_HOT_KEY_ALT)
	{
		if(nKey2 == VK_MENU)
		{
			static DWORD dwPreTime = 0;
			DWORD dwNow = GetCurrentTime();
			if(dwNow - dwPreTime < 500)
			{
				// 500 ms 内重复点击了alt 键,我们认为是双击alt,在光标的当前位置双击
				//mouse_event(MOUSEEVENTF_LEFTDOWN,)
				AfxMessageBox(_T("alt double press"));
				dwPreTime = 0;// 重置
			}
			else
			{
				dwPreTime = dwNow;
			}
		}
	}
	CDialog::OnHotKey(nHotKeyId, nKey1, nKey2);
}

展开阅读全文

没有更多推荐了,返回首页