Windows CE 3.0 及以后版本均可用!
此控件显示一文本框用户于捕获用户按键,常用于程序的设置功能(设置HotKey)。
可捕获除关机键外的任何按键。
用法:
CMS_HotKeyEdit::RegisterClass(hInstance); //注册窗口类,之后就可以用CreateWindow创建控件了。
#define MSHKM_GETKEY WM_USER+101 //获取字符
#define MSHKM_SETKEY WM_USER+102 //设置字符 wParam为字符
/*
* HotKey Control
* 2009.04.20 By Frank
* Mood Sunshiny Workroom
* Http://Www.MoodSun.Cn/
*
* Default Style(For Custom Control): 0x50810080
* ClassName: MS_HotKeyEdit
*/
static class CMS_HotKeyEdit
{
private:
static WNDPROC m_OldWinProc; //原窗口处理过程
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
public:
static BOOL RegisterClass(HINSTANCE hInstance);
};
#define MSHKM_SETKEY WM_USER+102 //设置字符 wParam为字符
/*
* HotKey Control
* 2009.04.20 By Frank
* Mood Sunshiny Workroom
* Http://Www.MoodSun.Cn/
*
* Default Style(For Custom Control): 0x50810080
* ClassName: MS_HotKeyEdit
*/
static class CMS_HotKeyEdit
{
private:
static WNDPROC m_OldWinProc; //原窗口处理过程
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
public:
static BOOL RegisterClass(HINSTANCE hInstance);
};
#include "StdAfx.h"
#include "MS_HotKeyEdit.h"
#include <gx.h>
#pragma comment(lib,"gx.lib")
WNDPROC CMS_HotKeyEdit::m_OldWinProc=NULL;
LRESULT CALLBACK CMS_HotKeyEdit::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE:
{
/*设置只读*/
LONG lOldLong=GetWindowLong(hwnd,GWL_STYLE);
if((lOldLong&ES_READONLY)!=ES_READONLY)SetWindowLong(hwnd,GWL_STYLE,lOldLong|ES_READONLY);
break;
}
case WM_SETFOCUS:
GXOpenInput(); //接管硬件按键
break;
case WM_KILLFOCUS:
GXCloseInput();
break;
case WM_KEYUP:
{
//通知父窗口内容变化
HWND hParent=NULL;
if(!(hParent=GetParent(hwnd)))
{
throw TEXT("此控件必须做为子控件!");
}
NMHDR nmd={hwnd,GetDlgCtrlID(hwnd),(DWORD)wParam};
LRESULT lRS=SendMessage(hParent,WM_NOTIFY,nmd.idFrom,(LPARAM)&nmd);
//给父窗口一个机会决定是否允许此HotKey
if(lRS==S_OK)
{
//设置新值到文本
TCHAR tcTxt[2]={0,0};
tcTxt[0]=(TCHAR)wParam;
SetWindowText(hwnd,tcTxt);
}
break;
}
case MSHKM_GETKEY:
{
TCHAR tcTxt[2]={0,0};
GetWindowText(hwnd,tcTxt,2);
return (LRESULT)(tcTxt[0]==TEXT('无')?0:tcTxt[0]);
}
case MSHKM_SETKEY:
{
TCHAR tcTxt[2]={0,0};
tcTxt[0]=wParam==0?TEXT('无'):(TCHAR)wParam;
SetWindowText(hwnd,tcTxt);
return S_OK;
}
}
return CallWindowProc(m_OldWinProc,hwnd,uMsg,wParam,lParam);
}
BOOL CMS_HotKeyEdit::RegisterClass(HINSTANCE hInstance)
{
WNDCLASS wc;
if(!GetClassInfo(hInstance,TEXT("EDIT"),&wc))return FALSE;
m_OldWinProc=wc.lpfnWndProc;
wc.hInstance=hInstance;
wc.lpszClassName = TEXT("MS_HotKeyEdit");
wc.lpfnWndProc = WindowProc;
// 注册自定义类
return (::RegisterClass(&wc) != 0);
}
#include "MS_HotKeyEdit.h"
#include <gx.h>
#pragma comment(lib,"gx.lib")
WNDPROC CMS_HotKeyEdit::m_OldWinProc=NULL;
LRESULT CALLBACK CMS_HotKeyEdit::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE:
{
/*设置只读*/
LONG lOldLong=GetWindowLong(hwnd,GWL_STYLE);
if((lOldLong&ES_READONLY)!=ES_READONLY)SetWindowLong(hwnd,GWL_STYLE,lOldLong|ES_READONLY);
break;
}
case WM_SETFOCUS:
GXOpenInput(); //接管硬件按键
break;
case WM_KILLFOCUS:
GXCloseInput();
break;
case WM_KEYUP:
{
//通知父窗口内容变化
HWND hParent=NULL;
if(!(hParent=GetParent(hwnd)))
{
throw TEXT("此控件必须做为子控件!");
}
NMHDR nmd={hwnd,GetDlgCtrlID(hwnd),(DWORD)wParam};
LRESULT lRS=SendMessage(hParent,WM_NOTIFY,nmd.idFrom,(LPARAM)&nmd);
//给父窗口一个机会决定是否允许此HotKey
if(lRS==S_OK)
{
//设置新值到文本
TCHAR tcTxt[2]={0,0};
tcTxt[0]=(TCHAR)wParam;
SetWindowText(hwnd,tcTxt);
}
break;
}
case MSHKM_GETKEY:
{
TCHAR tcTxt[2]={0,0};
GetWindowText(hwnd,tcTxt,2);
return (LRESULT)(tcTxt[0]==TEXT('无')?0:tcTxt[0]);
}
case MSHKM_SETKEY:
{
TCHAR tcTxt[2]={0,0};
tcTxt[0]=wParam==0?TEXT('无'):(TCHAR)wParam;
SetWindowText(hwnd,tcTxt);
return S_OK;
}
}
return CallWindowProc(m_OldWinProc,hwnd,uMsg,wParam,lParam);
}
BOOL CMS_HotKeyEdit::RegisterClass(HINSTANCE hInstance)
{
WNDCLASS wc;
if(!GetClassInfo(hInstance,TEXT("EDIT"),&wc))return FALSE;
m_OldWinProc=wc.lpfnWndProc;
wc.hInstance=hInstance;
wc.lpszClassName = TEXT("MS_HotKeyEdit");
wc.lpfnWndProc = WindowProc;
// 注册自定义类
return (::RegisterClass(&wc) != 0);
}