我使用面板和两个标签的组合制作了一个用户控件。我在用户控件上添加了mouseenter和mouseleave事件。我将用户控件放在表单上。我想在单击控件时打开一个新表单。但默认点击或鼠标点击事件不起作用。我怎么解决这个?
以下是用户控制代码:
static void Main()
{
public static Form1 s_mainForm = null;
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
s_formShutdownMsg = new FormMessage();
s_mainForm = new Form1();
Application.Run(s_mainForm);
}
catch (Exception e)
{
}
}
public static FormMessage s_formShutdownMsg = null; //somewhere in the project
这是用户控制点击事件代码:
public partial class group_control : UserControl
{
public group_control()
{
InitializeComponent();
}
public void setname(string s)
{
namelbl.Text = s;
}
public void setno(int n)
{
no_acc.Text = n.ToString();
}
private void logo_MouseEnter(object sender, EventArgs e)
{
logo.BackColor = System.Drawing.Color.Yellow;
no_acc.Visible = true;
label1.Visible = true;
namelbl.Visible = false;
}
private void logo_MouseLeave(object sender, EventArgs e)
{
logo.BackColor = System.Drawing.Color.Gray;
no_acc.Visible = false;
label1.Visible = false;
namelbl.Visible = true;
}
private void logo_Click(object sender, EventArgs e)
{
Program.cur_grp = namelbl.Text;
}
private void no_acc_Click(object sender, EventArgs e)
{
logo_Click(sender, e);
}
private void label1_Click(object sender, EventArgs e)
{
logo_Click(sender, e);
}
private void namelbl_Click(object sender, EventArgs e)
{
logo_Click(sender, e);
}
private void no_acc_MouseEnter(object sender, EventArgs e)
{
logo_MouseEnter(sender, e);
}
private void namelbl_MouseEnter(object sender, EventArgs e)
{
logo_MouseEnter(sender, e);
}
private void label1_MouseEnter(object sender, EventArgs e)
{
logo_MouseEnter(sender, e);
}
private void no_acc_MouseLeave(object sender, EventArgs e)
{
logo_MouseLeave(sender, e);
}
private void label1_MouseLeave(object sender, EventArgs e)
{
logo_MouseLeave(sender, e);
}
private void namelbl_MouseLeave(object sender, EventArgs e)
{
logo_MouseLeave(sender, e);
}
}
答案 0 :(得分:0)
如果您点击面板,那么面板将获得鼠标点击事件。如果您希望在用户控件上执行该事件,那么您的面板应该对鼠标单击是透明的。你就是这样做的;
public class ClickTransparentPanel : Panel
{
protected override void WndProc(ref Message m)
{
// To make panel "transparent"
// to mouse clicks i.e. Mouse clicks pass through this window
switch (m.Msg)
{
case (int) Win32Constants.WM_NCHITTEST:
m.Result = new IntPtr((int) Win32Constants.HTTRANSPARENT);
return;
}
base.WndProc(ref m);
}
}
其中
WM_NCHITTEST = 0x0084,
HTTRANSPARENT = -1