Common functions for Windows APIs

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/xiashengwang/p/4026259.html

. NET Although the class library is very strong, but there are some times the function is limited, master common API functions,

Will give us an alternative way of thinking about solving problems.

1, SetForegroundWindow

Displays the window to the front, provided the window is not minimized.

[Diiimport ("User32.dll")]

public static extern bool SetForegroundWindow (INTPTRH Wnd);

2, Showwindowasync

The display window, such as when minimized, appears normal, which is asynchronous.

[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

interprocess communication, the receiving window must have a message loop. 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

);

Example:

Sender:

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; What numbers do you write here?

Winn32.sendmessage (Runninginstance.mainwindowhandle, winn32.wm_copydata, 0, ref copydata);

Receiving party

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

Find the Window handle

[DllImport ("User32.dll", EntryPoint = "FindWindow")]

private static extern int FindWindow (string lpclassname, string lpwindowname);

5,setlocaltime

Set the system time

[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

Get icon to execute file icons

[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)]//path has Chinese characters, to be Unicode

public static extern int Shgetfileinfo (

String Pszpath,

int dwFileAttributes,

Out Shfileinfo Psfi,

UINT Cbfileinfo,

Shgfi uflags);

Example:

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, N., out info, (UINT) cbfileinfo, flags);

Return Icon.fromhandle (Info.hicon);

}

7,getwindowthreadprocessid

Gets the process and thread ID of the handle, and returns the process ID returned by the thread Id,ref

[System.Runtime.InteropServices.DllImport ("User32.dll")]

private static extern int GetWindowThreadProcessId (int Hwnd, ref int outpressid);

Example: Killing an Excel process

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 ();

}

}

Common functions for Windows APIs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.