如何在鼠标悬停时显示标题栏并在鼠标离开时隐藏 [英] How Do I Show Title Bar When Mouse Over It And Hide When Mouse Away

查看:362
本文介绍了如何在鼠标悬停时显示标题栏并在鼠标离开时隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当鼠标悬停在标题栏上时如何显示标题栏并在鼠标远离它时隐藏它在Windows窗体C#application。

How do i show title bar when mouse over it and hide when mouse away from it in windows form C# application.

推荐答案

你必须使用MouseMove-您的表格中的活动。

如果MouseCursor进入您想要显示Titlebar的区域,您可以将其显示或在表单中给它一个位置。



为了让它不可见,你可以使用Titelbar中的MouseLeave-Event。
You have to use the MouseMove-Event from your Form.
If the MouseCursor Comes in the Region where you want to show your "Titlebar" you make it visible or give it a location in your form.

To make it unvisible you use the MouseLeave-Event from your Titelbar.


我会告诉你一种方法,但是,我认为这是一个相当丑陋的黑客,我建议你不要这样做:)我认为一个更好的策略,更符合Windows程序指南,是为用户实现一些方式选择是否一个表单显示其TitleBar或隐藏它:上下文菜单?按钮?下拉选择?等等



你会意识到,如果你隐藏了TitleBar,你还会隐藏表格右边,左边,底边,边的任何边框。 ?
I'll show you one way to do this, but, I think this is a rather ugly hack, and I recommend you don't do this :) I think a much better strategy, more consistent with Windows Program Guidelines, is to implement some way for the User to choose whether a form displays its TitleBar or hides it: Context Menu ? Button ? Dropdown Choice ?, etc.

You do realize that if you hide the TitleBar, you will also hide any border around the right, left, bottom, sides of the Form ?
using System;
using System.Drawing;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            shownText = this.Text;
            shownFormBorderStyle = this.FormBorderStyle;
        }

        private Rectangle FormBounds;

        private bool TitleBarShowing = true;
        private bool LockMethods = false;

        private FormBorderStyle shownFormBorderStyle = FormBorderStyle.Sizable;
        private FormBorderStyle hiddenFormBorderStyle = FormBorderStyle.FixedToolWindow;
        
        private string shownText;

        private void Form1_Load(object sender, EventArgs e)
        {
            FormBounds = this.Bounds;
        }

        private void UpdateTitleBarVisibility(bool isvisible)
        {
            if (LockMethods) return;

            if (isvisible)
            {
                if (!TitleBarShowing)
                {
                    // see explanation in comment #1
                    if (! FormBounds.Contains(MousePosition)) return;

                    TitleBarShowing = true;

                    LockMethods = true;

                    this.SuspendLayout();
                        this.Text = shownText;
                        this.ControlBox = true;
                        this.FormBorderStyle = shownFormBorderStyle;
                    this.ResumeLayout();

                    // see explanation in comment #2
                    Application.DoEvents();
                    this.Visible = true;
                    this.BringToFront();
                    this.Refresh();

                    LockMethods = false;
                }
            }
            else if (TitleBarShowing)
            {
                if (FormBounds.Contains(MousePosition)) return;

                TitleBarShowing = false;

                LockMethods = true;

                this.SuspendLayout();
                    this.Text = "";
                    this.ControlBox = false;
                    this.FormBorderStyle = hiddenFormBorderStyle;
                this.ResumeLayout();

                LockMethods = false;
            }
        }

        private void Form1_Enter(object sender, EventArgs e)
        {
            UpdateTitleBarVisibility(true);
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            UpdateTitleBarVisibility(true);
        }

        private void Form1_MouseEnter(object sender, EventArgs e)
        {
            UpdateTitleBarVisibility(true);
        }

        private void Form1_Leave(object sender, EventArgs e)
        {
            UpdateTitleBarVisibility(false);
        }

        private void Form1_MouseLeave(object sender, EventArgs e)
        {
            UpdateTitleBarVisibility(false);
        }
    }
}

评论:



1.表格的边界属性...假设它被放置在屏幕上(它应放置在哪里)...在屏幕坐标中给出一个矩形,其中包括非客户区域,包括TitleBar和Form Border。通过拦截该区域的移动,并阻止更新事件,我们可以(希望)减少闪烁。



2.使用Application.DoEvents,Visible = true ,刷新,BringToFront这里是WinForms中怪癖的解决办法:如果没有这种咒语,表格可能会在表格失去焦点且TitleBar区域隐藏的情况下消失,并且您尝试通过移动来恢复TitleBar将鼠标移到可见表格上。你想知道为什么?:问微软:)



3.总结:一种hackish技术被证明依赖于你可以将鼠标移到外面的事实Form的边界比Form的移动检测处理程序可以捕获的更快。使用风险由您自己承担。

Comments:

1. A Form's Bounds property ... assuming it is "placed" on the Screen (which is where it should be placed) ... gives a rectangle in screen co-ordinates which includes non-client areas, including TitleBar and Form Border. By intercepting a move in that area, and preventing an update Event, we can (hopefully) reduce flashing.

2. The use of Application.DoEvents, Visible = true, Refresh, BringToFront here is a work-around for a "quirk" in WinForms : without this "incantation," the Form may disappear in the case the Form has lost focus with TitleBar area hidden and you attempt to restore the TitleBar by moving the Mouse onto the visible Form. You want to know "why ?:" ask Microsoft :)

3. Summary: a hackish technique is demonstrated which relies on the fact that you can move your mouse outside the Form's boundaries faster than the Form's move detection handler can catch. Use at your own risk.


这篇关于如何在鼠标悬停时显示标题栏并在鼠标离开时隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆