最近在做的WINCE系统
如下使用VISIO画的窗体结构关系图
PS:画的真丑 :)
简单解释一下这个图,就是所有的FORM都是继承自FormBase这个类设计器里面设计了程序大体的构造,全屏,标题栏状态栏,还有一些公共的相应键盘事件,比如WINCE系统左右肩膀的按键相应,一般的都是左键进入右键退出。、
DataListFormBase是所有详细展示页的基类,该类继承自FormBase.
设计器如下
可以见到,这个基类窗体还是比较简单的,只有标题栏跟状态栏。标题栏就是简单的窗体名,可以再每个子类中赋值,状态栏中左下是一个lable用于系统时间的显示,右下脚是一个很蹩脚的Button用于操作软键盘开关。
public partial class FormBase<T> : Form where T : class
{
public string FormName = "";
public T _controller;
public FormBase()
{
InitializeComponent();
}
int m;
int h;
int s;//记录旧时间
//记录新时间、主要用于比较是否已经过去了一分钟,当然我的这个程序已经处理了秒,你可以把相应的部分删除掉
int m1;
int h1;
int s1;
public FormBase(T controller)
{
InitializeComponent();
_controller = controller;
m = DateTime.Now.Minute;
h = DateTime.Now.Hour;
s = DateTime.Now.Second;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
lbName.Text = FormName;
Rectangle rectangle = Screen.PrimaryScreen.Bounds;
SetFullScreen(true, ref rectangle);
DoSetTime(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
timer1.Enabled = true;
lbLogin.Text = string.Format("登录人:{0}", DeviceInfo.LoginUserId);
lbName.Focus();
}
private void FormBase_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
{
// Up
}
if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
// Down
}
if ((e.KeyCode == System.Windows.Forms.Keys.Left))
{
// Left
}
if ((e.KeyCode == System.Windows.Forms.Keys.Right))
{
}
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
// Enter
}
if (e.KeyCode == Keys.F12)
{
CloseForm();
}
}
public virtual void CloseForm()//关闭FORM方法,可以再子类中扩展
{
this.Close();
}
public static bool SetFullScreen(bool fullscreen, ref Rectangle rectOld)//设置全屏方法,应用到了WIN32的API调用
{
int Hwnd = 0;
Hwnd = Win32.FindWindow("HHTaskBar", null);
if (Hwnd == 0) return false;
if (fullscreen)
{
Win32.ShowWindow((IntPtr)Hwnd, Win32.SW_HIDE);
Rectangle rectFull = Screen.PrimaryScreen.Bounds;
Win32.SystemParametersInfo(Win32.SPI_GETWORKAREA, 0, ref rectOld, Win32.SPIF_UPDATEINIFILE);//get
Win32.SystemParametersInfo(Win32.SPI_SETWORKAREA, 0, ref rectFull, Win32.SPIF_UPDATEINIFILE);//set
}
else
{
Win32.ShowWindow((IntPtr)Hwnd, Win32.SW_SHOW);
Win32.SystemParametersInfo(Win32.SPI_SETWORKAREA, 0, ref rectOld, Win32.SPIF_UPDATEINIFILE);
}
return true;
}
private void timer1_Tick(object sender, EventArgs e)
{
h1 = DateTime.Now.Hour;
m1 = DateTime.Now.Minute;
s1 = DateTime.Now.Second;
if (m1 > m || h1 > h || s1 > s)
{
this.DoSetTime(m1, h1, s1);
}
}
public delegate void SetTime(int m, int h, ints);//设置标签时间代理声明
private void DoSetTime(int m1, int h1, ints1)//设置标签时间事件,采用线程方式刷新
{
if (this.InvokeRequired)
{
SetTime setpt = new SetTime(DoSetTime);
IAsyncResult isre = this.BeginInvoke(setpt, new object[] { m1, h1, s1 });
try
{
this.EndInvoke(isre);
}
catch
{
}
}
else
{
lableTime.Text = string.Format("{0}:{1}:{2}", h1 < 10 ? "0" + h1.ToString() : h1.ToString(),
m1 < 10 ? "0" + m1.ToString() : m1.ToString(),
s1 < 10 ? "0" + s1.ToString() : s1.ToString());
}
}
public static InputPanel _softKeyBoard = new InputPanel();
/// <summary>
/// 显示/隐藏 软键盘
/// </summary>
public static void ShowHideSoftKeyBoard(Boolean isShow)//软键盘开关事件
{
_softKeyBoard.Enabled = isShow;
}
private void btnSoft_Click(object sender, EventArgs e)
{
if(_softKeyBoard.Enabled)
{
btnSoft.BackColor = SystemColors.ActiveCaption;
ShowHideSoftKeyBoard(false);
}
else
{
ShowHideSoftKeyBoard(true);
btnSoft.BackColor = SystemColors.ActiveBorder;
}
}
}
其他FORM全部继承自这个BASE,设置一下FormName即可,下面状态栏上的方法所有FORM通用,这样可以大大方便开发,也更加容易维护通用的东西.
PS: 开发过程遇到了一个很恶心的问题,就是子窗体的设计器都LOAD不成功,回报出“The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: MainForm --- The base class 'CP.Device.FormBase' could not be loaded. Ensure the assembly has been referenced and that all projects have been built. ”问题,但是运行完全没问题,目前还没找出什么好的解决方案,我很挫蛋的方式是每次要设计窗体的时候就把基层的基类改成Form改完之后再改回来。。。谁 有好的方案欢迎交流 ~