请教一个关于窗体自定义消息(传递消息)的问题(C#语言),请赐教
我先描述一下我的程序,这是一个浏览gif格式图片的查看器,打开gif文件时默认用该查看器查看,当该查看器已经打开的时候,再打开其他的gif格式文件,仍然以原查看器查看,并不是再次打开一个查看器。
起初我是用
public static extern int SendMessage(IntPtr hwnd, uint wMsg, IntPtr wParam, IntPtr lParam);
来传递消息,
我在主窗体中定义了一个方法
LoadImage(string fileName),用于加载目标gif文件,
在program类中定义了一个方法
private static void HandleRunningInstance(Process instance, string[] args)
{
if (args.Length > 0)
{
try
{
IntPtr lp = Marshal.StringToCoTaskMemAnsi(args[0] + "\0\0");
SendMessage(instance.MainWindowHandle, WM_USER + 1, IntPtr.Zero, lp);
//string fileName = Marshal.PtrToStringAnsi(lp);
//Log(lp.ToString() + "--" + fileName);
}
catch (Exception ex)
{
Log(ex.Message);
}
}
}
第一个参数是获取的已经打开的查看器的进程,第二个参数是入口Main(string[] args)方法的参数,args里包括目标Gif文件的文件名,
在主窗体中重写了方法:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_USER + 1)
{
try
{
string fileName = Marshal.PtrToStringAnsi(m.LParam);
Program.Log("WndProc获取到参数:" + fileName.ToString());
LoadImage(fileName.ToString());
}
catch (Exception ex)
{
Program.Log("error:" + ex.Message);
}
}
}
可是我用这种方法获取到的fileName是空值。。。
然后传递消息的方法改成了
public static extern int SendMessage(IntPtr HWnd, uint Msg, int WParam, ref Msg lParam);
对应代码:
private static void HandleRunningInstance(Process instance, string[] args)
{
if (args.Length > 0)
{
try
{
Msg msg = new Msg();
msg.FileName = args[0];
SendMessage(instance.MainWindowHandle, WM_USER + 1, 0, ref msg);
}
catch (Exception ex)
{
Log(ex.Message);
}
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_USER + 1)
{
try
{
Msg msg1 = new Msg();
Program.Log(msg1.GetType().ToString());
Msg msg = (Msg)m.GetLParam(msg1.GetType());
string fileName = msg.FileName;
Program.Log("WndProc获取到参数:" + fileName.ToString());
LoadImage(fileName.ToString());
}
catch (Exception ex)
{
Program.Log("error:" + ex.Message);
}
}
}
可是这样又没有取得msg,
我调试得到了LParam参数,可是参数转换成字符串不行,转换成结构也不行,这是怎么回事,请高手指教。
起初我是用
public static extern int SendMessage(IntPtr hwnd, uint wMsg, IntPtr wParam, IntPtr lParam);
来传递消息,
我在主窗体中定义了一个方法
LoadImage(string fileName),用于加载目标gif文件,
在program类中定义了一个方法
private static void HandleRunningInstance(Process instance, string[] args)
{
if (args.Length > 0)
{
try
{
IntPtr lp = Marshal.StringToCoTaskMemAnsi(args[0] + "\0\0");
SendMessage(instance.MainWindowHandle, WM_USER + 1, IntPtr.Zero, lp);
//string fileName = Marshal.PtrToStringAnsi(lp);
//Log(lp.ToString() + "--" + fileName);
}
catch (Exception ex)
{
Log(ex.Message);
}
}
}
第一个参数是获取的已经打开的查看器的进程,第二个参数是入口Main(string[] args)方法的参数,args里包括目标Gif文件的文件名,
在主窗体中重写了方法:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_USER + 1)
{
try
{
string fileName = Marshal.PtrToStringAnsi(m.LParam);
Program.Log("WndProc获取到参数:" + fileName.ToString());
LoadImage(fileName.ToString());
}
catch (Exception ex)
{
Program.Log("error:" + ex.Message);
}
}
}
可是我用这种方法获取到的fileName是空值。。。
然后传递消息的方法改成了
public static extern int SendMessage(IntPtr HWnd, uint Msg, int WParam, ref Msg lParam);
对应代码:
private static void HandleRunningInstance(Process instance, string[] args)
{
if (args.Length > 0)
{
try
{
Msg msg = new Msg();
msg.FileName = args[0];
SendMessage(instance.MainWindowHandle, WM_USER + 1, 0, ref msg);
}
catch (Exception ex)
{
Log(ex.Message);
}
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_USER + 1)
{
try
{
Msg msg1 = new Msg();
Program.Log(msg1.GetType().ToString());
Msg msg = (Msg)m.GetLParam(msg1.GetType());
string fileName = msg.FileName;
Program.Log("WndProc获取到参数:" + fileName.ToString());
LoadImage(fileName.ToString());
}
catch (Exception ex)
{
Program.Log("error:" + ex.Message);
}
}
}
可是这样又没有取得msg,
我调试得到了LParam参数,可是参数转换成字符串不行,转换成结构也不行,这是怎么回事,请高手指教。
...全文
相关推荐
C# 使用Usercontrol与主窗体进行消息传递
总之,熟练掌握委托和事件在UserControl与主窗体间的消息传递,将极大地提升你在C# Windows Forms开发中的效率和能力。
C#实现子窗体与父窗体消息通信指南
函数是Windows API中用于发送消息到消息队列的函数,它不同于直接发送消息的函数,将消息放入队列中后便立即返回,不会等待消息处理完成,因此对性能的负担较小,适用于发送不需要立即处理的消息。该函数在C#中通过DllImport属性导入,使用时需要指定消息的接收者、消息类型和相关参数。在C#中调用Windows API时,需要注意数据类型的映射,以确保消息能被正确发送和处理。在Windows操作系统中,句柄(Handle)是一个值,用于标识资源。
C# 使用委托方式Usercontrol与主窗体进行消息传递
在C#编程中,消息传递是UI组件之间通信的关键机制,特别是当我们在用户控件(UserControl)和主窗体(Form)之间需要交换数据时。
C# 两个窗体之间实时传递数据的实例
在C#编程中,开发GUI应用程序时经常需要在不同的窗体之间传递数据。在这个实例中,我们将探讨如何在两个窗体之间实现实时的数据传递,主要采用委托和事件的技术。
C#_winform_多个窗体之间相互传递数据操作
例如,在子窗体中定义一个`public string SelectedData { get; set; }`属性,然后在父窗体中设置这个属性。 - **事件驱动**:创建自定义事件并在窗体之间触发。


