2014-06-08 24 views
0

我想從一個控件傳遞一個事件(MouseWheel)到另一個控件。 當第一個控件捕獲事件時,它應該在第二個控件上調用此事件的默認處理程序。這是我嘗試做的一個僞代碼。如何將事件傳遞給c#winforms中的另一個控件?

void Control1_MouseWheel(object sender, MouseEventArgs e) 
{ 
    //do something 
    Control2_MouseWheel(sender,e); 
} 

編輯:Control2是一個COM接口,它的處理程序不寫我。

+0

沒有什麼理由讓它成爲僞代碼,直接調用事件處理程序方法並不是完全不尋常的。如果您願意,可以通過禁止SendMessage()來使其複雜化。 –

回答

1

您可以將兩個控件設置爲具有相同的事件處理程序。 (由設計師或代碼)

Control1.MouseWheel += CommonMouseWheelHandler; 
Control2.MouseWheel += CommonMouseWheelHandler; 


protected void CommonMouseWheelHandler(object sender, MouseEventArgs e) 
{ 
    ... your common code here... 
} 
+0

Control2是一個COM接口,它的處理程序不是我寫的。 –

0

可以使用方法SendMessage發送WM_MOUSEWHEEL消息發送到所述第二控制:

SendMessage(destWindowHandle, m.Msg, (int)m.WParam, (int)m.LParam); 

下面是樣品與2個列表框,滾動一個滾動另一個。

using System; 
using System.Collections.Generic; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace WindowsFormsApplication18 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1()); 
     } 



    } 

    public class MessageFilter : IMessageFilter 
    { 
     IntPtr sourceWindowHandle; 
     IntPtr destWindowHandle; 


     [DllImport("user32.dll", CharSet = CharSet.Auto)] 
     public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); 

     public MessageFilter(IntPtr sourceHandle, IntPtr destHandle) 
     { 
     sourceWindowHandle = sourceHandle; 
     destWindowHandle = destHandle; 
     } 
     public bool PreFilterMessage(ref Message m) 
     { 
     if (m.HWnd == sourceWindowHandle && m.Msg == 0x020A)// mousewheel 
      SendMessage(destWindowHandle, m.Msg, (int)m.WParam, (int)m.LParam); 
     return false; 
     } 
    } 



    public class Form1 : Form 
    { 


     /// <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.listBox1 = new System.Windows.Forms.ListBox(); 
     this.listBox2 = new System.Windows.Forms.ListBox(); 
     this.SuspendLayout(); 
     // 
     // listBox1 
     // 
     this.listBox1.FormattingEnabled = true; 
     this.listBox1.Items.AddRange(new object[] { 
      "1", 
      "2", 
      "3", 
      "4", 
      "5", 
      "6", 
      "7", 
      "8", 
      "9", 
      "10"}); 
     this.listBox1.Location = new System.Drawing.Point(56, 48); 
     this.listBox1.Name = "listBox1"; 
     this.listBox1.Size = new System.Drawing.Size(120, 95); 
     this.listBox1.TabIndex = 0; 
     // 
     // listBox2 
     // 
     this.listBox2.FormattingEnabled = true; 
     this.listBox2.Items.AddRange(new object[] { 
      "1", 
      "2", 
      "3", 
      "4", 
      "5", 
      "6", 
      "7", 
      "8", 
      "9", 
      "10"}); 
     this.listBox2.Location = new System.Drawing.Point(280, 48); 
     this.listBox2.Name = "listBox2"; 
     this.listBox2.Size = new System.Drawing.Size(120, 95); 
     this.listBox2.TabIndex = 1; 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(501, 361); 
     this.Controls.Add(this.listBox2); 
     this.Controls.Add(this.listBox1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.ResumeLayout(false); 

     } 

     #endregion 

     private System.Windows.Forms.ListBox listBox1; 
     private System.Windows.Forms.ListBox listBox2; 
     public Form1() 
     { 
     InitializeComponent(); 
     Application.AddMessageFilter(new MessageFilter(listBox1.Handle, listBox2.Handle)); 

     } 


    } 
} 
相關問題