gpt4 book ai didi

c# - ContextMenuStrip 在指定控件上方绘制不当

转载 作者:行者123 更新时间:2023-11-30 17:47:49 28 4
gpt4 key购买 nike

我正在尝试为我的应用程序创建一个专门的工具栏,它由几个按钮组成,单击这些按钮时应在它们上方显示一个 ContextMenuStrip 并提供一些选择。看来我无法将此菜单绘制在正确的位置。

下面是重现问题的 SSCCE,这里有几张显示问题的图片。

关于如何解决这个问题有什么想法吗?

enter image description here

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.testToolbar1 = new ContextMenuTest.TestToolbar();
this.SuspendLayout();
//
// testToolbar1
//
this.testToolbar1.Location = new System.Drawing.Point(12, 12);
this.testToolbar1.Name = "testToolbar1";
this.testToolbar1.Size = new System.Drawing.Size(321, 23);
this.testToolbar1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(352, 51);
this.Controls.Add(this.testToolbar1);
this.IsMdiContainer = true;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private TestToolbar testToolbar1;
}
}

测试工具栏.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ContextMenuTest
{
public partial class TestToolbar : UserControl
{
public TestToolbar()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
curType = contectType.One;
fromControl = button1;
showContextStrip();
}

private void button2_Click(object sender, EventArgs e)
{
curType = contectType.Two;
fromControl = button2;
showContextStrip();
}

private void button3_Click(object sender, EventArgs e)
{
curType = contectType.Three;
fromControl = button3;
showContextStrip();
}

private void button4_Click(object sender, EventArgs e)
{
curType = contectType.Four;
fromControl = button4;
showContextStrip();
}

contectType curType;
Control fromControl;
enum contectType { One, Two, Three, Four }

private void showContextStrip()
{
int numShow = 0;
switch (curType)
{
case contectType.One:
{
numShow = 1;
} break;
case contectType.Two:
{
numShow = 2;
} break;
case contectType.Three:
{
numShow = 3;
} break;
case contectType.Four:
{
numShow = 4;
} break;
}

contextMenuStrip1.Items.Clear();

for (int i = 0; i < numShow; i++)
{
ToolStripMenuItem newItem = new ToolStripMenuItem();
newItem.Text = i.ToString();
contextMenuStrip1.Items.Add(newItem);
}

Console.Out.WriteLine("1Pos: " + button1.Location);
Console.Out.WriteLine("2Pos: " + button2.Location);
Console.Out.WriteLine("3Pos: " + button3.Location);
Console.Out.WriteLine("4Pos: " + button4.Location);
Console.Out.WriteLine("Ctrl: " + fromControl.Location);

contextMenuStrip1.Show(fromControl, fromControl.Location, ToolStripDropDownDirection.AboveRight);
}

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Component Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(3, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(84, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(165, 0);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(246, 0);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 3;
this.button4.Text = "button4";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Name = "contextMenuStrip1";
//
// TestToolbar
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "TestToolbar";
this.Size = new System.Drawing.Size(321, 23);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
}
}

最佳答案

啊,看来我的英语知识让我误入歧途了。

Positions the ToolStripDropDown relative to the specified control at the specified location and with the specified direction relative to the parent control. (Inherited from ToolStripDropDown.)

这个:

contextMenuStrip1.Show(fromControl, fromControl.Location, ToolStripDropDownDirection.AboveRight);

应该是这样的:

Point origin = new Point(0, 0);
contextMenuStrip1.Show(fromControl, origin, ToolStripDropDownDirection.AboveRight);

关于c# - ContextMenuStrip 在指定控件上方绘制不当,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24183424/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com