这是什么控制? (“打开”按钮,下拉)

时间:2008-08-27 20:03:04

标签: .net winforms .net-2.0

某些Windows应用程序中使用的打开文件对话框上的“打开”按钮包含一个下拉箭头,其中包含其他选项列表 - 即“打开方式...”。

我还没有在每个Windows应用程序中看到这个,所以你可能需要尝试一些才能得到它,但是如果你去菜单并选择SQL Server Management Studio和Visual Studio 2005都会显示按钮文件>开路>文件...

我想在我的一个应用程序中使用这样的按钮和内置列表,但我找不到他们在visual studio中使用的控件。我应该澄清一下,我正在寻找那个特定的按钮,而不是整个对话框。有什么想法吗?

7 个答案:

答案 0 :(得分:6)

我使用Spy ++中的可拖动搜索(与VS一起安装)来查看VS的文件打开对话框中的拆分打开按钮。

这表明它是一个普通的windows按钮,其风格包括BS_DEFSPLITBUTTON。这是一个神奇的关键词,可以让你进入一些有趣的地方,包括

http://www.codeplex.com/windowsformsaero/SourceControl/FileView.aspx?itemId=212902&changeSetId=9930

在这里

http://msdn.microsoft.com/en-us/library/bb775949.aspx#using_splits

希望这会对你有所帮助。

编辑:

我实际上只是尝试了CodePlex中的代码并且确实创建了一个拆分按钮 - 但是你必须确保将按钮的FlatStyle设置为'System'而不是默认的'Standard'。我并不打算将事件处理内容连接到下拉列表,但我认为这已经包含在MSDN链接中。

当然,这只是Vista版本(但不需要启用Aero,尽管codeplex上有名称) - 如果您需要早期的操作系统支持,那么您将自己回来绘制它。

答案 1 :(得分:6)

我记得Ketarin有一个这样的按钮。

使用Reflector我找到了名为wyDay.SplitButton的开源控件。 wyDay.SplitButton

答案 2 :(得分:4)

我认为你要找的东西叫做toolStripSplitButton。它仅在toolStrip中可用。但是您可以在表单的任何位置添加toolStripContainer,然后将toolStrip和toolStripSplitButton放入容器中。

您不希望显示夹点,因此您需要设置gripMargin = 0.您还可以设置autosize = true,以便工具条符合您的按钮。该按钮看起来就像是表单上的普通按钮(拆分部分除外)。

答案 3 :(得分:2)

我不熟悉使用其中任何一个,但尝试在msdn中搜索splitbutton或dropdownbutton。我认为这些与您正在寻找的相似。

答案 4 :(得分:2)

这是我的拆分按钮实现。它不绘制箭头,焦点/非焦点行为略有不同。

我和原件都处理视觉风格,并且使用Aero看起来很棒。

基于http://wyday.com/splitbutton/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing;
using System.ComponentModel;
using System.Diagnostics;

// Original: http://blogs.msdn.com/jfoscoding/articles/491523.aspx
// Wyatt's fixes: http://wyday.com/splitbutton/
// Trimmed down and redone significantly from that version (Nick 5/6/08)
namespace DF
{
    public class SplitButton : Button
    {
        private ContextMenuStrip m_SplitMenu = null;
        private const int SplitSectionWidth = 14;
        private static int BorderSize = SystemInformation.Border3DSize.Width * 2;
        private bool mBlockClicks = false;
        private Timer mTimer;

        public SplitButton()
        {
            this.AutoSize = true;
            mTimer = new Timer();
            mTimer.Interval = 100;
            mTimer.Tick += new EventHandler(mTimer_Tick);
        }

        private void mTimer_Tick(object sender, EventArgs e)
        {
            mBlockClicks = false;
            mTimer.Stop();
        }

        #region Properties
        [DefaultValue(null)]
        public ContextMenuStrip SplitMenu
        {
            get
            {
                return m_SplitMenu;
            }
            set
            {
                if (m_SplitMenu != null)
                    m_SplitMenu.Closing -= 
                        new ToolStripDropDownClosingEventHandler(m_SplitMenu_Closing);

                m_SplitMenu = value;

                if (m_SplitMenu != null)
                    m_SplitMenu.Closing += 
                        new ToolStripDropDownClosingEventHandler(m_SplitMenu_Closing);
            }
        }

        private void m_SplitMenu_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            HideContextMenuStrip();
            // block click events for 0.5 sec to prevent re-showing the menu

        }

        private PushButtonState _state;
        private PushButtonState State
        {
            get
            {
                return _state;
            }
            set
            {
                if (!_state.Equals(value))
                {
                    _state = value;
                    Invalidate();
                }
            }
        }

        #endregion Properties

        protected override void OnEnabledChanged(EventArgs e)
        {
            if (Enabled)
                State = PushButtonState.Normal;
            else
                State = PushButtonState.Disabled;

            base.OnEnabledChanged(e);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
                return;
            if (State.Equals(PushButtonState.Disabled))
                return;
            if (mBlockClicks)
                return;

            if (!State.Equals(PushButtonState.Pressed))
                ShowContextMenuStrip();
            else
                HideContextMenuStrip();
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            if (!State.Equals(PushButtonState.Pressed) && !State.Equals(PushButtonState.Disabled))
            {
                State = PushButtonState.Hot;
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            if (!State.Equals(PushButtonState.Pressed) && !State.Equals(PushButtonState.Disabled))
            {
                if (Focused)
                {
                    State = PushButtonState.Default;
                }

                else
                {
                    State = PushButtonState.Normal;
                }
            }
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);

            Graphics g = pevent.Graphics;
            Rectangle bounds = this.ClientRectangle;

            // draw the button background as according to the current state.
            if (State != PushButtonState.Pressed && IsDefault && !Application.RenderWithVisualStyles)
            {
                Rectangle backgroundBounds = bounds;
                backgroundBounds.Inflate(-1, -1);
                ButtonRenderer.DrawButton(g, backgroundBounds, State);

                // button renderer doesnt draw the black frame when themes are off =(
                g.DrawRectangle(SystemPens.WindowFrame, 0, 0, bounds.Width - 1, bounds.Height - 1);
            }
            else
            {
                ButtonRenderer.DrawButton(g, bounds, State);
            }

            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            g.DrawString(Text, Font, SystemBrushes.ControlText, bounds, format);
        }

        private void ShowContextMenuStrip()
        {
            State = PushButtonState.Pressed;
            if (m_SplitMenu != null)
            {
                m_SplitMenu.Show(this, new Point(0, Height), ToolStripDropDownDirection.BelowRight);
            }
        }

        private void HideContextMenuStrip()
        {
            State = PushButtonState.Normal;
            m_SplitMenu.Hide();
            mBlockClicks = true;
            mTimer.Start();
        }
    }
}

答案 5 :(得分:1)

我认为没有内置的控件可以在.NET中实现。我在MSDN文档中浏览了标准的Windows Button控件,但它看起来并不像那里。

我找到了一个带有自定义实现的Code Project article;这可能会有所帮助。

答案 6 :(得分:0)

因为我在Windows中找到了控件,所以我希望已经在某处找到了它,所以我不需要在我的代码库中添加任何内容来使用它。但是this link处的分割按钮(通过msdn建议找到)看起来很有希望。

我稍后会自己尝试,但我不知道处理视觉风格的效果如何。