.NET中,使用c# 编的,我怎样在form上获取鼠标位置,下面这个加进去,没反应。还有能在Picturebox中获取鼠标坐标吗private void Form1_MouseMove(object sender, MouseEventArgs e)
{
label1 .Text ="x坐标:"+e.X.ToString () ;
label2.Text = "y坐标:" + e.Y.ToString(); }
{
label1 .Text ="x坐标:"+e.X.ToString () ;
label2.Text = "y坐标:" + e.Y.ToString(); }
{
label1 .Text ="x坐标:" + e.X.ToString () ;
label2.Text = "y坐标:" + e.Y.ToString(); } private void panel1_MouseEnter(object sender, EventArgs e)
{
this.Capture = true;
} private void Form1_MouseLeave(object sender, EventArgs e)
{
this.Capture = false;
}
int x = p.X;
int y = p.Y;
label1.Text = String.Format("Mouse X:{0}, Mouse Y :{1}", x, y);
或
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point pt); Point CursorPosition; GetCursorPos(out CursorPosition);
label1.Text = "X: " + CursorPosition.X.ToString() + " Y: " + CursorPosition.Y.ToString();
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point p= = new Point(e.X, e.Y);
txtStartX.Text = p.X.ToString();
txtStartY.Text = p.Y.ToString();
}
在任何地方使用都是返回当前鼠标的point
他那个是PictureBoxShow控件,显示是个WIN32控件,那种控件多数不支持鼠标事件的,所以他才用Form上的鼠标事件。现在是事件捕获的问题,而不是鼠标位置的获取问题。都不能响应鼠标点击事件的话,如何显示鼠标坐标?
如果实在不行,那我就在上面生成一个form,但是怎样让两个form的右边和上边对齐呢,急救!!!
{
public form1()
{
Application.AddMessageFilter(this);
}
bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
int WM_MOUSEMOVE=0x0200; if (m.msg==WM_MOUSEMOVE)
{
int X = lparam 低位
int Y = lparam 高位
}
return false;
}
一个是Form1还有一个是Form3, 我在Form1添加一个按钮,按下按钮启动了Form3,但是我怎样让他们右边和上边对齐呢,高度又一样,也就是完全覆盖在上面,一样大,还有在Form3点击鼠标,在Form1上显示鼠标坐标
怎么办啊,大侠们,出点点子吧
那我怎样在两个Form上传递textbox的text内容呢
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text=e.X.ToString();
label2.Text = e.Y.ToString();
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = e.Location.ToString();
}
FMChild fmChild = new FMChild();
fmChild.ShowDialog();
fmChild.Dispose();
如果我们需要将主窗体FMMain中的string strValueA的值传给FMChild,那么我们首先对strValueA进行如下处理:private string strValueA;
public string StrValueA
{
get
{
return strValueA;
}
set
{
strValueA = value;
}
}
使其成为主窗体FMMain的一个属性,接着修改显示子窗体的代码为以下两种的其中一种。方法一:FMChild fmChild = new FMChild();
fmChild.ShowDialog(this);
fmChild.Dispose();
方法二:FMChild fmChild = new FMChild();
FMChild.Owner = this;
fmChild.ShowDialog();
fmChild.Dispose();
然后在修改子窗体FMChild中申明一个主窗体FMMain对象,FMMain fmMain;
在需要使用主窗体FMMain的string strValueA的地方加上如下代码:fmMain = (FMMain)this.Owner;
这样,就可以获得主窗体FMMain中strValueA的值了。这时,如果你需要将子窗体FMChild中的string strValueB传给主窗体FMMain,同样处理string strValueB。private string strValueB;
public string StrValueB
{
get { return strValueB; }
set { strValueB = value; }
}
那么你在关闭子窗体代码fmChild.Dispose();后,可以写一些代码来保存或者处理FMChild的strValueB,例如:string strTmp = fmChild.StrValueB;
注意,在Visual C#智能设备Pocket PC 2003设备应用程序中显示子窗体FMChild只能使用:FMChild fmChild = new FMChild();FMChild.Owner = this;fmChild.ShowDialog();fmChild.Dispose();
在Visual C#智能设备Pocket PC 2003的设备应用程序中ShowDialog()没有重载。C#窗体间传值的几种方法
调用窗体(父):Form1,被调用窗体(子):Form2
方法1: 所有权法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//在调用Form2时,要把Form2的所有者设为Form1
Form2 f2 = new Form2() ;
f2.Owner = this;
f2.ShowDialog() ;
//Form2:
//在需要对其调用者(父)刷新时
Form1 f1 ;
f1 = (Form1)this.Owner;
f1.Refresh_Method() ;
方法2:自身传递法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
Form2 f2 = new Form2() ;
f2.ShowDialog(this) ;
//Form2:
private Form1 p_f1;
public Form2(Form1 f1)
{
InitializeComponent();
p_f1 = f1;
}
//刷新时
p_f1.Refresh_Method() ;
方法3:属性法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//调用时
Form2 f2 = new Form2() ;
f2.P_F1 = this;
f2.Show() ;//Form2:
private Form1 p_f1;
public Form1 P_F1
{
get{return p_f1;}
set{p_f1 = value;}
}
//刷新时
p_f1.Refresh_Method() ;
方法4:委托法
//声明一个委托
public delegate void DisplayUpdate();
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//调用时
Form2 f2 = new Form2() ;
f2.ShowUpdate += new DisplayUpdate(Refresh_Method) ;
f2.Show() ;
//Form2:
//声明事件
public event DisplayUpdate ShowUpdate;
//刷新时,放在需要执行刷新的事件里
ShowUpdate();
Form1中放一个Label1,一个Button1,Form1为主窗口,当点击Button1时,执行:
Form2 f2 = new Form2();
f2.ShowDialog(this);Form2中放一个TextBox1,一个Button1,当点击Button1时执行:Form1 f1 = (Form1)this.Owner;
f1.Label1.Text = this.TextBox1.Text;注意:Form1中的label1要设为public用C#实现两个WinForm间的传值这个题目我曾经出给来公司面试的人,也曾经出给刚毕业的大学生,几乎是没有人会做啊。难道这个问题真的这么难么?现在我给出实现的主要源代码(开发平台为VC#2005)。实现的思路:
从Form1传递到Form2: 2个窗体即两个类,两个窗体间的数据传送,可以采用构造函数来实现。 从Form2返回到Form1,并传递数据:实例化Form2后,打f2用ShowDialog()方法,然后等待f2关闭时再回传数据到Form1。实现步骤及代码: 1:新建两个窗口: Form1,Form2;
2:打开Form2,添加一个textBox:textBox1;添加一个Button:button1;然后添加一个构造函数:
//定义一个变量,用来传值。
public string returnValue ; public Form2(string txtValue)
{
InitializeComponent(); this.textBox1.Text = txtValue;
}
然后在button1的单击事件中添加如下代码:
private void button1_Click(object sender, EventArgs e)
{
returnValue = this.textBox1.Text;
this.Close();
} 3:Form1中添加一个textBox:textBox1;添加一个Button:button1;然后在button1的单击事件中添加如下代码:
private void button1_Click(object sender, EventArgs e)
{
string txtValue = this.textBox1.Text;
Form2 f2 = new Form2(txtValue);
f2.ShowDialog();
this.textBox1.Text = f2.returnValue;
}
Form1 中 (父窗口:)
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnOpen;
public System.Windows.Forms.TextBox txtContent; //注意是public ........ ........ [STAThread]
static void Main()
{
Application.Run(new Form1());
} private void btnOpen_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(this);
frm.ShowDialog();
}}Form2中(子窗口)public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtValue; private Form _parentForm=null;
public Form2()
{
InitializeComponent();
} public Form2(Form parentForm)
{
InitializeComponent();
this._parentForm =parentForm;
} ........ ........
//更新父窗口中文本框中的值!
public class MyMessageFilter : IMessageFilter
{
public event EventHandler<EventArgs> MyMouseMove;
public void OnMyMouseMove(object sender, EventArgs e)
{
if (MyMouseMove != null)
{
MyMouseMove(sender, e);
}
}
public bool PreFilterMessage(ref Message objMessage)
{
if (objMessage.Msg == 0x0200)
{ // WM_MOUSEMOVE
OnMyMouseMove(this, new EventArgs());
return true;
}
return false;
}
}