一旦支持了鼠标功能MF可研究的领域似乎变得无穷无尽这也许是我近几天一直陷于此中不能自拔的原因了
有了鼠标即使没有键盘我们也可以通过虚拟键盘完成输入工作这个工作很有意义也很有挑战性因为这不是windows平台也不是Dos平台这是一穷二白的MF平台所有的工作都得从头做起(实现绘制键盘向有输入焦点控件发送按键信息删除添加字符等等功能)
目前字符串不仅无法转换为数字并且没有字符插入功能所以这个函数也必须自己来实现此外输入焦点光标也需要自己绘制好了先不说难处了先看看最终成果
)thiswidth=; height=>
这是一个文本输入实例当文本框有输入焦点时自动弹出输入面板
image onmousewheel=javascript:return big(this) border= alt= src= width= onload=javascript:if(thiswidth>)thiswidth=; height=>
虚拟键盘上的Shift键也是有效的切换后可以输入符号和大写字母
image onmousewheel=javascript:return big(this) border= alt= src= width= onload=javascript:if(thiswidth>)thiswidth=; height=>
当文本框失去输入焦点的时候虚拟键盘消失
以上应用实现的代码如下
//个人信息
public class YFSelfInfo : YFDialog
{
YFLabel[] label=new YFLabel[];
YFText[] text=new YFText[];
string[] strLabelName = new string[] {姓名:单位:博客:};
public YFSelfInfo(string Title int Left int Top int Width int Height YFWindowBase Parent): base(Title Width Height Parent)
{
//
for(int i=;i<;i++)
{
label[i] = new YFLabel(strLabelName[i] ClientRectTop + +*i );
ChildrenAdd(label[i]);
text[i] = new YFText( label[i]Top );
text[i]Enter += new YFSoftSPOTPresentationYFEventHandler(text_Enter);
text[i]Leave += new YFSoftSPOTPresentationYFEventHandler(text_Leave);
ChildrenAdd(text[i]);
}
}
//获得输入焦点时显示输入面板
void text_Leave(object sender EventArgs e)
{
HideInputPanel();
thisHeight = SystemMetricsScreenHeight;
}
//失去焦点时隐藏输入面板
void text_Enter(object sender EventArgs e)
{
ShowInputPanel();
thisHeight = thisInputPanelTop + ;
}
//主窗体鼠标信息
public override void OnMouseClick(object sender MouseEventArgs e)
{
text_Leave(null null); //隐藏输入面板
baseOnMouseClick(sender e);
}
}
用户程序是不是很简单?再看看我实现的一个记事本程序
image onmousewheel=javascript:return big(this) border= alt= src= width= onload=javascript:if(thiswidth>)thiswidth=; height=>
正在输入界面
image onmousewheel=javascript:return big(this) border= alt= src= width= onload=javascript:if(thiswidth>)thiswidth=; height=>
菜单功能支持
相关代码如下
//记事本
public class YFNote : YFDialog
{
YFText text;
public YFNote(string Titleint Left int Top int Width int Height YFWindowBase Parent)
: base(Title Width Height Parent)
{
text = new YFText( ClientRectLeft ClientRectTop ClientRectWidth ClientRectHeight);
textBorderStyle = BorderStyleNone;
textAlign = false;
textEnter += new YFSoftSPOTPresentationYFEventHandler(text_Enter);
textLeave += new YFSoftSPOTPresentationYFEventHandler(text_Leave);
ChildrenAdd(text);
thisLeft = Left;
thisTop = Top;
MenuAddItem(new MenuItem(新建));
MenuAddItem(new MenuItem(保存));
MenuAddItem(new MenuItem());
MenuAddItem(new MenuItem(退出));
}
//获得输入焦点时显示输入面板
void text_Leave(object sender EventArgs e)
{
HideInputPanel();
thisHeight = SystemMetricsScreenHeight;
textHeight = ClientRectHeight;
}
//失去焦点时隐藏输入面板
void text_Enter(object sender EventArgs e)
{
ShowInputPanel();
thisHeight = thisInputPanelTop+;
textHeight = ClientRectHeight;
}
//主窗体鼠标信息
public override void OnMouseClick(object sender MouseEventArgs e)
{
text_Leave(null null); //隐藏输入面板
baseOnMouseClick(sender e);
}
//菜单
public override void OnMenuClick(MenuEventArgs e)
{
switch (eText)
{
case 新建:
textText = ;
break;
case 保存:
MessageBox(不好意思暂时没有保存功能记事本 MessageBoxButtonsOK MessageBoxIconWarning);
break;
case 退出:
Close();
break;
}
baseOnMenuClick(e);
}
}
程序也超简单但功能还是蛮强大的我们下面看看虚拟键盘的相关代码实现思路和鼠标功能一样也是直接借助基类的虚拟函数向指定的窗体和控件发送按键消息
核心代码如下
public void TransactKey(KeyState state object sender MouseEventArgs e)
{
//向控件发送按键消息
if (thisParent != null)
{
Keys key = KeysNone;
int keyValue=;
char _char = (char);
YFButton b = (YFButton)sender;
if (bText == || bText==En || bText==Cn) return;
Button btn = ButtonNone;
switch (bText)
{
case Tab:
key = KeysTab;
break;
case Caps:
key = KeysCapsLock;
break;
case Shift:
key = KeysSelect;
break;
case Ent:
key = KeysEnter;
btn = ButtonSelect;
break;
case Esc:
key = KeysEscape;
break;
case Ins:
key = KeysInsert;
break;
case Del:
key = KeysDelete;
break;
case Menu:
key = KeysMenu;
btn = ButtonMenu;
break;
case ↑:
key = KeysUp;
btn = ButtonUp;
break;
case ←:
key = KeysLeft;
btn = ButtonLeft;
break;
case ↓:
key = KeysDown;
btn = ButtonDown;
break;
case →:
key = KeysRight;
btn = ButtonRight;
break;
case <:
key = KeysBack;
btn = ButtonBack;
break;
default:
_char = bText[];
key = (Keys)bName[];
break;
}
keyValue = (int)key;
//向当前有输入焦点的窗口发送系统按键消息
if (btn!= ButtonNone)
{
if ((state & KeyStateDown) > ) ParentGetFocus()OnButtonDownEx(new ButtonEventArgs(null null new TimeSpan() btn));
if ((state & KeyStateUp) > ) ParentGetFocus()OnButtonUpEx(new ButtonEventArgs(null null new TimeSpan() btn));
}
//获得输入焦点的控件
YFControl c = thisParentChildrenGetFocus();
if (c != null)
{
if ((state & KeyStateDown) > ) cOnKeyDown(sender new KeyEventArgs(key keyValue button[]Checked button[]Checked button[]Checked));
if ((state & KeyStatePress) > && _char != (char)) cOnKeyPress(sender new KeyPressEventArgs(_char));
if ((state & KeyStateUp) > ) cOnKeyUp(sender new KeyEventArgs(key keyValue button[]Checked button[]Checked button[]Checked));
}
//父窗体要求接收按键消息
if (ParentKeyPreview)
{
if ((state & KeyStateDown) > ) ParentOnKeyDown(Parent new KeyEventArgs(key keyValue button[]Checked button[]Checked button[]Checked));
if ((state & KeyStatePress) > && _char != (char)) ParentOnKeyPress(Parent new KeyPressEventArgs(_char));
if ((state & KeyStateUp) > ) ParentOnKeyUp(Parent new KeyEventArgs(key keyValue button[]Checked button[]Checked button[]Checked));
}
}
}
以上仅仅实现的是英文输入中文输入怎么做?似乎要困难的多此外粘贴复制功能要不要做?总之有很多很多的东西值得我们去探索去实现这一切似乎我们在走Microsoft的老路要在MF平台上实现一个Micro Windows!说到这我想到一个笑话有一个人给上帝进行打赌说他也能造人上帝问他你怎么做他说我先用泥土捏一个…等等上帝打断了他你要用你自己的泥土