예제 #1
0
        /// <summary>
        /// Creates new tooltip form for displaying given tooltip
        /// </summary>
        public static ToolTipForm CreateToolTipForm(string text)
        {
            IToolTipRenderer renderer = null;

            if (text.StartsWith("#user#"))
            {
                renderer = new PlayerTooltipRenderer(text.Substring(6));
            }
            else if (text.StartsWith("#battle#"))
            {
                renderer = new BattleTooltipRenderer(int.Parse(text.Substring(8)));
            }
            else if (text.StartsWith("#map#"))
            {
                renderer = new MapTooltipRenderer(text.Substring(5));
            }
            else
            {
                renderer = new TextTooltipRenderer(text);
            }

            var nt   = new ToolTipForm(renderer);
            var size = nt.GetTooltipSize();

            if (size != null)
            {
                nt.Size = size.Value;
            }
            else
            {
                nt.Dispose();
                return(null);
            }

            return(nt);
        }
예제 #2
0
        private void RefreshToolTip(bool invalidate)
        {
            if (Program.MainWindow != null && Program.MainWindow.IsHandleCreated && !Program.CloseOnNext && Program.MainWindow.Visible && Program.MainWindow.WindowState != FormWindowState.Minimized)
            {
                var    control = MainWindow.Instance.GetHoveredControl();
                string text    = null;
                if (control != null)
                {
                    tooltips.TryGetValue(control, out text);
                }
                bool isWindowActive = Form.ActiveForm != null;
                bool newTooltip     = false; //to trigger position update

                if (lastText != text || lastVisible != Visible || lastActive != isWindowActive)
                {
                    if (tooltip != null)
                    {
                        tooltip.Close();
                        tooltip.Dispose();
                        tooltip = null;
                    }

                    if (!string.IsNullOrEmpty(text) && Visible && isWindowActive)
                    {
                        tooltip = ToolTipForm.CreateToolTipForm(text);
                        if (tooltip != null)
                        {
                            tooltip.ForeColor = Program.Conf.OtherTextColor;
                            tooltip.Visible   = true;
                        }
                    }

                    lastText    = text;
                    lastVisible = visible;
                    lastActive  = isWindowActive;
                    newTooltip  = true; //trigger position update
                }

                if (tooltip != null)
                {
                    //method B: tooltip remain stationary until user block the vision or when new tooltip is available
                    //var mp = System.Windows.Forms.Control.MousePosition;
                    //int tooltipLocationX = tooltip.Location.X;
                    //int tooltipLocationY = tooltip.Location.Y;
                    //if (mp.X > tooltipLocationX && mp.X < tooltipLocationX + tooltip.Width)
                    //{
                    //    if (mp.Y > tooltipLocationY && mp.Y < tooltipLocationY + tooltip.Height)
                    //    {
                    //        newTooltip = true;
                    //    }
                    //}
                    //if (Math.Abs(mp.X - tooltipLocationX) > 50 || Math.Abs(mp.Y - tooltipLocationY) > 50)
                    //{
                    //    newTooltip = true;
                    //}
                    //if (newTooltip) //set new position for new tooltip
                    //{
                    //    var point = MainWindow.Instance.PointToScreen(new Point(5, 5));
                    //    var scr = Screen.GetWorkingArea(new Point((int)point.X, (int)point.Y));

                    //    //need screen0's bounds because SetDesktopLocation is relative to screen0.
                    //    var scr1 = Screen.AllScreens[0].WorkingArea;
                    //    var scr1B = Screen.AllScreens[0].Bounds;

                    //    var nx = Math.Min(mp.X + 14 + scr1B.X - scr1.X, scr.Right - tooltip.Width - 2);
                    //    var ny = Math.Min(mp.Y + 14 + scr1B.Y - scr1.Y, scr.Bottom - tooltip.Height - 2);

                    //    var rect = new Rectangle(nx, ny, tooltip.Width, tooltip.Height);
                    //    if (rect.Contains(mp))
                    //    {
                    //        nx = mp.X - tooltip.Width - 8;
                    //        ny = mp.Y - tooltip.Height - 8;
                    //    }

                    //    tooltip.SetDesktopLocation(nx, ny);

                    //    var newSize = tooltip.GetTooltipSize();
                    //    if (newSize.HasValue && newSize.Value != tooltip.Size) tooltip.Size = newSize.Value;
                    //}
                    //end method B

                    //method A: tooltip follow mouse cursor everywhere it go
                    var mp = System.Windows.Forms.Control.MousePosition;

                    var point = MainWindow.Instance.PointToScreen(new Point(5, 5));
                    var scr   = Screen.GetWorkingArea(new Point((int)point.X, (int)point.Y));

                    //need screen0's bounds because SetDesktopLocation is relative to screen0.
                    var scr1  = Screen.AllScreens[0].WorkingArea;
                    var scr1B = Screen.AllScreens[0].Bounds;

                    var nx = Math.Min(mp.X + 14 + scr1B.X - scr1.X, scr.Right - tooltip.Width - 2);
                    var ny = Math.Min(mp.Y + 14 + scr1B.Y - scr1.Y, scr.Bottom - tooltip.Height - 2);

                    var rect = new Rectangle(nx, ny, tooltip.Width, tooltip.Height);
                    if (rect.Contains(mp))
                    {
                        nx = mp.X - tooltip.Width - 8;
                        ny = mp.Y - tooltip.Height - 8;
                    }

                    tooltip.SetDesktopLocation(nx, ny);

                    var newSize = tooltip.GetTooltipSize();
                    if (newSize.HasValue && newSize.Value != tooltip.Size)
                    {
                        tooltip.Size = newSize.Value;
                    }
                    //end method A

                    if (invalidate)
                    {
                        tooltip.Invalidate(true);
                    }
                }
            }
        }