65.9K
CodeProject 正在变化。 阅读更多。
Home

SIP(软输入面板)重叠控件

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.60/5 (7投票s)

2008年7月2日

CPOL
viewsIcon

35861

downloadIcon

339

如何解决输入面板重叠问题

引言

pc_capture1.JPG pc_capture2.JPG pc_capture3.JPG pc_capture4.JPG

本文提供了一个简单但非常有效的解决方案,用于解决在打开inputpanel(软键盘)时控件不可见的问题。 您可以看到在截图中,所有控件都根据inputpanel和可用空闲空间进行了调整。

Using the Code

将该类添加到您的项目中。 它是一个static类,因此可以从任何窗体调用。 为了使之工作,您需要在每个窗体中添加一个inputpanel控件。 如果您使用的是选项卡页,则仅传递当前选项卡页。 例如

OverLappingInputPanel.Inputpanel(tabControl1.TabPages
[tabControl1.SelectedIndex].Controls, inputPanel1.Enabled, 
inputPanel1.VisibleDesktop.Height); 

OverlappingInputPanel

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;  

namespace MFA_Common
{
    public static class OverLappingInputPanel
    {
        private static int oldrest = 0;
        private static int oldheight = 0;
        private static int oldtop = 0;
        private static Control isFocused = null;

        /// <summary>
        /// Brings the control that has focus on top of the inputpanel
        /// </summary>
        /// <param name=""controls""></param>
        /// <param name=""SIDCollaps""></param>
        public static void Inputpanel(Control.ControlCollection controls,
            Boolean SIDExpended, int Visualdesktopheight)
        {
            try
            {
                if (controls != null)
                {
                    if (SIDExpended == true)
                    {
                            //Loop through the controls to determinate which has focus
                            //this can be avoided if the gotfocus event is fired
                            //for each control that is beneath the inputpanel if expanded

                                foreach (Control ctrl in controls)
                            {
                                if (ctrl.Focused)
                                {
                                    isFocused = ctrl;
                                    // if this is not the case calculate the needed space
                                    int rest = Visualdesktopheight - (
                                        ctrl.Top + ctrl.Height);
                                    if (rest < 0)
                                    {
                                        //move each control up with the calculated space
                                        foreach (Control ctrl2 in controls)
                                        {
                                            ctrl2.Top = ctrl2.Top + rest;
                                        }
                                        oldrest = rest;

                                    }
                                    if (ctrl.Height > Visualdesktopheight)
                                    {
                                        oldtop = ctrl.Top; 
                                        oldheight = ctrl.Height;
                                        ctrl.Height = Visualdesktopheight;
                                        ctrl.Top = 0;
                                    }
                                }
                            }

                            //escape the current function if the selected control is
                            //not under the inputpanel
                            return;
                    }
                    else
                    {
                        //when the inputpanel is hidden, recalculate the top position
                        //for each control.
                        //the old value has to be reversed *-1 to inverse the changes
                        //if the control was bigger than the available screen, then
                        //give it back its original value 
                        if (oldheight != 0)
                        {
                            isFocused.Top = oldtop; 
                            isFocused.Height = oldheight;
                            oldheight = 0;
                        }
                        foreach (Control ctrl2 in controls)
                        {
                            ctrl2.Top = ctrl2.Top + (-1 * oldrest);
                        }
                        oldrest = 0;
                    }
                }
            }

            catch 
            {
                //the ObjectDisposedException is trapped here. This occurs when the user 
                //doesn't closes the sip before moving to an other screen
                //if an inputpanel is closed on an other form, 
                //then the object that is stored is dispose
                //if this is the case, catch the exception and set the object to null;
                oldrest = 0;
                controls = null;
            }
        }
    }
}

如果您传递需要屏幕调整的对象,则可以改进代码。 只需为每个被inputpanel重叠的控件添加gotfocus事件,并将其传递给函数。 这样,该函数就不需要循环遍历控件容器中的每个控件了。

历史

  • 2008年7月2日:初始发布
© . All rights reserved.