[英]Detect switching between 2 instances of UserControl
我有一個派生自UserControl
的類(如下)。 用戶一次可以打開此控件的多個實例。 我希望能夠檢測到用戶何時從一個切換到另一個,即單擊了一個然后又單擊了另一個。
僅在首次顯示控件時OnEnter
和OnLoad
永遠不會調用OnLeave
。
從不調用OnGotFocus
和OnLostFocus
,但顯然不鼓勵使用它們。
我需要做什么?
public partial class MyView : System.Windows.Forms.UserControl
{
// Before `OnLoad`
// WM_NCCREATE, WM_NCCALCSIZE WM_CREATE, WM_SIZE, WM_MOVE,WM_REFLECT, WM_SHOWWINDOW, WM_PARENTNOTIFY
// After `OnLoad`, Before `OnEnter`
// WM_WINDOWPOSCHANGING, WM_NCCALCSIZE, WM_WINDOWPOSCHANGED, WM_SIZE, WM_GETTEXTLENGTH, WM_GETTEXT, (WM_WINDOWPOSCHANGING, WM_NCCALCSIZE, WM_WINDOWPOSCHANGED, WM_SIZE, WM_CHILDACTIVATE, WM_MOVE, WM_SHOWWINDOW
private const int WM_ACTIVATE = 0x006;
protected override void WndProc(ref Message message)
{
if (message.Msg == WM_ACTIVATE)
{
System.Diagnostics.Trace.WriteLine("MyView.WndProc(): message: " + message);
}
base.WndProc(ref message);
}
// called before OnEnter
protected override void OnLoad(System.EventArgs eventArgs)
{
base.OnLoad(eventArgs);
Form form1 = this.FindForm(); // null
Form parentForm = this.ParentForm; // null
}
// called only on first display, not when the user switches between the 2 controls
protected override void OnEnter(EventArgs eventArgs)
{
base.OnEnter(eventArgs);
}
protected override void OnLeave(EventArgs eventArgs)
{
base.OnLeave(eventArgs);
}
// Never called
protected override void OnGotFocus(EventArgs eventArgs)
{
base.OnGotFocus(eventArgs);
}
// Never called
protected override void OnLostFocus(EventArgs eventArgs)
{
base.OnGotFocus(eventArgs);
}
}
這是用戶派生控件的完整源代碼。 它按原樣工作。
在表單上,我創建了該控件的兩個實例,然后可以單擊它們。 正如我所做的那樣,方塊從紅色切換為藍色。
您應該能夠在OnGotFocus和OnLostFocus方法中設置斷點。 無效者在那里強制重新粉刷。
魔術是有兩個控件(也有兩個控件),但此外,OnButtonclicked覆蓋設置了焦點。 我相信這是您的示例中缺少的部分。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DesktopApp1
{
public partial class MyView : System.Windows.Forms.UserControl
{
protected Color SelectedColor { get; set; } = Color.Red;
protected Color NormalColor { get; set; } = Color.Blue;
protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush blueBrush = new SolidBrush(this.Focused?SelectedColor:NormalColor))
using (Pen blackPen = new Pen(Color.Black, 3))
{
e.Graphics.FillRectangle(blueBrush, ClientRectangle);
Rectangle inset = new Rectangle(this.ClientRectangle.X + 1, this.ClientRectangle.Y + 1, this.ClientRectangle.Width -3 , this.ClientRectangle.Height - 3);
e.Graphics.DrawRectangle(blackPen, inset);
}
base.OnPaint(e);
}
private void OnButton1Clicked(object sender, EventArgs e)
{
this.Select();
}
protected override void OnGotFocus(EventArgs e)
{
Invalidate();
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
Invalidate();
}
}
}
要完成答案,這里是form1.Designer.cs
namespace DesktopApp1
{
partial class Form1
{
/// <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.myView2 = new DesktopApp1.MyView();
this.myView1 = new DesktopApp1.MyView();
this.SuspendLayout();
//
// myView2
//
this.myView2.Location = new System.Drawing.Point(189, 12);
this.myView2.Name = "myView2";
this.myView2.Size = new System.Drawing.Size(150, 150);
this.myView2.TabIndex = 1;
//
// myView1
//
this.myView1.Location = new System.Drawing.Point(9, 12);
this.myView1.Name = "myView1";
this.myView1.Size = new System.Drawing.Size(150, 150);
this.myView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(355, 175);
this.Controls.Add(this.myView2);
this.Controls.Add(this.myView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private MyView myView1;
private MyView myView2;
}
}
要求:“我希望能夠檢測到操作員...何時單擊了一個,然后單擊了另一個。”
顯然,您希望在控件或其子控件上單擊時得到通知。 為此,您必須預訂所有子控件的Click事件。
protected override void OnControlAdded(ControlEventArgs e)
{
// subscribe to click event of e.Control
e.Control.Click += OnChildClicked;
}
// TODO: de-subscribe in OnControlRemoved
public void OnChildClicked(object Sender, EventArgs e)
{
// Clicked on child control, act as if Clicked on me
this.OnClick(e);
}
單擊事件將由任何類似單擊的事件調用,即使它是通過鍵盤完成的,例如通過按Enter鍵。
您寫了您想要的鼠標單擊。 如果您確實只希望單擊鼠標,請使用MouseClick事件。 如果操作員使用TAB選擇您的子控件之一,然后按Enter鍵單擊它,則不會收到通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.