#include "stdafx.h"
...
const HOTKEY = 'T'; // <WinKey>+T is hotkey to activate
static void SendString(LPCTSTR str);
class CStaticAbbrev : public CStatic {
protected:
afx_msg UINT OnGetDlgCode() {
return DLGC_WANTCHARS; // I want all chars
}
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
DECLARE_MESSAGE_MAP()
};
class CMyDialog : public CDialog {
public:
CMyDialog(CWnd* pParent = NULL);
~CMyDialog();
protected:
...
CStatic m_wndHelp; // help message
CButton m_wndOK; // OK/Exit button
CStaticAbbrev m_wndKeys;// static text to display/receive abbreviations
UINT m_nIDHotKey; // hot key identifier
BOOL m_bFirstTime; // first time activated?
afx_msg LRESULT OnHotKey(WPARAM wp, LPARAM lp);
DECLARE_MESSAGE_MAP()
};
struct ABBREV {
TCHAR key;
LPCTSTR text;
} MYABBREVS[] = {
{ _T('n'),_T("Elmer Fudd") }, // name
{ _T('a'),_T("1 Bunny Way") }, // addr
{ _T('c'),_T("Redmond") }, // city
{ _T('p'),_T("206 555 1212") }, // phone
{ 0,NULL},
};
BEGIN_MESSAGE_MAP(CStaticAbbrev, CStatic)
ON_WM_GETDLGCODE()
ON_WM_CHAR()
END_MESSAGE_MAP()
void CStaticAbbrev::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) {
for (int i=0; MYABBREVS[i].key; i++) {
if (nChar==MYABBREVS[i].key) { // found:
GetParent()->ShowWindow(SW_HIDE); // ..hide dialog
SendString(MYABBREVS[i].text); // ..and send text
return;
}
}
MessageBeep(0);
}
LRESULT CMyDialog::OnHotKey(WPARAM wp, LPARAM lp) {
if (wp==m_nIDHotKey && !IsWindowVisible()) {
if (m_bFirstTime) {
// first activation: replace initial message with abbrevs list
CString text;
for (int i=0; MYABBREVS[i].key; i++) {
CString temp;
temp.Format(_T("%c = %s\n"), MYABBREVS[i].key,
MYABBREVS[i].text);
text += temp;
}
m_wndKeys.SetWindowText(text);
m_wndHelp.SetWindowText(
_T("Type one of the keys below to enter text."));
m_wndOK.SetWindowText(_T("E&xit"));
m_bFirstTime = FALSE;
}
ShowWindow(SW_NORMAL); // activate myself
m_wndKeys.SetFocus(); // set focus to abbrev control
}
return 0;
}
static void SendString(LPCTSTR str) {
INPUT inp[2];
memset(inp,0,sizeof(INPUT));
inp[0].type = INPUT_KEYBOARD;
inp[0].ki.dwFlags = KEYEVENTF_UNICODE; // to avoid shift, and so on
inp[1] = inp[0];
inp[1].ki.dwFlags |= KEYEVENTF_KEYUP;
for (LPCTSTR p=str; *p; p++) {
inp[0].ki.wScan = inp[1].ki.wScan = *p;
SendInput(2, inp, sizeof(INPUT));
}
}
Figure 4 ExitWindowsEx Flags
| Shutdown Type | Description |
|---|---|
| EWX_LOGOFF | Shuts down all processes, then logs the user off |
| EWX_POWEROFF | Shuts down the system and turns off the power |
| EWX_REBOOT | Restarts the system |
| EWX_SHUTDOWN | Performs shutdown so it's safe to turn off the power |
| Modifiers | Description |
| EWX_FORCE | Force-terminates processes without sending WM_QUERYENDSESSION/WM_ENDSESSION |
| EWX_FORCEIFHUNG | Force-terminates processes if they don't respond to WM_QUERYENDSESSION/WM_ENDSESSION |