Windows API常用函数
转自:http://www.cnblogs.com/xiashengwang/p/4026259.html
.NET中虽然类库很强,但还是有些时候功能有限,掌握常用的api函数,
会给我们解决问题提供另一种思路。
1、SetForegroundWindow
将窗口显示到最前面,前提是窗口没有最小化。
[DIIImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtrh Wnd);
2、ShowWindowAsync
显示窗口,如最小化后显示正常,这是异步的。
[DllImport("User32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
public enum ShowState : int
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11,
SW_MAX = 11
}
3,SendMessage
进程间通信,接受窗口要有消息循环才行。WM_COPYDATA
public const int WM_COPYDATA = 0x004A;
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
ref COPYDATASTRUCT lParam // second message parameter
);
例子:
发送方:
byte[] sarr = System.Text.Encoding.Default.GetBytes(args[0]);
Winn32.COPYDATASTRUCT copyData = new Winn32.COPYDATASTRUCT();
copyData.cbData = sarr.Length + 1;
copyData.lpData = args[0];
copyData.dwData = (IntPtr)100; //这里随便写什么数字
Winn32.SendMessage(runningInstance.MainWindowHandle, Winn32.WM_COPYDATA, 0, ref copyData);
接收方
protected override void DefWndProc(ref Message m)
{
if (m.Msg == Winn32.WM_COPYDATA)
{
Winn32.COPYDATASTRUCT copyData = new Winn32.COPYDATASTRUCT();
Type type = copyData.GetType();
copyData = (Winn32.COPYDATASTRUCT)m.GetLParam(type);
this.textBox1.Text = copyData.lpData;
}
base.DefWndProc(ref m);
}
4,FindWindow
找到窗口句柄
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
5,SetLocalTime
设定系统时间
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
public void FromDateTime(DateTime dateTime)
{
wYear = (ushort)dateTime.Year;
wMonth = (ushort)dateTime.Month;
wDayOfWeek = (ushort)dateTime.DayOfWeek;
wDay = (ushort)dateTime.Day;
wHour = (ushort)dateTime.Hour;
wMinute = (ushort)dateTime.Minute;
wSecond = (ushort)dateTime.Second;
wMilliseconds = (ushort)dateTime.Millisecond;
}
public DateTime ToDateTime()
{
return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond);
}
}
[DllImport("kernel32.dll")]
public static extern bool SetLocalTime(ref SYSTEMTIME Time);
6,SHGetFileInfo
获取执行文件的图标icon
[Flags]
enum SHGFI : int
{
/// <summary>get icon</summary>
Icon = 0x000000100,
/// <summary>get display name</summary>
DisplayName = 0x000000200,
/// <summary>get type name</summary>
TypeName = 0x000000400,
/// <summary>get attributes</summary>
Attributes = 0x000000800,
/// <summary>get icon location</summary>
IconLocation = 0x000001000,
/// <summary>return exe type</summary>
ExeType = 0x000002000,
/// <summary>get system icon index</summary>
SysIconIndex = 0x000004000,
/// <summary>put a link overlay on icon</summary>
LinkOverlay = 0x000008000,
/// <summary>show icon in selected state</summary>
Selected = 0x000010000,
/// <summary>get only specified attributes</summary>
Attr_Specified = 0x000020000,
/// <summary>get large icon</summary>
LargeIcon = 0x000000000,
/// <summary>get small icon</summary>
SmallIcon = 0x000000001,
/// <summary>get open icon</summary>
OpenIcon = 0x000000002,
/// <summary>get shell size icon</summary>
ShellIconSize = 0x000000004,
/// <summary>pszPath is a pidl</summary>
PIDL = 0x000000008,
/// <summary>use passed dwFileAttribute</summary>
UseFileAttributes = 0x000000010,
/// <summary>apply the appropriate overlays</summary>
AddOverlays = 0x000000020,
/// <summary>Get the index of the overlay in the upper 8 bits of the iIcon</summary>
OverlayIndex = 0x000000040,
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero;
iIcon = 0;
dwAttributes = 0;
szDisplayName = "";
szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]//路径有中文字符,要用unicode
public static extern int SHGetFileInfo(
string pszPath,
int dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags);
例子:
private static Icon GetIcon(string strPath, bool bSmall)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon | SHGFI.LargeIcon;
Win32API.SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
7,GetWindowThreadProcessId
得到句柄的进程和线程ID,返回的是线程ID,ref返回的是进程ID
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern int GetWindowThreadProcessId(int Hwnd, ref int OutPressId);
例子:杀死excel进程
int processID = 0;
int threadID;
threadID = GetWindowThreadProcessId(excelApp.Hwnd, ref processID);
if (processID > 0)
{
System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(processID);
if (process != null)
{
process.Kill();
}
}
Windows API常用函数的更多相关文章
- Windows API 常用函数---转载
		
Windows API 常用函数 2014-10-15 14:21 xiashengwang 阅读(2105) 评论(0) 编辑 收藏 .Net中虽然类库很强的,但还是有些时候功能有限,掌握 ...
 - Windows API 常用函数
		
.Net中虽然类库很强的,但还是有些时候功能有限,掌握常用的api函数,会给我们解决问题提供另一种思路,下面给出自己常用到的Api函数,以备查询. 知道api函数,但却不知道c#或VB.net该如何声 ...
 - 使用IDA PRO+OllyDbg+PEview 追踪windows API 动态链接库函数的调用过程
		
使用IDA PRO+OllyDbg+PEview 追踪windows API 动态链接库函数的调用过程 http://blog.csdn.net/liujiayu2/article/details/5 ...
 - Appium——api常用函数
		
appium常用函数介绍: 获取页面信息: 1. def get_current_activity(cls, driver): ''' 获取当前页面的activity :param drive ...
 - VC API常用函数简单例子大全(1-89)
		
第一个:FindWindow根据窗口类名或窗口标题名来获得窗口的句柄,该函数返回窗口的句柄 函数的定义:HWND WINAPI FindWindow(LPCSTR lpClassName ,LPCST ...
 - winform窗体之间通过 windows API SendMessage函数传值
		
-----------------------------------------------------------‘接收窗体’代码.cs------------------------------ ...
 - windows 编程—— 常用函数 与 操作
		
目录: MessageBox() 和 PlaySound() 获得窗口 或屏幕大小 获得字体大小 输出文字 屏蔽和显示控制台窗口 1. MessageBox() 和 PlaySound() Messa ...
 - C++使用Windows API CreateMutex函数多线程编程
		
C++中也可以使用Windows 系统中对应的API函数进行多线程编程.使用CreateThread函数创建线程,并且可以通过CreateMutex创建一个互斥量实现线程间数据的同步: #includ ...
 - windows API普通函数跟回调函数有何区别
		
通俗点讲:1.普通函数(假设我们都是函数)你卖电脑,我买电脑,我给你钱(调用你)后,你给我电脑(得到返回值).这种情况下,我给钱后就不能走开,必须等你把电脑给我,否则你交货的时候可能找不到人.2.回调 ...
 
随机推荐
- 深入Spring Boot:ClassLoader的继承关系和影响
			
前言 对spring boot本身启动原理的分析, Spring boot里的ClassLoader继承关系 可以运行下面提供的demo,分别在不同的场景下运行,可以知道不同场景下的Spring bo ...
 - Delivering Goods UVALive - 7986(最短路+最小路径覆盖)
			
Delivering Goods UVALive - 7986(最短路+最小路径覆盖) 题意: 给一张n个点m条边的有向带权图,给出C个关键点,问沿着最短路径走,从0最少需要出发多少次才能能覆盖这些关 ...
 - BZOJ1086 [SCOI2005]王室联邦 【dfs + 贪心】
			
题目 "余"人国的国王想重新编制他的国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成 员来管理.他的国家有n个城市,编号为1..n.一些城市之间有道路相连,任意两 ...
 - jQuery遍历文档(重要)
			
什么是遍历? jQuery 遍历,意为"移动",用于根据其相对于其他元素的关系来"查找"(或选取)HTML 元素.以某项选择开始,并沿着这个选择移动,直到抵达您 ...
 - 三、第一个cocos2d程序的代码分析
			
http://blog.csdn.net/q199109106q/article/details/8591706 在第一讲中已经新建了第一个cocos2d程序,运行效果如下: 在这讲中我们来分析下里面 ...
 - (十)stm32 GPIO口复用,重映射 RCC_APB2Periph_AFIO
			
什么时候需要用到RCC_APB2Periph_AFIO--复用IO时钟的使用 需要用到外设的重映射功能时才需要使能AFIO的时钟 外部中断(EXTI)中与AFIO有关的寄存器是AFIO-EXTICR1 ...
 - mysql 事务、游标
			
mysql 事务 在MySQL中只有使用了Innodb数据库引擎的数据库或表才支持事务,所以很多情况下我们都使用innodb引擎. 事务处理可以用来维护数据库的完整性,保证成批的SQL语句要么全部执行 ...
 - vim注释颜色更改
			
通过putty或者securecrt连到linux服务器,使用vi编辑*.php 或*.c文件时, 蓝色的注释很淡,看不清,看起来很费眼 效果如下 我本身有习惯于默认配色,不想修改配色方案,只想把看不 ...
 - Visual Studio跨平台开发(4):Xamarin Android控制项介绍
			
前言 不同于iOS, Xamarin 在Visual Studio中针对Android, 可以直接设计使用者界面. 在本篇教学文章中, 笔者会针对Android的专案目录结构以及基本控制项进行介绍, ...
 - DB2—alter追加/删除/重置column操作
			
DB2—alter追加/删除/重置column操作 1.添加字段 alter table 表名称 add 字段名称 类型 Demo: 1 alter table table_name a ...