コンテキストメニューで行っている私は、実際のコードエディターのように右クリックすると、次のようになります。
そして、このコードを使用して、カット、コピー、および貼り付けを既に完了しています。
private void rtb_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MenuItem[] menuItems = new MenuItem[] {
new MenuItem("Cut", new System.EventHandler(this.CutMenuItemClick)),
new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick)),
new MenuItem("Paste", new System.EventHandler(this.PasteMenuItemClick)),
ContextMenu rightcontext = new ContextMenu(menuItems);
int xOffset = Cursor.Position.X - DtexteditoR.ActiveForm.Location.X;
int yOffset = Cursor.Position.Y - DtexteditoR.ActiveForm.Location.Y;
rightcontext.Show(DtexteditoR.ActiveForm, new Point(xOffset, yOffset));
}
}
private void CutMenuItemClick(object sender, EventArgs e)
{
rtb.Cut();
}
private void CopyMenuItemClick(object sender, EventArgs e)
{
rtb.Copy();
}
private void PasteMenuItemClick(object sender, EventArgs e)
{
rtb.Paste();
}
動的コントロールでwinformを使用しています(デザイナーを使用しないでください)。私の質問は、コントロール(異なるハンドラー)で複数のイベントハンドラーを次のようにする方法に関するものでした:
new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick) || new System.Windows.Forms.MeasureItemEventHandler(this.MeasureCopy)),
private void MeasureCopy(object obj,
MeasureItemEventArgs miea)
{
MenuItem mi = (MenuItem)obj;
// Get standard menu font so that the text in this
// menu rectangle doesn't look funny with a
// different font
Font menuFont = SystemInformation.MenuFont;
StringFormat strfmt = new StringFormat();
SizeF sizef =
miea.Graphics.MeasureString(mi.Text, menuFont, 1000, strfmt);
// Get image so size can be computed
Bitmap bmMenuImage = new Bitmap(typeof(NewForm), "COPY.BMP");
// Add image height and width to the text height and width when
// drawn with selected font (got that from measurestring method)
// to compute the total height and width needed for the rectangle
miea.ItemWidth = (int)Math.Ceiling(sizef.Width) + bmMenuImage.Width;
miea.ItemHeight = (int)Math.Ceiling(sizef.Height) + bmMenuImage.Height;
}
「コピー」の横に画像を追加できるようにします。
このことを行う方法:
new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick) || new System.Windows.Forms.MeasureItemEventHandler(this.MeasureCopy)),
正しい方法。ありがとう!