.NET で Windows.Forms を使用して単純な C# アプリケーションを開発しています。サブカテゴリを含むドロップダウン メニューを表示するボタンが必要です。ToolStripMenu によく似ていますが、ボタンです。私はそれを検索しましたが、亜種は見つかりませんでした。
私の質問は次のとおりです:これを行う方法はありますか?メニューをそれに添付できる秘密のボタンプロパティでしょうか?
どんな助けでも大歓迎です。
.NET で Windows.Forms を使用して単純な C# アプリケーションを開発しています。サブカテゴリを含むドロップダウン メニューを表示するボタンが必要です。ToolStripMenu によく似ていますが、ボタンです。私はそれを検索しましたが、亜種は見つかりませんでした。
私の質問は次のとおりです:これを行う方法はありますか?メニューをそれに添付できる秘密のボタンプロパティでしょうか?
どんな助けでも大歓迎です。
クリック イベントで ContextMenuStrip を表示できます。
private void button1_Click(object sender, EventArgs e) {
contextMenuStrip1.Show(button1, new Point(0, button1.Height));
}
メニューをボタンの上または下に表示するかどうかを独自に決定するには、次のコードを使用してみてください。
private void button1_Click(object sender, EventArgs e) {
Point screenPoint = button1.PointToScreen(new Point(button1.Left, button1.Bottom));
if (screenPoint.Y + contextMenuStrip1.Size.Height > Screen.PrimaryScreen.WorkingArea.Height) {
contextMenuStrip1.Show(button1, new Point(0, -contextMenuStrip1.Size.Height));
} else {
contextMenuStrip1.Show(button1, new Point(0, button1.Height));
}
}
@Jaex の回答を少し拡張して、区切り線、何も構成されていない場合の矢印の条件付き描画、およびメイン ボタン本体とメニュー矢印の個別のクリック イベントを許可します。
より良い配置のために、次のように設定できることに注意してください。button.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
ここに私のわずかな改善があります
public class SplitButton : Button
{
[DefaultValue(null), Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public ContextMenuStrip Menu { get; set; }
[DefaultValue(20), Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int SplitWidth { get; set; }
public SplitButton()
{
SplitWidth = 20;
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
var splitRect = new Rectangle(this.Width - this.SplitWidth, 0, this.SplitWidth, this.Height);
// Figure out if the button click was on the button itself or the menu split
if (Menu != null &&
mevent.Button == MouseButtons.Left &&
splitRect.Contains(mevent.Location) )
{
Menu.Show(this, 0, this.Height); // Shows menu under button
//Menu.Show(this, mevent.Location); // Shows menu at click location
}
else
{
base.OnMouseDown(mevent);
}
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
if (this.Menu != null && this.SplitWidth > 0)
{
// Draw the arrow glyph on the right side of the button
int arrowX = ClientRectangle.Width - 14;
int arrowY = ClientRectangle.Height / 2 - 1;
var arrowBrush = Enabled ? SystemBrushes.ControlText : SystemBrushes.ButtonShadow;
var arrows = new[] { new Point(arrowX, arrowY), new Point(arrowX + 7, arrowY), new Point(arrowX + 3, arrowY + 4) };
pevent.Graphics.FillPolygon(arrowBrush, arrows);
// Draw a dashed separator on the left of the arrow
int lineX = ClientRectangle.Width - this.SplitWidth;
int lineYFrom = arrowY - 4;
int lineYTo = arrowY + 8;
using( var separatorPen = new Pen(Brushes.DarkGray){DashStyle = DashStyle.Dot})
{
pevent.Graphics.DrawLine(separatorPen, lineX, lineYFrom, lineX, lineYTo);
}
}
}
}
最も簡単なオプションは、1 つのボタンのみを表示するドッキング解除された ToolStrip で ToolStripDropDownButton を使用することです。これを行うには: - ツールストリップをコントロール/フォームにドラッグします - レイアウト ヘルパーを使用して DropDownButton を追加します - GripStyle を Hidden に設定します - Dock を None に設定します
結果は、説明したドロップダウン動作をサポートするスタンドアロンのツールバー スタイルのボタンになります。
簡単にできました。これは役立つかもしれません:)
ContextMenuStrip contextMenuStrip1 = new ContextMenuStrip();
private void button1_Click(object sender, EventArgs e)
{
contextMenuStrip1.Items.Clear();
contextMenuStrip1.Items.Add("item1");
contextMenuStrip1.Items.Add("item2");
contextMenuStrip1.Show(button1, new Point(0, button1.Height));
}
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "item1")
{
MessageBox.Show(e.ClickedItem.Text);
}
}
上記の Jaex の MenuButton クラスは、私にとって完璧でした。以下のロジックを OnMouseDown に追加して、矢印をクリックした場合にのみコンテキスト メニューが表示されるようにしました。大きい部分をクリックすると、通常のクリック イベントがトリガーされます。「デフォルト」のクリック アクションで許可されます。
if (Menu != null && mevent.Button == MouseButtons.Left)
{
if (mevent.Location.X >= this.Width - 14)
{
System.Drawing.Point menuLocation;
if (ShowMenuUnderCursor)
{
menuLocation = mevent.Location;
}
else
{
menuLocation = new System.Drawing.Point(0, Height);
}
Menu.Show(this, menuLocation);
}
}
これは誰かに役立つかもしれないと思った。ありがとうジェックス
ボタンがクリックされたときに、ボタンの下にコンテキスト メニューを表示します。
InfragisticsにはWinDropDownButtonがあります:http://help.infragistics.com/Help/NetAdvantage/WinForms/2012.1/CLR2.0/html/WinDropDownButton_About_WinDropDownButton.html
したがって、それは確かに存在しますが、有料のサードパーティのコントロールを探しているわけではありません。