一个程序关闭另外一个程序
来源:互联网 发布:java集合和数组的区别 编辑:程序博客网 时间:2024/05/07 06:49
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应用程序上运行后程序没有任何反应。
0 0
- 一个程序关闭另外一个程序
- 在一个程序里关闭另外一个进程
- vc 启动另外一个程序
- Symbian 一个程序调用另外一个程序片段代码
- 在一个Android程序中启动另外一个Android程序
- iOS 一个程序 调起另外一个程序
- 如何通过一个程序启动另外一个程序
- 向另外一个程序写入数据
- android 启动另外一个程序的Activity
- 带参数启动另外一个exe程序
- 一个关闭程序的例子
- SQL2005 在程序中调用另外一个预存程序
- Symbian 在程序中启动另外一个Symbian程序
- Symbian 在程序中启动另外一个Symbian程序
- android中一个程序中再次开启另外一个应用程序
- 启动另外一个程序的其中一个Activity
- android中一个程序中再次开启另外一个应用程序
- 在一个程序中启动另外一个Activity
- 【Android】自定义带进度条的WebView,修复不弹出软键盘的BUG
- C++11新特性应用--介绍几个新增的便利算法(关于最大值和最小值的算法)
- mysql配置
- 03-NFS
- 排序-Java系统库的排序算法
- 一个程序关闭另外一个程序
- Activity被回收之后怎么拿到之前的数据
- How to NSLog a Call stack when a program is running?
- 交换两个值
- hibernate 映射文件的 <generator>元素
- POJ2152 Fire
- android studio 断点调试
- Python3中重新加载一个新的更正的代码
- 设计三层异常如何处理?