[收藏]WINFORM(C#)的键盘测试
来源:互联网 发布:大溪地黑珍珠 知乎 编辑:程序博客网 时间:2024/05/15 05:10
using System;
using System.Drawing;
using System.Windows.Forms;
public class KeyExamine: Form
{
public static void Main()
{
Application.Run(new KeyExamine());
}
enum EventType
{
None, KeyDown, KeyUp, KeyPress
}
struct KeyEvent
{
public EventType evttype;
public EventArgs evtargs;
}
const int iNumLines = 25;
int iNumValid = 0;
int iInsertIndex = 0;
KeyEvent[] akeyevt = new KeyEvent[iNumLines];
int xEvent, xChar, xCode, xMods, xData,
xShift, xCtrl, xAlt, xRight;
public KeyExamine()
{
Text = "Key Examine";
BackColor = SystemColors.Window;
ForeColor = SystemColors.WindowText;
xEvent = 0;
xChar = xEvent + 5 * Font.Height;
xCode = xChar + 5 * Font.Height;
xMods = xCode + 8 * Font.Height;
xData = xMods + 8 * Font.Height;
xShift = xData + 5 * Font.Height;
xCtrl = xShift +5 * Font.Height;
xAlt = xCtrl +5 * Font.Height;
xRight = xAlt +5 * Font.Height;
ClientSize = new Size(xRight, Font.Height * (iNumLines + 1));
FormBorderStyle = FormBorderStyle.Fixed3D;
MaximizeBox = false;
}
protected override void OnKeyDown(KeyEventArgs kea)
{
akeyevt[iInsertIndex].evttype = EventType.KeyDown;
akeyevt[iInsertIndex].evtargs = kea;
onKey();
}
protected override void OnKeyUp(KeyEventArgs kea)
{
akeyevt[iInsertIndex].evttype = EventType.KeyUp;
akeyevt[iInsertIndex].evtargs = kea;
onKey();
}
protected override void OnKeyPress(KeyPressEventArgs kpea)
{
akeyevt[iInsertIndex].evttype = EventType.KeyPress;
akeyevt[iInsertIndex].evtargs = kpea;
onKey();
}
void onKey()
{
if(iNumValid < iNumLines)
{
Graphics grfx = CreateGraphics();
DisplayKeyInfo(grfx, iInsertIndex, iInsertIndex);
grfx.Dispose();
}
else
{
ScrollLines();
}
iInsertIndex = (iInsertIndex + 1) % iNumLines;
iNumValid = Math.Min(iNumValid + 1, iNumLines);
}
protected virtual void ScrollLines()
{
Rectangle rect = new Rectangle(0, FontHeight,
ClientSize.Width,
ClientSize.Height - Font.Height);
Invalidate(rect);
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
BoldUnderLine(grfx, "Event", xEvent, 0);
BoldUnderLine(grfx, "KeyChar", xChar, 0);
BoldUnderLine(grfx, "KeyCode", xCode, 0);
BoldUnderLine(grfx, "Modifiers", xMods, 0);
BoldUnderLine(grfx, "KeyData", xData, 0);
BoldUnderLine(grfx, "Shift", xShift, 0);
BoldUnderLine(grfx, "Control", xCtrl, 0);
BoldUnderLine(grfx, "Alt", xAlt, 0);
if(iNumValid < iNumLines)
{
for (int i = 0; i < iNumValid; i++)
DisplayKeyInfo(grfx, i, i);
}
else
{
for (int i = 0; i < iNumLines; i++)
DisplayKeyInfo(grfx, i, (iInsertIndex + i) % iNumLines);
}
}
void BoldUnderLine(Graphics grfx, string str, int x, int y)
{
Brush brush = new SolidBrush(ForeColor);
grfx.DrawString(str, Font, brush, y, y);
grfx.DrawString(str, Font, brush, x+1, y);
SizeF sizef = grfx.MeasureString(str, Font);
grfx.DrawLine(new Pen(ForeColor), x, y + sizef.Height,
x+sizef.Width, y+sizef.Height);
}
void DisplayKeyInfo(Graphics grfx, int y, int i)
{
Brush br = new SolidBrush(ForeColor);
y = (1+y) * Font.Height;
grfx.DrawString(akeyevt[i].evttype.ToString(), Font, br, xEvent, y);
if (akeyevt[i].evttype == EventType.KeyPress)
{
KeyPressEventArgs kpea =
(KeyPressEventArgs) akeyevt[i].evtargs;
string str = String.Format("/x202D{0} (0x{1:X4})",
kpea.KeyChar, (int) kpea.KeyChar);
grfx.DrawString(str, Font, br, xChar, y);
}
else
{
KeyEventArgs kea = (KeyEventArgs) akeyevt[i].evtargs;
string str = String.Format("{0} ({1})", kea.KeyCode, (int) kea.KeyCode);
grfx.DrawString(str, Font, br, xCode, y);
grfx.DrawString(kea.Modifiers.ToString(), Font, br, xMods, y);
grfx.DrawString(kea.KeyData.ToString(), Font, br, xData, y);
grfx.DrawString(kea.Shift.ToString(), Font, br, xShift, y);
grfx.DrawString(kea.Control.ToString(), Font, br, xCtrl, y);
grfx.DrawString(kea.Alt.ToString(), Font, br, xAlt, y);
}
}
}
- [收藏]WINFORM(C#)的键盘测试
- winform键盘事件的响应(C#)
- winform键盘事件的响应(C#,winform)
- Winform实现屏蔽键盘的win和alt+F4(C#)
- 自动化测试程序之一自定义键盘的模拟测试程序(C语言)
- C#winform处理鼠标和键盘事件
- C#winform的加密与解密源代码测试成功
- 制作一个WinForm的闪屏收藏
- WinForm特效:桌面上的遮罩层 收藏
- C# WinForm 键盘快捷键的使用
- C# winform的窗体 获取键盘事件
- 背景透明的ListBox(C#.WinForm)
- 背景透明的ListBox(C#.WinForm)
- C#(Winform的几个小知识点)
- linux模拟键盘按键方案(收藏)
- 收藏的博客[C++]
- 诡异的心理测试[收藏]
- C#(WINFORM)学习
- C语言图形函数介绍篇
- 赵本山给美国人测智商(暴笑)
- 面向对象软件工程方法学实践
- javascript函数库
- 程序员的wiki
- [收藏]WINFORM(C#)的键盘测试
- 使用SuperWaba在PPC和Palm上编程【转贴】
- Java 中的位运算
- 聖誕老人就要來咯
- 祥解Visual C#数据库编程
- 大学的二十一件事
- 18位身份证号码编码标准
- 用Delphi编制可自动升级程序
- Book Review of Software Project Survival Guide 2. Requirement Development