C# Windows窗体间消息传递的问题
2013-04-26 20:40
465 查看
--
提出Windows窗体间消息传递的问题:如何根据子窗体的状态更改父窗体的状态?如双击DataGridView的行记录后弹出修改该行记录的子窗体,在修改完数据后希望DataGridView数据刷新,这就需要子窗体通知父窗体刷新数据!
考虑在子窗体上移动鼠标,将子窗体鼠标的坐标传递给父窗体的标题栏,如何实现?反过来在父窗体上移动鼠标,如何传递给子窗体?
经典的做法为方法回调,在父窗体类中注册子窗体的MouseMove事件调用父窗体的Method直接修改标题栏;当然我们也可以将父窗体的对象指针船体给子窗体对象的tag,然后在子窗体的MouseMove事件中将tag转换为父窗体对象,然后修改其标题栏文本。回调方案不会破坏程序的逻辑,也不会破坏程序的封装;而tag对象的传递方式必须要public公开tag对象实例的方法和属性才可以!
下面分别示例两种方式实现,考虑父窗体修改子窗体如何实现?!
//target: implement mouse move on child window, call father Window's Method
//implement: before Show the ChildWindow, give EventHandler to the childWindow
//and pick up a local method to the EvenHandler The Callback Method!!!!
using System;
using System.Windows.Forms;
namespace infoFromWindows
{
// TypDefName: infoFromWindows.frmFather (02000003)
// Flags : [NotPublic] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100000)
// Extends : 01000001 [TypeRef] System.Windows.Forms.Form
class frmFather : System.Windows.Forms.Form
{
private Button btnAbout;
private frmFather()
{
btnAbout = new Button();
btnAbout.ForeColor = System.Drawing.Color.Green;
btnAbout.Text="&About Me";
btnAbout.Click += new System.EventHandler(this.btnAboutClick);
this.Width=400;
this.Controls.Add(btnAbout);
}
//this method will be called from child window WHEN mouse move on Child Window
//notice here, private void ... question: who is the "sender"?
private void SetChildPosToFather(object sender, MouseEventArgs e)
{
string strPos = string.Format( "Child Mouse POS:({0},{1})",e.X,e.Y);
this.Text = strPos;
Form about = (Form) sender;
about.Width=400;
about.Text=string.Format( "Current Mouse POS:({0},{1})",e.X,e.Y);
}
private void btnAboutClick(object sender,System.EventArgs e)
{
Form about = new Form();
//here, we give the child window a EventHandler to call local Method!!!
about.MouseMove += new MouseEventHandler(SetChildPosToFather);
about.Show();
}
private static void Main()
{
frmFather father = new frmFather();
Application.Run(father);
}
}
}
tag传递
using System;
using System.Windows.Forms;
using System.Collections.Generic;
namespace infoFromWindows
{
public class frmAbout : Form
{
public frmAbout()
{
this.Text="I'm Child Window";
this.MouseMove += new MouseEventHandler(changeFatherText);
}
private void changeFatherText(object sender, MouseEventArgs e)
{
frmFather father = (frmFather) this.Tag;
father.Text = string.Format( "Child Mouse POS:({0},{1})",e.X,e.Y);
}
}
// TypDefName: infoFromWindows.frmFather (02000003)
// Flags : [NotPublic] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100000)
// Extends : 01000001 [TypeRef] System.Windows.Forms.Form
class frmFather : System.Windows.Forms.Form
{
private Button btnAbout;
private void btnAboutClick(object sender,System.EventArgs e)
{
frmAbout about = new frmAbout();
about.Tag = this;
//here,we transfer current Form POINT to "about" window,
//then from "about" window
//we can access current Form any PUBLIC method and properties.
about.Show();
}
public frmFather()
{
btnAbout = new Button();
btnAbout.ForeColor = System.Drawing.Color.Green;
btnAbout.Text="&About Me";
btnAbout.Click += new System.EventHandler(this.btnAboutClick);
this.Width=400;
this.Controls.Add(btnAbout);
}
public static void Main()
{
frmFather father = new frmFather();
Application.Run(father);
}
}
}
利用回调实现双窗口通讯,尝试开启多个窗口一起测试
using System;
using System.Windows.Forms;
using System.Collections.Generic;
namespace infoFromWindows
{
public class frmAbout : Form
{
public frmAbout()
{
this.Text="I'm Child Window";
}
//public, called by father window through MouseMove Event
public void ChangeColor(object sender, MouseEventArgs e)
{
this.BackColor = System.Drawing.Color.FromArgb(e.X,e.Y,0);
}
}
// TypDefName: infoFromWindows.frmFather (02000003)
// Flags : [NotPublic] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100000)
// Extends : 01000001 [TypeRef] System.Windows.Forms.Form
class frmFather : System.Windows.Forms.Form
{
private Button btnAbout;
public frmFather()
{
btnAbout = new Button();
btnAbout.ForeColor = System.Drawing.Color.Green;
btnAbout.Text="&About Me";
btnAbout.Click += new System.EventHandler(this.btnAboutClick);
this.Height=255;
this.Width=255;
this.MaximizeBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Controls.Add(btnAbout);
}
private void GetChildPosCallFromFather(object sender,MouseEventArgs e)
{
this.Text=string.Format("({0},{1})",e.X,e.Y);
}
private void btnAboutClick(object sender,System.EventArgs e)
{
frmAbout about = new frmAbout();
//here,we transfer current Form POINT to "about" window,
//then from "about" window
//we can access current Form any PUBLIC method and properties.
about.Show();
about.MouseMove += new MouseEventHandler(GetChildPosCallFromFather);
this.MouseMove += new MouseEventHandler(about.ChangeColor);
}
public static void Main()
{
frmFather father = new frmFather();
Application.Run(father);
}
}
}
提出Windows窗体间消息传递的问题:如何根据子窗体的状态更改父窗体的状态?如双击DataGridView的行记录后弹出修改该行记录的子窗体,在修改完数据后希望DataGridView数据刷新,这就需要子窗体通知父窗体刷新数据!
考虑在子窗体上移动鼠标,将子窗体鼠标的坐标传递给父窗体的标题栏,如何实现?反过来在父窗体上移动鼠标,如何传递给子窗体?
经典的做法为方法回调,在父窗体类中注册子窗体的MouseMove事件调用父窗体的Method直接修改标题栏;当然我们也可以将父窗体的对象指针船体给子窗体对象的tag,然后在子窗体的MouseMove事件中将tag转换为父窗体对象,然后修改其标题栏文本。回调方案不会破坏程序的逻辑,也不会破坏程序的封装;而tag对象的传递方式必须要public公开tag对象实例的方法和属性才可以!
下面分别示例两种方式实现,考虑父窗体修改子窗体如何实现?!
//target: implement mouse move on child window, call father Window's Method
//implement: before Show the ChildWindow, give EventHandler to the childWindow
//and pick up a local method to the EvenHandler The Callback Method!!!!
using System;
using System.Windows.Forms;
namespace infoFromWindows
{
// TypDefName: infoFromWindows.frmFather (02000003)
// Flags : [NotPublic] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100000)
// Extends : 01000001 [TypeRef] System.Windows.Forms.Form
class frmFather : System.Windows.Forms.Form
{
private Button btnAbout;
private frmFather()
{
btnAbout = new Button();
btnAbout.ForeColor = System.Drawing.Color.Green;
btnAbout.Text="&About Me";
btnAbout.Click += new System.EventHandler(this.btnAboutClick);
this.Width=400;
this.Controls.Add(btnAbout);
}
//this method will be called from child window WHEN mouse move on Child Window
//notice here, private void ... question: who is the "sender"?
private void SetChildPosToFather(object sender, MouseEventArgs e)
{
string strPos = string.Format( "Child Mouse POS:({0},{1})",e.X,e.Y);
this.Text = strPos;
Form about = (Form) sender;
about.Width=400;
about.Text=string.Format( "Current Mouse POS:({0},{1})",e.X,e.Y);
}
private void btnAboutClick(object sender,System.EventArgs e)
{
Form about = new Form();
//here, we give the child window a EventHandler to call local Method!!!
about.MouseMove += new MouseEventHandler(SetChildPosToFather);
about.Show();
}
private static void Main()
{
frmFather father = new frmFather();
Application.Run(father);
}
}
}
tag传递
using System;
using System.Windows.Forms;
using System.Collections.Generic;
namespace infoFromWindows
{
public class frmAbout : Form
{
public frmAbout()
{
this.Text="I'm Child Window";
this.MouseMove += new MouseEventHandler(changeFatherText);
}
private void changeFatherText(object sender, MouseEventArgs e)
{
frmFather father = (frmFather) this.Tag;
father.Text = string.Format( "Child Mouse POS:({0},{1})",e.X,e.Y);
}
}
// TypDefName: infoFromWindows.frmFather (02000003)
// Flags : [NotPublic] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100000)
// Extends : 01000001 [TypeRef] System.Windows.Forms.Form
class frmFather : System.Windows.Forms.Form
{
private Button btnAbout;
private void btnAboutClick(object sender,System.EventArgs e)
{
frmAbout about = new frmAbout();
about.Tag = this;
//here,we transfer current Form POINT to "about" window,
//then from "about" window
//we can access current Form any PUBLIC method and properties.
about.Show();
}
public frmFather()
{
btnAbout = new Button();
btnAbout.ForeColor = System.Drawing.Color.Green;
btnAbout.Text="&About Me";
btnAbout.Click += new System.EventHandler(this.btnAboutClick);
this.Width=400;
this.Controls.Add(btnAbout);
}
public static void Main()
{
frmFather father = new frmFather();
Application.Run(father);
}
}
}
利用回调实现双窗口通讯,尝试开启多个窗口一起测试
using System;
using System.Windows.Forms;
using System.Collections.Generic;
namespace infoFromWindows
{
public class frmAbout : Form
{
public frmAbout()
{
this.Text="I'm Child Window";
}
//public, called by father window through MouseMove Event
public void ChangeColor(object sender, MouseEventArgs e)
{
this.BackColor = System.Drawing.Color.FromArgb(e.X,e.Y,0);
}
}
// TypDefName: infoFromWindows.frmFather (02000003)
// Flags : [NotPublic] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100000)
// Extends : 01000001 [TypeRef] System.Windows.Forms.Form
class frmFather : System.Windows.Forms.Form
{
private Button btnAbout;
public frmFather()
{
btnAbout = new Button();
btnAbout.ForeColor = System.Drawing.Color.Green;
btnAbout.Text="&About Me";
btnAbout.Click += new System.EventHandler(this.btnAboutClick);
this.Height=255;
this.Width=255;
this.MaximizeBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Controls.Add(btnAbout);
}
private void GetChildPosCallFromFather(object sender,MouseEventArgs e)
{
this.Text=string.Format("({0},{1})",e.X,e.Y);
}
private void btnAboutClick(object sender,System.EventArgs e)
{
frmAbout about = new frmAbout();
//here,we transfer current Form POINT to "about" window,
//then from "about" window
//we can access current Form any PUBLIC method and properties.
about.Show();
about.MouseMove += new MouseEventHandler(GetChildPosCallFromFather);
this.MouseMove += new MouseEventHandler(about.ChangeColor);
}
public static void Main()
{
frmFather father = new frmFather();
Application.Run(father);
}
}
}
相关文章推荐
- C#中一个窗体传递另一个窗体的值和sting类型到double类型的转换两个问题
- [原创]C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。
- c#关于Delegate最简单的例子:两个窗体间的消息传递
- 关于 OnCloseQuery: 顺序、不能关机等(所有的windows的广播消息都是逐窗口传递的)——如果一个窗体的OnCloseQuery事件中如果写了代码那么WM_QUERYENDSESSION消息就传不过去了msg.result会返回0,关机事件也就停止了
- C# 利用接口在窗体间传递消息(很基础很实用)
- C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。
- c#winform编程 窗体之间传递参数问题总结(转)
- C#环境下使用Windows消息传递字符串数据的研究
- c#winform编程 窗体之间传递参数问题总结(转)
- 利用 WM_COPYDATA 消息 在 C# 和 传统C++应用之间传递数据时,64位平台的问题
- MFC 线程与窗体进行消息传递的时候出现的无效线程问题
- 黑马程序员 关于c# windows窗体关闭时线程未能完全退出问题(专题一)
- c#winform编程 窗体之间传递参数问题总结(转)
- 在C#中用户控件与窗体间的消息传递
- c#关于Delegate最简单的例子:两个窗体间的消息传递
- C#关于Windows窗体最大化所引起的闪烁问题的处理经验
- MFC 线程的消息传递问题一---线程和窗体之间
- c# winform窗体之间传递参数问题总结
- C#对象间的协作和信息交换(二)主窗体向从窗体传递消息
- 在C#中用户控件与窗体间的消息传递