MFC的CButton类,当然可以接收消息,只是由于MFC对它的封装度较高,所以稍微麻烦一些罢了。
其实有个简单的办法实现你的需求,就是使用位图按钮。关注如下函数:
CBitmapButton::LoadBitmaps 你可以设置四个状态的位图,这样就可以有焦点效果。
如果以上不能满足你的需求,还可以通过SubclassDlgItem子类化一个自定义的按钮或者高版本中的CButtonST等方式实现。子类化自定义按钮示例:
C语言的话
你可以选择利用 getchar()
你一按回车 就可以执行你 getchar(); 后面的代码了。
比如
#include <stdioh>
main(){
getchar();
printf("1");
}
你运行后 一开始什么都没有 你按下回车 就能把 1 显示出来。
要在view中添加按钮,就只能动态创建了,先new一个CButton对象,然后调用成员Create创建这个按钮。Create中有一个参数ID,你可以在消息映射中添加ON_COMMAND(ID, func) 来相应按钮的单机消息。
可以用FlexibleButton表示。
FlexibleButton 是一个基于标准 C 语言的小巧灵活的按键处理库,支持单击、连击、短按、长按、自动消抖,可以自由设置组合按键,可用于中断和低功耗场景。
该按键库解耦了具体的按键硬件结构,理论上支持轻触按键与自锁按键,并可以无限扩展按键数量。另外,FlexibleButton 使用扫描的方式一次性读取所有所有的按键状态,然后通过事件回调机制上报按键事件。
核心的按键扫描代码仅有三行,没错,就是经典的 三行按键扫描算法。使用 C 语言标准库 API 编写,也使得该按键库可以无缝兼容任意的处理器平台,并且支持任意 OS 和 non-OS(裸机编程)。
快速体验:
FlexibleButton 库中提供了一个测试例程 /examples/demo_rtt_iotboardc,该例程基于 RT-Thread OS 进行测试,硬件平台选择了 RT-Thread IoT Board Pandora v251 开发板。
当然你可以选择使用其他的 OS,或者使用裸机测试,只需要移除 OS 相关的特性即可。
如果你使用自己的硬件平台,只需要将 FlexibleButton 库源码和例程加入你既有的工程下即可。
DEMO 程序说明:
该示例程序可以直接在 RT-Thread stm32l475-atk-pandora BSP 中运行,可以在该 BSP 目录下,使用 menuconfig 获取本软件包。
已经ID的情况下
int GetDlgItemText( int nID, LPTSTR lpStr, int nMaxCount ) const;
{int GetDlgItemText( int nID, CString& rString ) const;}
或
先CWnd GetDlgItem( int nID ) const;得到该按钮的CWnd指针,然后
int GetWindowText( LPTSTR lpszStringBuf, int nMaxCount ) const;
{void GetWindowText( CString& rString ) const;}
如果该按钮的ID已经绑定了一个CButton对象,如CButton abc;那就可以之间调用GetWindowText了。
你需要使用下面的API函数,并建立一个需要检测的按键 List。
把检测按键放到 Timer 里面每秒检测 30 次以上就能监听全局按键。
API函数
/// <summary>
/// GetAsyncKeyState函数返回的是指定虚拟键瞬时的状态值,它有四种返回值:
///<para></para>0---键未被按下,而且键也未被按过(自上次调用GetAsyncKeyState后);
///<para></para>1---键抬起瞬间,在此之前键曾经被按过;
///<para></para>-32768(即16进制数&H8000) --- 键按下瞬间,但在此之前键未被按过;
///<para></para>-32767(即16进制数&H8001) --- 键按下持续,在此之前键也曾经被按过。
/// </summary>
[SystemRuntimeInteropServicesDllImport("user32dll")]
public static extern int GetAsyncKeyState ( int vKey );
/// <summary>GetCursorPos函数返回鼠标位置</summary>
[SystemRuntimeInteropServicesDllImport("user32dll")]
public static extern int GetCursorPos (ref MOUSEPOINT lpPoint );
/// <summary>返回指定虚拟码的按键状态。
/// <para>参见〈<see cref="EKeyState"/>〉</para>
/// </summary>
public int 获取按键状态 ( int 键) { return GetAsyncKeyState(键); }
/// <summary>获取并返回鼠标位置</summary>
public MOUSEPOINT 鼠标位置() {
MOUSEPOINT p = new MOUSEPOINT();
var k = 0;
k = GetCursorPos(ref p);
return p;
}
/// <summary>表示鼠标在屏幕位置的结构,为了使用API函数而定制</summary>
[StructLayout(LayoutKindSequential)]
public struct MOUSEPOINT{
public int X;
public int Y;
public MOUSEPOINT(int x, int y) {
thisX = x;
thisY = y;
}
}
键位表
public enum EKey {
//鼠标b
None = 0,
鼠标左键 = 1, LButton = 0x1,
鼠标右键 = 2, RButton = 0x2,
取消 = 3, Cancel = 0x3,
鼠标中键 = 4, MButton = 0x4,
鼠标侧键1 = 5, XButton1 = 0x5,
鼠标侧键2 = 6, XButton2 = 0x6,
//功能键
退格 = 8, Back = 0x8,
跳格 = 9, Tab = 0x9,
LineFeed = 0xA, //10,
Clear = 0xC, //12,
回车 = 13, Enter = 0xD,
退出 = 27, Esc = 0x1B,
空格 = 32, Space = 0x20,
CapsLock = 0x14,
ScrollLock = 0x91,
NumLock = 0x90,
//组合键
Modifiers = -65536,
按Shift = 16, Shift = 0x10,
按Ctrl = 17, Ctrl = 0x11,
按Alt = 18, Menu = 0x12, /ALT键码名为Menu/
按住Shift = 65536, //0x10000,
按住Ctrl = 131072, //0x20000,
按住Alt = 262144, //0x40000,
左Shift = 160, LShift = 0xA0,
右Shift = 161, RShift = 0xA1,
左Ctrl = 162, LControl = 0xA2,
右Ctrl = 163, RControl = 0xA3,
左Alt = 164, LMENU = 0xA4,
右Alt = 165, RMENU = 0xA5,
左Win = 91, LWIN = 0x5B,
右Win = 92, RWIN = 0x5C,
目录 = 93, Apps = 0x5D,
//键盘上部
分号 = 186, 冒号 = 186, OEMSemicolon = 0xBA, OEM1 = 0xBA, / ; : /
等号 = 187, 加号 = 187, OEMPlus = 0xBB, / = + /
逗号 = 188, 小于号 = 188, OEMComma = 0xBC, / , < /
减号 = 189, 下划线 = 189, OEMinus = 0xBD, / = + /
句号 = 190, 大于号 = 190, OEMPeriod = 0XBE, / > /
斜杠 = 191, 问号 = 191, OEMQuestion = 0xBF, OEM2 = 0xBF, / / /
重音 = 192, 波浪 = 192, OEMtilde = 0xC0, OEM3 = 0xC0, / ` ~ /
左方括号 = 219, 左大括号 = 219, OEMOpenBreackets = 0xDB, OEM4 = 0xDB, / [ { /
反斜杠 = 220, 竖线 = 220, OEMPipe = 0xDC, OEM5 = 0xDC, / \ | /
右方括号 = 221, 右大括号 = 221, OEMCloseBreackets = 0xDD, OEM6 = 0xDD, / ] } /
引号 = 222, 双引号 = 222, 单引号 = 222, OEMQuptes = 0xDE, OEM7 = 0xDE, / ' " /
尖括号 = 223, OEMBackslash = 0xDF, / 102键盘上的尖括号或反斜杠键 /
//键盘中部
截屏 = 44, PrintScreen = 0x2C,
暂停 = 19, Pause = 0x13, Break = 0x13, /BREAK为自定义/
休眠 = 95, Sleep = 0x5F,
Insert = 0x2D,
Home = 0x24,
End = 0x23,
Delete = 0x2E,
PageDown = 0x22,
PageUp = 0x21,
左键 = 37, LEFT = 0x25,
上键 = 38, UP = 0x26,
右键 = 39, RIGHT = 0x27,
下键 = 40, DOWN = 0x28,
//小键盘
侧0 = 96, Numpad0 = 0x60, NumpadInsert = 96, /NumpadInsert为自定义/
侧1 = 97, Numpad1 = 0x61, NumpadDelete = 97, /NumpadDelete为自定义/
侧2 = 98, Numpad2 = 0x62, NumpadDown = 98, /NumpadDown为自定义/
侧3 = 99, Numpad3 = 0x63,
侧4 = 100, Numpad4 = 0x64, NumpadLeft = 100, /NumpadLeft为自定义/
侧5 = 101, Numpad5 = 0x65,
侧6 = 102, Numpad6 = 0x66, NumpadRight = 102, /NumpadRight为自定义/
侧7 = 103, Numpad7 = 0x67,
侧8 = 104, Numpad8 = 0x68, NumpadUp = 104, /NumpadUp为自定义/
侧9 = 105, Numpad9 = 0x69,
侧乘 = 106, Mutiply = 0x6A,
侧加 = 107, Add = 0x6B,
侧回车 = 108, Separator = 0x6C,
侧减 = 109, Subtract = 0x6D,
侧点 = 110, Decimal = 0x6E,
侧除 = 111, Divide = 0x6F,
//功能键
F1 = 0x70,
F2 = 0x71,
F3 = 0x72,
F4 = 0x73,
F5 = 0x74,
F6 = 0x75,
F7 = 0x76,
F8 = 0x77,
F9 = 120,
F10 = 0x79,
F11 = 0x7A,
F12 = 0x7B,
F13 = 0x7C,
F14 = 0x7D,
F15 = 0x7E,
F16 = 0x7F,
F17 = 0x80,
F18 = 0x81,
F19 = 130,
F20 = 0x83,
F21 = 0x84,
F22 = 0x85,
F23 = 0x86,
F24 = 0x87,
//数字字母
数0 = 48, D0 = 0x30,
数1 = 49, D1 = 0x31,
数2 = 50, D2 = 0x32,
数3 = 51, D3 = 0x33,
数4 = 52, D4 = 0x34,
数5 = 53, D5 = 0x35,
数6 = 54, D6 = 0x36,
数7 = 55, D7 = 0x37,
数8 = 56, D8 = 0x38,
数9 = 57, D9 = 0x39,
A = 65, //0x41,
B = 66, //0x42,
C = 67, //0x43,
D = 68, //0x44,
E = 69, //0x45,
F = 70, //0x46,
G = 71, //0x47,
H = 72, //0x48,
I = 73, //0x49,
J = 74, //0x4A,
K = 75, //0x4B,
L = 76, //0x4C,
M = 77, //0x4D,
N = 78, //0x4E,
O = 79, //0x4F,
P = 80, //0x50,
Q = 81, //0x51,
R = 82, //0x52,
S = 83, //0x53,
T = 84, //0x54,
U = 85, //0x55,
V = 86, //0x56,
W = 87, //0x57,
X = 88, //0x58,
Y = 89, //0x59,
Z = 90, //0x5A,
//其他
Attn = 0xF6,
BrowserBack = 0xA6,
BrowserFavorites = 0xAB,
BrowserForward = 0xA7,
BrowserHome = 0xAC,
BrowserRefresh = 0xA8,
BrowserSearch = 170,
BrowserStop = 0xA9,
Crsel = 0xF7,
EraseEof = 0xF9,
Exsel = 0xF8,
FinalMode = 0x18,
HanguelMode = 0x15,
HangulMode = 0x15,
HanjaMode = 0x19,
帮助 = 47, Help = 0x2F,
IMEAccept = 30,
IMEAceept = 30,
IMEConvert = 0x1C,
IMEModeChange = 0x1F,
IMENonconvert = 0x1D,
JunjaMode = 0x17,
KanaMode = 0x15,
KanjiMode = 0x19,
KeyCode = 0xFFFF,
LaunchApplication1 = 0xB6,
LaunchApplication2 = 0xB7,
LaunchMail = 180,
音量减小 = 0xAE,
音量禁用 = 0xAD,
音量放大 = 0xAF,
选择 = 41, Select = 0x29,
打印 = 42, Print = 0x2A,
Execute = 0x2B,
MediaSelect = 0xB5,
MediaNextTrack = 0xB0,
MediaPlayPause = 0xB3,
MediaPreviousTrack = 0xB1,
MediaStop = 0xB2,
播放 = 250, Play = 0xFA,
OemBackslash = 0xE2,
OemClear = 0xFE,
Pa1 = 0xFD,
Packet = 0xE7,
Prior = 0x21,
ProcessKey = 0xE5,
NoName = 0xFC,
Zoom = 0xFB,
}
以上就是关于MFC 按钮能够接受到消息吗,如果接受不到消息,那么怎么让当鼠标移动到按钮上时让按钮变色或者高亮全部的内容,包括:MFC 按钮能够接受到消息吗,如果接受不到消息,那么怎么让当鼠标移动到按钮上时让按钮变色或者高亮、c语言 按钮 执行语句、mfc 怎么在cview类中添加按钮,并让这个按钮响应消息,求指导等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)