【发布时间】:2015-08-26 09:18:37
【问题描述】:
我正在尝试创建一个以表格为中心的标签,该表格需要我使用label.dock = dockStyle.Fill。因此我尝试实现代码,这意味着当我按住标签时,我可以移动表单。这是我到目前为止所拥有的:
private void messageIndicator_MouseUp(object sender, MouseEventArgs e)
{
window.AllowTransparency = true;
window.TransparencyKey = window.TransparencyKey = window.BackColor;
isDragging = false;
}
void messageIndicator_MouseHover(object sender, EventArgs e)
{
window.AllowTransparency = false;
}
private void messageIndicator_MouseDown(object sender, MouseEventArgs e)
{
// Set the drag mode
isDragging = true;
// Get the initial location
lastLocation = e.Location;
}
private void messageIndicator_MouseMove(object sender, MouseEventArgs e)
{
// Only drag if in correct state (mouse down)
if (isDragging)
{
// The parameter sender is the form object
Form f = (Form)sender;
// Calculate the new location and update the form
f.Location = new Point((f.Location.X - lastLocation.X) + e.X, (f.Location.Y - lastLocation.Y) + e.Y);
f.Update();
}
}
messageIndicator 是表单上的标签。执行Form f = (form)sender 时确实会崩溃。非常感谢您的帮助:)
【问题讨论】:
-
不,发件人必须是
Label。它引发了什么异常? -
Pear GIS.exe 中发生“System.InvalidCastException”类型的未处理异常附加信息:无法将“System.Windows.Forms.Label”类型的对象转换为“System.Windows.Forms”类型.Form'。
标签: c# forms window label mouseevent