using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
using System.Runtime.InteropServices ;
namespace test
{
class Program
{
[DllImport ("User32.dll") ]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern Boolean SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public Alert hehe;
static IntPtr he;
int scantime;
// void loadconfig(string data);
private const int WM_ACTIVATEAPP = 0x001C;
private bool appActive = true;
const int WM_CLOSE = 0x10;
const uint WM_DESTROY = 0x02;
const uint WM_QUIT = 0x12;
static void Main(string[] args)
{
systest conf;
//conf=new systest() ;
// conf.loadconfig("config.xml");
// Console.WriteLine(conf.pptdir+conf.pptdir );
he = FindWindow(null, "yuan");
Console.WriteLine(he.ToString());
Console.WriteLine(he.ToInt64());
// PostMessage(he, WM_QUIT, IntPtr.Zero, IntPtr.Zero);
// SetForegroundWindow(he);
SendMessage(he, WM_QUIT, IntPtr.Zero, IntPtr.Zero);
// SendMessage(he, WM_QUIT, 0, 0);
Thread.Sleep(20000);
}
public void backthread()
{
hehe = new Alert();
Form f = new Form();
Application.Run(f);
f.Show();
hehe.ShowDialog();
}
}
class systest
{
public XmlDocument doc;
public string pptexe, pptdir, scantime;
public void loadconfig(string data)
{
doc = new XmlDocument();
doc.Load(data);
XmlNode pptdirnode, pptexenode, timenode;
pptexenode = doc.SelectSingleNode("/root/PPTExe");
pptdirnode = doc.SelectSingleNode("/root/PPTDir");
timenode = doc.SelectSingleNode("/root/ScanTime");
pptexe = pptexenode.InnerText.Trim();
pptdir = pptdirnode.InnerText.Trim();
//scantime = Convert.ToInt32(timenode.InnerText.Trim());
// MessageBox.Show(pptdir);
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
using System.Runtime.InteropServices ;
namespace test
{
class Program
{
[DllImport ("User32.dll") ]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern Boolean SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public Alert hehe;
static IntPtr he;
int scantime;
// void loadconfig(string data);
private const int WM_ACTIVATEAPP = 0x001C;
private bool appActive = true;
const int WM_CLOSE = 0x10;
const uint WM_DESTROY = 0x02;
const uint WM_QUIT = 0x12;
static void Main(string[] args)
{
systest conf;
//conf=new systest() ;
// conf.loadconfig("config.xml");
// Console.WriteLine(conf.pptdir+conf.pptdir );
he = FindWindow(null, "yuan");
Console.WriteLine(he.ToString());
Console.WriteLine(he.ToInt64());
// PostMessage(he, WM_QUIT, IntPtr.Zero, IntPtr.Zero);
// SetForegroundWindow(he);
SendMessage(he, WM_QUIT, IntPtr.Zero, IntPtr.Zero);
// SendMessage(he, WM_QUIT, 0, 0);
Thread.Sleep(20000);
}
public void backthread()
{
hehe = new Alert();
Form f = new Form();
Application.Run(f);
f.Show();
hehe.ShowDialog();
}
}
class systest
{
public XmlDocument doc;
public string pptexe, pptdir, scantime;
public void loadconfig(string data)
{
doc = new XmlDocument();
doc.Load(data);
XmlNode pptdirnode, pptexenode, timenode;
pptexenode = doc.SelectSingleNode("/root/PPTExe");
pptdirnode = doc.SelectSingleNode("/root/PPTDir");
timenode = doc.SelectSingleNode("/root/ScanTime");
pptexe = pptexenode.InnerText.Trim();
pptdir = pptdirnode.InnerText.Trim();
//scantime = Convert.ToInt32(timenode.InnerText.Trim());
// MessageBox.Show(pptdir);
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
}
}
}
在一个GUI程序运行环境下,我们通过得到窗体的句柄,然后便可以向该句柄发送消息,WndProc(Window Procedure)函数会处理相应的事件。其中WM_CLOSE相当于用户点击关闭按钮,使用PostMessage将WM_CLOSE发送至主窗体等价于.NET中Process类的CloseMainWindow方法,当接收到WM_CLOSE消息时,应用程序是可以选择是否真正结束程序的,如果继续结束程序而不取消。接着WM_DESTROY消息会发送,这个消息代表着窗体开始真正关闭,此时可以进行一些资源的清理。最后当前线程接收到WM_QUIT消息,线程的消息循环会被终止。
因此向窗体发送这3个消息,只有WM_CLOSE会引发Closing事件,属于正常窗体退出逻辑,其他两个中消息会直接强行关闭窗体。
注意WM_QUIT消息只能用PostMessage将其送至消息队列尾部,使用SendMessage立即发送在WPF应用程序上运行后程序没有任何反应。