📄 warcraftkeydlg.cpp
字号:
// WarcraftKeyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WarcraftKey.h"
#include "WarcraftKeyDlg.h"
#include ".\warcraftkeydlg.h"
#include "IconMenu.h"
#define GETHIWORD(p) (WORD) ((p&0xFFFF0000)>>16)
#define GETLOWORD(p) (WORD) (p&0xFFFF)
int num_i;
datastruct datain[20];
//UINT WM_SHOWTASK;
const int g_nKeyCodeList[]=
{
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x80, 0x8a
};
const char* const g_strKeyCodeList[]=
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"Num0", "Num1", "Num2", "Num3", "Num4", "Num5", "Num6", "Num7", "Num8", "Num9",
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"
};
inline int g_GetIdxByKeyValue(int Value)
{
for(int i=0; i<sizeof(g_nKeyCodeList)/ sizeof(g_nKeyCodeList[0]); i++)
{
if (g_nKeyCodeList[i]== Value)
{
return i;
}
}
return 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()
/////////////////////////////////////////////////////////////////////////////
// CWarcraftKeyDlg dialog
CWarcraftKeyDlg::CWarcraftKeyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWarcraftKeyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWarcraftKeyDlg)
m_nDest = -1;
m_nSrc = -1;
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWarcraftKeyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWarcraftKeyDlg)
DDX_Control(pDX, IDC_MAP_KEY, m_ctlMapKey);
DDX_Control(pDX, IDC_DEST_KEY, m_ctlDest);
DDX_Control(pDX, IDC_SRC_KEY, m_ctlSrc);
DDX_CBIndex(pDX, IDC_DEST_KEY, m_nDest);
DDX_CBIndex(pDX, IDC_SRC_KEY, m_nSrc);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWarcraftKeyDlg, CDialog)
//{{AFX_MSG_MAP(CWarcraftKeyDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_DELETE_KEY, OnDeleteKey)
ON_BN_CLICKED(IDC_MAKE_KEY, OnMakeKey)
ON_WM_SYSCOMMAND()
ON_WM_CLOSE()
ON_BN_CLICKED(ID_ABOUT, OnAbout)
ON_MESSAGE(WM_SHOWTASK,onShowTask)
//}}AFX_MSG_MAP
//ON_MESSAGE(WM_SHOWTASK,onShowTask)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWarcraftKeyDlg message handlers
BOOL CWarcraftKeyDlg::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);
}
}
int VirKey[]= {VK_HOME, VK_END,VK_TAB };
for(int i=0; i<3; ++i)
{
char buff[20];
sprintf(buff, "Key%02d", i);
int ID= GlobalAddAtom(buff);
m_IdList.push_back(ID);
RegisterHotKey(GetSafeHwnd(), ID, 0, VirKey[i]);
}
OnInitKeyList();
//m_PressReturn = TRUE;
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
num_i=0;
return TRUE; // return TRUE unless you set the focus to a control
}
void CWarcraftKeyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else if ((nID & 0xFFF0) == SC_MINIMIZE )
{
//ShowWindow(SW_HIDE);
//Shell_NotifyIcon(NIM_ADD,&m_traydata);
toTray();
}
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 CWarcraftKeyDlg::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();
}
}
HCURSOR CWarcraftKeyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWarcraftKeyDlg::OnDeleteKey()
{
// TODO: Add your control notification handler code here
int nSel = m_ctlMapKey.GetCurSel();
if (nSel<0)
return;
int k = 0;
for(CIntMap::iterator i = m_KeyMap.begin(); i!=m_KeyMap.end() && k < nSel; i++, k++)
{
int m = k;
};
UnregisterHotKey(GetSafeHwnd(), i->second.m_nRegID);
m_ctlMapKey.DeleteString(k);
m_KeyMap.erase(i);
}
LRESULT CWarcraftKeyDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if (message== WM_HOTKEY)
{
OnHotKeyMsg(wParam, lParam);
return 0;
}
return CDialog::WindowProc(message, wParam, lParam);
}
VOID CWarcraftKeyDlg::OnInitKeyList()
{
for(int i=0; i<sizeof(g_strKeyCodeList)/ sizeof(g_strKeyCodeList[0]); ++i)
{
m_ctlSrc.InsertString(i, g_strKeyCodeList[i]);
m_ctlDest.InsertString(i, g_strKeyCodeList[i]);
}
m_ctlDest.SetCurSel(0);
m_ctlSrc.SetCurSel(0);
}
VOID CWarcraftKeyDlg::OnMakeKey()
{
// TODO: Add your control notification handler code here
UpdateData();
int SrcCode = g_nKeyCodeList[m_nSrc];
int DestCode= g_nKeyCodeList[m_nDest];
if ( m_KeyMap.find(SrcCode) != m_KeyMap.end())
{
return;
}
int ID= GlobalAddAtom(g_strKeyCodeList[m_nSrc]);
RegisterHotKey(GetSafeHwnd(), ID, 0, SrcCode);
tagKey key;
key.m_nDestKey = DestCode;
key.m_nRegID = ID;
m_KeyMap.insert(std::make_pair(SrcCode, key));
datain[num_i].DestCode_ = DestCode;
datain[num_i].SrcCode_ = SrcCode;
datain[num_i].SrcStr = g_strKeyCodeList[m_nSrc];
num_i++;
OnUpdateKeyBox();
}
VOID CWarcraftKeyDlg::RevertData()
{
for(int i=0;i<num_i;++i)
{
int SrcCode = datain[i].SrcCode_;
int DestCode= datain[i].DestCode_;
int ID= GlobalAddAtom(datain[i].SrcStr);
RegisterHotKey(GetSafeHwnd(), ID, 0, SrcCode);
tagKey key;
key.m_nDestKey = DestCode;
key.m_nRegID = ID;
m_KeyMap.insert(std::make_pair(SrcCode, key));
}
}
VOID CWarcraftKeyDlg::OnHotKeyMsg(WPARAM wParam, LPARAM lParam)
{
WORD Key= GETHIWORD(lParam);
if(Key == VK_TAB)
{
HWND hWnd= ::GetActiveWindow();
m_PressReturn =!m_PressReturn;
//MessageBox("dd","dd");
}
if(m_PressReturn)
{
RevertData();
switch(Key)
{
case VK_HOME:
{
HWND hWnd= ::GetActiveWindow();
if (m_bShowSelfHP)
{
keybd_event(0xDD, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
else
{
keybd_event(0xDD, 0, KEYEVENTF_EXTENDEDKEY, 0);
}
m_bShowSelfHP = ! m_bShowSelfHP;
}
break;
case VK_END:
{
HWND hWnd= ::GetActiveWindow();
if (m_bShowEmenyHP)
{
keybd_event(0xDB, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
else
{
keybd_event(0xDB, 0, KEYEVENTF_EXTENDEDKEY, 0);
}
m_bShowEmenyHP = ! m_bShowEmenyHP;
}
break;
default:
{
CIntMap::iterator i= m_KeyMap.find(Key);
if (i != m_KeyMap.end())
{
keybd_event(i->second.m_nDestKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(i->second.m_nDestKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
}
}
else
{
HWND hWnd= ::GetActiveWindow();
for(CIntMap::iterator j= m_KeyMap.begin(); j!= m_KeyMap.end(); ++j)
{
UnregisterHotKey(GetSafeHwnd(), j->second.m_nRegID);
}
}
}
VOID CWarcraftKeyDlg::OnUpdateKeyBox()
{
m_ctlMapKey.ResetContent();
char buff[200];
int n = 0;
for( CIntMap::iterator i = m_KeyMap.begin();
i!= m_KeyMap.end(); ++i)
{
sprintf(buff, "%s -> %s", g_strKeyCodeList[g_GetIdxByKeyValue(i->first)], g_strKeyCodeList[g_GetIdxByKeyValue(i->second.m_nDestKey)]);
m_ctlMapKey.InsertString(n++, buff);
}
UpdateData(FALSE);
}
void CWarcraftKeyDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
for(int i=0; i<m_IdList.size(); ++i)
{
UnregisterHotKey(GetSafeHwnd(), m_IdList[i]);
}
for(CIntMap::iterator j= m_KeyMap.begin(); j!= m_KeyMap.end(); ++j)
{
UnregisterHotKey(GetSafeHwnd(), j->second.m_nRegID);
}
CDialog::OnClose();
}
void CWarcraftKeyDlg::OnAbout()
{
// TODO: Add your control notification handler code here
CAboutDlg dlg;
dlg.DoModal();
}
void CWarcraftKeyDlg::toTray()
{
NOTIFYICONDATA nid;
nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd=this->m_hWnd;
nid.uID=IDR_MAINFRAME;
nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;
nid.uCallbackMessage=WM_SHOWTASK;//自定义的消息名称
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(nid.szTip,"改键工具");//信息提示条为“计划任务提醒”
Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
ShowWindow(SW_HIDE);//隐藏主窗口
}
LRESULT CWarcraftKeyDlg::onShowTask(WPARAM wParam,LPARAM lParam)
//wParam接收的是图标的ID,而lParam接收的是鼠标的行为
{
//MessageBox("dd");
if(wParam!=IDR_MAINFRAME)
return 1;
switch(lParam)
{
case WM_RBUTTONUP://右键起来时弹出快捷菜单,这里只有一个“关闭”
{
//LPPOINT lpoint=new tagPOINT;
//::GetCursorPos(lpoint);//得到鼠标位置
//CMenu menu;
//menu.CreatePopupMenu();//声明一个弹出式菜单
////增加菜单项“关闭”,点击则发送消息WM_DESTROY给主窗口(已
////隐藏),将程序结束。
//menu.AppendMenu(MF_STRING,WM_DESTROY,"关闭");
////确定弹出式菜单的位置
//menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
////资源回收
//HMENU hmenu=menu.Detach();
//menu.DestroyMenu();
//delete lpoint;
//CPoint m_point;
CPoint m_point;
::GetCursorPos(&m_point);
CIconMenu* m_submenu = (CIconMenu*)m_menu.GetSubMenu(0);
m_submenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, m_point.x,m_point.y,AfxGetApp()->m_pMainWnd,TPM_LEFTALIGN);
}
break;
case WM_LBUTTONDOWN://单击左键的处理
{
ShowWindow(SW_SHOW);//简单的显示主窗口完事儿
//CWarcraftKeyDlg dlg;
//MessageBox("dd","dd");
//dlg.DoModal();
}
break;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -