日期:2014-05-17  浏览次数:20916 次

怎么正确关闭屏幕键盘?直接用CloseMainWindow();会报错
以下代码, 当我在保存文件前用鼠标点屏幕键盘的红叉时,会报错。如果不关闭键盘,就不会有问题。
问题是, 我怎么来判断这个屏幕键盘(进程)是否已经在其它地方被关闭了呢?
谢谢
C# code

           System.Diagnostics.Process prKeyBoard = System.Diagnostics.Process.Start("osk.exe");
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                //  ...

                // 关闭屏幕键盘
                if (prKeyBoard != null)
                {
                    prKeyBoard.CloseMainWindow();
                    /// prKeyBoard.Close();
                }
            }



------解决方案--------------------
参考以下代码,稍作修改就可以
[code=C#]
/// <summary>
/// 打开窗体
/// </summary>
/// <param name="mdiChildTypeString"></param>
/// <returns></returns>
private void ShowWindow(string mdiChildTypeString)
{
Form currentMdiChild = null;

if (!IsMDIChildFormExist(mdiChildTypeString))
{
Assembly assembly = Assembly.GetExecutingAssembly();
Type typForm = assembly.GetType(mdiChildTypeString);

Object mdiChild = typForm.InvokeMember(
null,
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null);

if (mdiChild != null)
{
currentMdiChild = mdiChild as Form;
currentMdiChild.MdiParent = this;
//currentMdiChild.WindowState = FormWindowState.Maximized;
currentMdiChild.Show();
currentMdiChild.Focus();
}
}
}
[code]
------解决方案--------------------
C# code

/// <summary>
        /// 检查MDI子窗体是否存在
        /// </summary>
        /// <param name="mdiChildTypeString"></param>
        /// <returns></returns>        
        private bool IsMDIChildFormExist(string mdiChildTypeString)
        {
            Form currentMDIChild = null;
            foreach (Form mdiChildFrm in this.MdiChildren)
            {
                if (mdiChildFrm.GetType().ToString() == mdiChildTypeString)
                {
                    currentMDIChild = mdiChildFrm;
                    break;
                }
            }

            if (currentMDIChild != null)
            {
                if (currentMDIChild.WindowState == FormWindowState.Minimized)
                {
                    currentMDIChild.WindowState = FormWindowState.Normal;
                }
                currentMDIChild.TopMost = true;
                currentMDIChild.Activate();
                currentMDIChild.Focus();
                return true;
            }
            else
            {
                return false;
            }

        }