VC++中,按钮属于CButton类,它继承自CWnd这个窗口基类。
Button控件的Disable属性是指使按钮变成灰色,即失效。由于按钮属于一种窗口,也就是窗口失效。在CWnd中有如下成员函数:
BOOL EnableWindow(BOOL bEnable = TRUE
);
微软帮助文档给出的说明主要意思为:如果参数为TRUE(默认),则窗口有效,为FALSE窗口失效。该函数调用会返回之前窗口的状态。
对于指定的按钮控件 ,可以自己指定它的 ID 或 用系统的 默认ID 这时使用 CWnd::GetDlgItem根据这个ID可以获取按钮(实际是CWnd)的指针。此时直接使用该指针调用CWnd的EnableWindow函数参数为FALSE,如下:
GetDlgItem(按钮ID) -> EnableWindow( FALSE );
//在按钮对应对话框的窗口类中这
//样用,不是对应的窗口类,要先获取按钮所属的窗口
注意按钮CButton类的SetState并不能使按钮失效,而是模拟鼠标单击消息,该函数调用结束会恢复原始状态,即瞬间状态,之后会恢复原来状态。如:
reinterpret_cast<CButton>( 按钮ID ) ->SetState( 1 );
//只是暂时设置为按下(不是失效),此时会发送WM_LBUTTONDOWN消息,该函数调用结 //束就会像松开鼠标的按钮一样
GetDlgItem有多个重载版本,注意调用方式。
如Win32版本的:
HWND hButton;
::GetDlgItem( hButton , 按钮ID );//获取按钮句柄
::EnableWindow( hButton , FALSE );//设置按钮为无效
你需要使用下面的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,
}
楼上的别乱教 GetDlgltem是用来获取子窗口或者控件的指针的
关于如何在MFC中获取控件句柄
在win32利用API来编程时 我们是通过hwnd = CreateWindow( )来建立一个窗体,同时返回一个句柄
MFC中的窗口类和控件类都是又一个基类CWnd派生出来的
MFC里封装了CreateWindow( )的过程
所以CWnd类本来自己就有一个句柄成员为m_hWnd
所以只要获得了窗口或者控件的指针 就可以调用成员变量m_hWnd来获取句柄
例如在当前窗口或者控件下 *** 作 就可以直接用this指针 this->m_hWnd
另外 GetSafeHwnd 这个函数也可以试试 具体情况具体分析
具体用法看MSDN吧
记住一点的是 MFC中的消息映射的过程就是通过指针与句柄的映射来实现的
以上就是关于创建一个窗口,里面有一个button控件,当点击botton时,d出另外一个窗口,怎么实现全部的内容,包括:创建一个窗口,里面有一个button控件,当点击botton时,d出另外一个窗口,怎么实现、c#如何检测其他的软件的按键按下、mfc 获取按钮句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)