控制台——对WIN32 API的使用(user32.dll)

时间:2024-05-12 08:06:49

Win32 API概念:即为Microsoft 32位平台的应用程序编程接口(Application Programming Interface)。所有在Win32平台上运行的应用程序都可以调用这些函数。

Win32 API作用:应用程序可以充分挖掘Windows的32位操作系统的潜力。 Mircrosoft的所有32位平台都支持统一的API,包括函数、结构、消息、宏及接口。使用 Win32 API不但可以开发出在各种平台上都能成功运行的应用程序,而且也可以充分利用每个平台特有的功能和属性。

Win32 API分:1、窗口管理 2、窗口通用控制 3、Shell特性 4、图形设备接口 5、系统服务 6、国际特性 7、网络服务 
Win32 API使用:C#中使用DllImport关键字引入包含非托管方法的 DLL 的名称。例如user32.dll。
user32.dll概念:user32.dll是Windows用户界面相关应用程序接口,用于包括Windows处理,基本用户界面等特性,如创建窗口和发送消息。
通过案例演示如何使用user32.dll关闭“MyLove”这个程序,首先引入命名空间
 using System.Windows.Forms;
using System.Runtime.InteropServices;

通过DllImport引入user32.dll,这是核心代码

 [DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
private static void CloseWin()
{
const int WM_CLOSE = 0x10; //关闭
const uint WM_DESTROY = 0x02;
const uint WM_QUIT = 0x12;
const int BM_CLICK = 0xF5; //单击
IntPtr Window_Handle = (IntPtr)FindWindow(null, "MyLove");//查找所有的窗体,看看想查找的句柄是否存在,Microsoft Word 句柄
if (Window_Handle == IntPtr.Zero) //如果没有查找到相应的句柄
{
MessageBox.Show("没有找到窗体");
}
else //查找到相应的句柄
{
SendMessage(Window_Handle, WM_CLOSE, , ); //关闭窗体
}
}

控制台住函数入口处进行调用

 static void Main(string[] args)
{
Console.Title = "关闭其他窗体";
CloseWin();
}