最近偷的半日闲,看了一下API函数,挑了几个简单的做了一个小程序,都有注释,可以给有兴趣的朋友一晒
using System;
using System.Runtime.InteropServices;
using System.Collections;
![]()
namespace ConsoleApplication1
![]()
![]()
{
![]()
![]()
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
![]()
{
class Win32API
![]()
{
[DllImport("user32")]
public static extern long ExitWindowsEx(HowExitConst HowExitConst, long dwReserved);
![]()
public enum HowExitConst:uint
![]()
{
EWX_LOGOFF = 0, // 登出;
EWX_SHUTDOWN = 1,// 關機
EWX_REBOOT= 2,//重開機;
EWX_FORCE=4, // 強制關機
}
![]()
![]()
[DllImport("shell32")]
public static extern long ShellAbout(long uFlags, long dwReserved);
![]()
[DllImport("kernel32.dll", EntryPoint="Beep")]
public static extern int Beep(int dwFreq,int dwDuration);
![]()
[DllImport("kernel32.dll", EntryPoint="GetTickCount")]
public static extern int GetTickCount ();
![]()
[DllImport("user32.dll", EntryPoint="SwapMouseButton")]
public static extern int SwapMouseButton(int bSwap);
![]()
[DllImport("kernel32.dll", EntryPoint="LoadModule")]
public static extern int LoadModule(string lpModuleName,ref int lpParameterBlock);
![]()
[DllImport("shell32.dll", EntryPoint="ShellExecute")]
public static extern int ShellExecute (
int hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd
);
![]()
[DllImport("winmm.dll", EntryPoint="mciSendString")]
public static extern int mciSendString (
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
![]()
[DllImport("user32.dll", EntryPoint="WindowFromPoint")]
public static extern int WindowFromPoint (
int xPoint,
int yPoint
);
![]()
[DllImport("user32.dll", EntryPoint="EnableWindow")]
public static extern int EnableWindow (
int hwnd,
int fEnable
);
![]()
[DllImport("user32.dll", EntryPoint="FlashWindow")]
public static extern int FlashWindow (
int hwnd,
int bInvert
);
![]()
[DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
public static extern int GetDesktopWindow ();
![]()
[DllImport("user32.dll", EntryPoint="GetWindowText")]
public static extern int GetWindowText (
int hwnd,
out STRINGBUFFER lpString,
int cch
);
![]()
public struct STRINGBUFFER
![]()
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
public string szText;
}
![]()
[DllImport("user32.dll", EntryPoint="ShowWindow")]
public static extern int ShowWindow (
int hwnd,
nCmdShow nCmdShow
);
public enum nCmdShow:uint
![]()
{
SW_FORCEMINIMIZE=0x0,
SW_HIDE=0x1,
SW_MAXIMIZE=0x2,
SW_MINIMIZE=0x3,
SW_RESTORE=0x4,
SW_SHOW=0x5,
SW_SHOWDEFAULT=0x6,
SW_SHOWMAXIMIZED=0x7,
SW_SHOWMINIMIZED=0x8,
SW_SHOWMINNOACTIVE=0x9,
SW_SHOWNA=0xA,
SW_SHOWNOACTIVATE=0xB,
SW_SHOWNORMAL=0xC,
}
![]()
}
![]()
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
![]()
{
//
// TODO: 在此处添加代码以启动应用程序
//
// 关闭系统
// Console.WriteLine("bye,boy!");
// Console.WriteLine("Press Enter key to exit.");
Win32API.ExitWindowsEx(Win32API.HowExitConst.EWX_SHUTDOWN,1);
// Console.ReadLine();
// 发声
// System.Random ran=new Random();
// for (int i=0;i<30;i++)
// {
// Win32API.Beep(ran.Next(10000),100);
// }
![]()
// 得到系统运行时间
// int hour;
// int min;
// int second;
// hour=Win32API.GetTickCount()/3600000;
// min=(Win32API.GetTickCount()/60000-hour*60);
// second=(Win32API.GetTickCount()/1000-hour*3600-min*60);
// Console.WriteLine(hour+"hours"+min+"minutes"+second+"seconds");
![]()
// 鼠标左右健转换
// Console.WriteLine("输入一个数字:");
// int parant = int.Parse(Console.ReadLine());
// Win32API.SwapMouseButton(parant);
// 打开文件
// Win32API.ShellExecute(0,"open","http://www.gogle.com",null,null,1);
![]()
// 打开光驱&file
// Console.WriteLine("输入一个数字:");
// int cdparant = int.Parse(Console.ReadLine());
// if (cdparant==0)
// {
// Win32API.mciSendString("Set CDAudio Door Open Wait",null,0,0);
// }
// else
// {
// Win32API.mciSendString("Set CDAudio Door Closed Wait",null,0,0);
// }
![]()
// Win32API.mciSendString("open aaa.wav type waveaudio",null,0,0);
![]()
// 锁顶窗口
![]()
![]()
// Console.WriteLine(Win32API.WindowFromPoint(0,0));
// long result=Win32API.EnableWindow(Win32API.WindowFromPoint(0,0),0);
// Win32API.STRINGBUFFER winname;
// Win32API.GetWindowText(Win32API.WindowFromPoint(0,0),out winname,256);
// if (result==0)
// Console.WriteLine(winname.szText+" Lock Success!");
// else
// Console.WriteLine(winname.szText+"Lock Failed!");
![]()
// 闪烁窗口
// Console.WriteLine(Win32API.WindowFromPoint(0,0));
// Win32API.STRINGBUFFER winname;
// Win32API.GetWindowText(Win32API.WindowFromPoint(0,0),out winname,256);
// Console.WriteLine(winname.szText+"窗口 is been Flash,she say ,Please click me!!");
// Win32API.FlashWindow(Win32API.WindowFromPoint(0,0),1000);
![]()
// 最大化窗口
// Console.WriteLine(Win32API.WindowFromPoint(0,0));
// Win32API.STRINGBUFFER winname;
// Win32API.GetWindowText(Win32API.WindowFromPoint(0,0),out winname,256);
// Win32API.nCmdShow nCmdShow_parant;
// long max=Win32API.ShowWindow(Win32API.WindowFromPoint(0,0),SW_MAXIMIZE);
// if (max==0)
// Console.WriteLine(winname.szText+"windows MAX is Failed!");
// else
// Console.WriteLine(winname.szText+"windows MAX is Successful!");
![]()
// 缩定桌面
// long result=Win32API.EnableWindow(Win32API.GetDesktopWindow(),0);
// if (result==0)
// Console.WriteLine("Lock Success!");
// else
// Console.WriteLine("Lock Failed!");
}
}
}