Private Const WM_LBUTTONDOWN As Integer = &H201 Public Structure MOUSEHOOKSTRUCT Public pt As Point Public hWnd As IntPtr Public hitTest As Int32 Public extra As Int32 End Structure Public Delegate Function CallBack(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As Int32 <MarshalAs(UnmanagedType.FunctionPtr)> Private _mouseProc As CallBack <MarshalAs(UnmanagedType.FunctionPtr)> Private _keyboardProc As CallBack Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As CallBack, ByVal hInstance As IntPtr, ByVal wParam As Int32) As Int32 Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Int32, ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As Int32 Private Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Integer Private Sub Frm_Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load _mouseProc = New CallBack(AddressOf MouseHook) SetWindowsHookExW(7, _mouseProc, IntPtr.Zero, GetCurrentThreadId()) End Sub Public Function MouseHook(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As Int32 If nCode >= 0 Then If wParam = WM_LBUTTONDOWN Then Dim con As Control = Form.FromHandle(lParam.hWnd) If TypeOf con Is Button Then MsgBox(con.Name) End If End If End If Return CallNextHookEx(7, nCode, wParam, lParam) End Function
把每个按钮的Click事件指向这个方法
private void Click(object sender, EventArgs e)
{
///显示当前激活控件名称
MessageBox.Show(ActiveControl.Name);
}
附:Java代码package jianan;
/**
* Created by IntelliJ IDEA.
* User: Christen
* Date: 2008-2-4
* Time: 9:43:51
*/
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
public Counter extends Frame{
//声明 3个面板布局
GridLayout gl1,gl2,gl3;
Panel p0,p1,p2,p3;
JTextField tf1;
TextField tf2;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26;
StringBuffer str;//显示屏所显示串
double x,y;//x和y都是运算数
z;//Z表示单击了那个运算符.0表示"+",1表示"-",2表示"*",3表示"/"
double m;//记忆数字
public Counter{
gl1= GridLayout(1,4,10,0);//例子化 3个面板布局
gl2= GridLayout(4,1,0,15);
gl3= GridLayout(4,5,10,15);
tf1= JTextField(27);//显示屏
tf1.HorizontalAlignment(JTextField.RIGHT);
tf1.Enabled(false);
tf1.Text("0");
tf2= TextField(10);//显示记忆索引值
tf2.Editable(false);
//例子化所有按钮、设置其前景色并注册监听器
b0= Button("Backspace");
b0.Foreground(Color.red);
b0.addActionListener( Bt);
b1= Button("CE");
b1.Foreground(Color.red);
b1.addActionListener( Bt);
b2= Button("C");
b2.Foreground(Color.red);
b2.addActionListener( Bt);
b3= Button("MC");
b3.Foreground(Color.red);
b3.addActionListener( Bt);
b4= Button("MR");
b4.Foreground(Color.red);
b4.addActionListener( Bt);
b5= Button("MS");
b5.Foreground(Color.red);
b5.addActionListener( Bt);
b6= Button("M+");
b6.Foreground(Color.red);
b6.addActionListener( Bt);
b7= Button("7");
b7.Foreground(Color.blue);
b7.addActionListener( Bt);
b8= Button("8");
b8.Foreground(Color.blue);
b8.addActionListener( Bt);
b9= Button("9");
b9.Foreground(Color.blue);
b9.addActionListener( Bt);
b10= Button("/");
b10.Foreground(Color.red);
b10.addActionListener( Bt);
b11= Button("sqrt");
b11.Foreground(Color.blue);
b11.addActionListener( Bt);
b12= Button("4");
b12.Foreground(Color.blue);
b12.addActionListener( Bt);
b13= Button("5");
b13.Foreground(Color.blue);
b13.addActionListener( Bt);
b14= Button("6");
b14.Foreground(Color.blue);
b14.addActionListener( Bt);
b15= Button("*");
b15.Foreground(Color.red);
b15.addActionListener( Bt);
b16= Button("%");
b16.Foreground(Color.blue);
b16.addActionListener( Bt);
b17= Button("1");
b17.Foreground(Color.blue);
b17.addActionListener( Bt);
b18= Button("2");
b18.Foreground(Color.blue);
b18.addActionListener( Bt);
b19= Button("3");
b19.Foreground(Color.blue);
b19.addActionListener( Bt);
b20= Button("-");
b20.Foreground(Color.red);
b20.addActionListener( Bt);
b21= Button("1/X");
b21.Foreground(Color.blue);
b21.addActionListener( Bt);
b22= Button("0");
b22.Foreground(Color.blue);
b22.addActionListener( Bt);
b23= Button("+/-");
b23.Foreground(Color.blue);
b23.addActionListener( Bt);
b24= Button(".");
b24.Foreground(Color.blue);
b24.addActionListener( Bt);
b25= Button("+");
b25.Foreground(Color.red);
b25.addActionListener( Bt);
b26= Button("=");
b26.Foreground(Color.red);
b26.addActionListener( Bt); //例子化 4个面板
p0= Panel;
p1= Panel;
p2= Panel;
p3= Panel;
//创建个空串缓冲区
str= StringBuffer;
//添加面板p0中组件和设置其在框架中位置和大小
p0.add(tf1);
p0.Bounds(10,25,300,40);
//添加面板p1中组件和设置其在框架中位置和大小
p1.Layout(gl1);
p1.add(tf2);
p1.add(b0);
p1.add(b1);
p1.add(b2);
p1.Bounds(10,65,300,25);
//添加面板p2中组件并设置其框架中位置和大小
p2.Layout(gl2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.Bounds(10,110,40,150);
//添加面板p3中组件并设置其在框架中位置和大小
p3.Layout(gl3);//设置p3布局
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(b10);
p3.add(b11);
p3.add(b12);
p3.add(b13);
p3.add(b14);
p3.add(b15);
p3.add(b16);
p3.add(b17);
p3.add(b18);
p3.add(b19);
p3.add(b20);
p3.add(b21);
p3.add(b22);
p3.add(b23);
p3.add(b24);
p3.add(b25);
p3.add(b26);
p3.Bounds(60,110,250,150);
//设置框架中布局为空布局并添加4个面板
Layout(null);
add(p0);
add(p1);
add(p2);
add(p3);
Title("计算器");
Resizable(false);//禁止调整框架大小
//匿名类关闭窗口
addWindowListener( WindowAdapter{
public void windowClosing(WindowEvent e1)
{
.exit(0);
}
});
Background(Color.lightGray);
Bounds(100,100,320,280);
Visible(true);
//设置图标
this.IconImage(Toolkit.getDefaultToolkit.createImage("images/2.jpg"));
}
//构造监听器
Bt implements ActionListener
{
public void actionPerformed(ActionEvent e2)
{
try{
(e2.getSourceb1)//选择"CE"清零
{
tf1.Text("0");//把显示屏清零
str.Length(0);//清空串缓冲区以准备接收新输入运算数
}
(e2.getSourceb2)//选择"C"清零
{
tf1.Text("0");//把显示屏清零
str.Length(0);
}
(e2.getSourceb23)//单击"+/-"选择输入运算数是正数还是负数
{
x=Double.parseDouble(tf1.getText.trim);
tf1.Text(""+(-x));
}
(e2.getSourceb25)//单击加号按钮获得x值和z值并清空y值
{
x=Double.parseDouble(tf1.getText.trim);
str.Length(0);//清空缓冲区以便接收新另个运算数
y=0d;
z=0;
}
(e2.getSourceb20)//单击减号按钮获得x值和z值并清空y值
{
x=Double.parseDouble(tf1.getText.trim);
str.Length(0);
y=0d;
z=1;
}
(e2.getSourceb15)//单击乘号按钮获得x值和z值并清空y值
{
x=Double.parseDouble(tf1.getText.trim);
str.Length(0);
y=0d;
z=2;
}
(e2.getSourceb10)//单击除号按钮获得x值和z值并空y值
{
x=Double.parseDouble(tf1.getText.trim);
str.Length(0);
y=0d;
z=3;
}
(e2.getSourceb26)//单击等号按钮输出计算结果
{
str.Length(0);
switch(z)
{
0 : tf1.Text(""+(x+y));;
1 : tf1.Text(""+(x-y));;
2 : tf1.Text(""+(x*y));;
3 : tf1.Text(""+(x/y));;
}
}
(e2.getSourceb24)//单击"."按钮输入小数
{
(tf1.getText.trim.indexOf(".")!=-1)
//判断串中是否已经包含了小数点
{
//do nothing
}
//如果没数点有小
{
(tf1.getText.trim.equals("0"))//如果初时显示为0
{
str.Length(0);
tf1.Text((str.append("0"+e2.getActionCommand)).toString);
}
(tf1.getText.trim.equals(""))//如果初时显示为空则不做任何操作
{
}
{
tf1.Text(str.append(e2.getActionCommand).toString);
}
}
y=0d;
}
(e2.getSourceb11)//求平方根
{
x=Double.parseDouble(tf1.getText.trim);
tf1.Text("数字格式异常");
(x<0)
tf1.Text("负数没有平方根");
tf1.Text(""+Math.sqrt(x));
str.Length(0);
y=0d;
}
(e2.getSourceb16)//单击了"%"按钮
{
x=Double.parseDouble(tf1.getText.trim);
tf1.Text(""+(0.01*x));
str.Length(0);
y=0d;
}
(e2.getSourceb21)//单击了"1/X"按钮
{
x=Double.parseDouble(tf1.getText.trim);
(x0)
{
tf1.Text("除数不能为零");
}
{
tf1.Text(""+(1/x));
}
str.Length(0);
y=0d;
}
(e2.getSourceb3)//MC为清除内存
{
m=0d;
tf2.Text("");
str.Length(0);
}
(e2.getSourceb4)//MR为重新存储数据
{
(tf2.getText.trim!="")//有记忆数字
{
tf1.Text(""+m);
}
}
(e2.getSourceb5)//MS为存储显示数据
{ m=Double.parseDouble(tf1.getText.trim);
tf2.Text("M");
tf1.Text("0");
str.Length(0);
}
(e2.getSourceb6)//M+为将显示数字和已经存储数据相加要查看新数字单击MR
{
m=m+Double.parseDouble(tf1.getText.trim);
}
//选择是其他按钮
{
(e2.getSourceb22)//如果选择是"0"这个数字键
{
(tf1.getText.trim.equals("0"))//如果显示屏显示为零不做操作
{
}
{
tf1.Text(str.append(e2.getActionCommand).toString);
y=Double.parseDouble(tf1.getText.trim);
}
}
(e2.getSourceb0)//选择是“BackSpace”按钮
{
(!tf1.getText.trim.equals("0"))//如果显示屏显示不是零
{
(str.length!=1)
{
tf1.Text(str.delete(str.length-1,str.length).toString);//可能抛出串越界异常
}
{
tf1.Text("0");
str.Length(0);
}
}
y=Double.parseDouble(tf1.getText.trim);
}
//其他数字键
{
tf1.Text(str.append(e2.getActionCommand).toString);
y=Double.parseDouble(tf1.getText.trim);
}
}
}
catch(NumberFormatException e){
tf1.Text("数字格式异常");
}
catch(StringIndexOutOfBoundsException e){
tf1.Text("串索引越界");
}
}
}
public void (String args)
{
Counter;
}
}
Bt implements ActionListener
{}C#有没有类似这样的Listener
Private Const WM_LBUTTONDOWN As Integer = &H201 Public Structure MOUSEHOOKSTRUCT
Public pt As Point
Public hWnd As IntPtr
Public hitTest As Int32
Public extra As Int32
End Structure Public Delegate Function CallBack(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As Int32 <MarshalAs(UnmanagedType.FunctionPtr)> Private _mouseProc As CallBack
<MarshalAs(UnmanagedType.FunctionPtr)> Private _keyboardProc As CallBack
Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As CallBack, ByVal hInstance As IntPtr, ByVal wParam As Int32) As Int32
Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Int32, ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As Int32
Private Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Integer Private Sub Frm_Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
_mouseProc = New CallBack(AddressOf MouseHook)
SetWindowsHookExW(7, _mouseProc, IntPtr.Zero, GetCurrentThreadId())
End Sub Public Function MouseHook(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As Int32
If nCode >= 0 Then
If wParam = WM_LBUTTONDOWN Then
Dim con As Control = Form.FromHandle(lParam.hWnd) If TypeOf con Is Button Then
MsgBox(con.Name)
End If
End If
End If Return CallNextHookEx(7, nCode, wParam, lParam)
End Function
{
Button b=(Button)sender;
MessageBox.Show(b.Name.ToString());
}
Private void Form_Load(object Sender,EventArgs e)
{
button1.Click+=new EventHandler(Button_Click);
button2.Click+=new EventHandler(Button_Click);
button3.Click+=new EventHandler(Button_Click);
button4.Click+=new EventHandler(Button_Click);
....
}
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;
using System.Runtime.InteropServices; namespace winform1218
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const int INTERNET_CONNECTION_MODEM = 1;
private const int INTERNET_CONNECTION_LAN = 2;
[DllImport("winInet.dll")]
private static extern bool InternetGetConnectedState(ref int dwFlag,int dwReserved);
private void button1_Click(object sender, EventArgs e)
{
//测试网络连接状态
//System.Int32 dwFlag = new int();
//if (!InternetGetConnectedState(ref dwFlag, 0))
// MessageBox.Show("未连网!");
//else
// if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0)
// MessageBox.Show("采用调治解调器上网。");
// else
// if ((dwFlag & INTERNET_CONNECTION_LAN) != 0)
// MessageBox.Show("采用网卡上网。");
} private void Click(object sender, EventArgs e)
{
textBox1.AppendText(ActiveControl.Name+"\r\n");
}
}
}
namespace winform1218
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 376);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 60);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 12);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(239, 349);
this.textBox1.TabIndex = 1;
//
// button2
//
this.button2.Location = new System.Drawing.Point(95, 376);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 60);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(418, 31);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 4;
this.label1.Text = "label1";
//
// button3
//
this.button3.Location = new System.Drawing.Point(176, 376);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 60);
this.button3.TabIndex = 5;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(566, 448);
this.Controls.Add(this.button3);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button3;
}
}