📄 native.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
namespace HardwareDistributor.Utilities
{
public sealed class Native
{
/// <summary>
/// Performs a gradient fill much like the LinearGradientBrush does in .Net
/// </summary>
/// <param name="gr">the object to fill</param>
/// <param name="rectangle">rectangle to fill</param>
/// <param name="startColor">start color of the gradient</param>
/// <param name="endColor">end color of the gradient</param>
/// <param name="vertical">true for a vertical gradient, false for horizontal</param>
/// <returns>true for success</returns>
public static bool GradientFill(
Graphics gr,
Rectangle rectangle,
Color startColor, Color endColor, bool vertical)
{
TRIVERTEX[] triVertex = new TRIVERTEX[2];
triVertex[0] = new TRIVERTEX(rectangle.X, rectangle.Y, startColor);
triVertex[1] = new TRIVERTEX(rectangle.Right, rectangle.Bottom, endColor);
GRADIENT_RECT[] gradient = new GRADIENT_RECT[] { new GRADIENT_RECT(0, 1) };
// Get the hDC from the Graphics object.
IntPtr hdc = gr.GetHdc();
bool ret = GradientFill(hdc,
triVertex, (uint)triVertex.Length,
gradient, (uint)gradient.Length,
vertical ? GRADIENT_FILL_RECT_V : GRADIENT_FILL_RECT_H);
// Release the hDC from the Graphics object.
gr.ReleaseHdc(hdc);
return ret;
}
/// <summary>
/// Defines the triangles that make up the area to draw the gradient
/// </summary>
public struct TRIVERTEX
{
public int x;
public int y;
public ushort Red;
public ushort Green;
public ushort Blue;
public ushort Alpha;
public TRIVERTEX(int x, int y, Color color)
: this(x, y, color.R, color.G, color.B, color.A)
{
}
public TRIVERTEX(
int x, int y,
ushort red, ushort green, ushort blue,
ushort alpha)
{
this.x = x;
this.y = y;
this.Red = (ushort)(red << 8);
this.Green = (ushort)(green << 8);
this.Blue = (ushort)(blue << 8);
this.Alpha = (ushort)(alpha << 8);
}
}
public struct GRADIENT_RECT
{
public uint UpperLeft;
public uint LowerRight;
public GRADIENT_RECT(uint ul, uint lr)
{
this.UpperLeft = ul;
this.LowerRight = lr;
}
}
[System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError = true, EntryPoint = "GradientFill")]
public extern static bool GradientFill(
IntPtr hdc,
TRIVERTEX[] pVertex,
uint dwNumVertex,
GRADIENT_RECT[] pMesh,
uint dwNumMesh,
uint dwMode);
public const uint GRADIENT_FILL_RECT_H = 0;
public const uint GRADIENT_FILL_RECT_V = 1;
/// <summary>
/// Pass in a string seed to return a unique Device Id in the form of
/// a byte array
/// </summary>
/// <param name="appString"></param>
/// <returns></returns>
public static string GetDeviceID(string appString)
{
string deviceId = string.Empty;
// Call the GetDeviceUniqueID
byte[] appData = Encoding.Unicode.GetBytes(appString);
int appDataSize = appData.Length;
byte[] deviceOutput = new byte[20];
uint sizeOut = 20;
GetDeviceUniqueID(appData, appDataSize, 1, deviceOutput, out sizeOut);
//foreach (byte currentByte in deviceOutput)
//{
// deviceId = String.Format("{0}{1:X2} ", deviceId, currentByte);
//}
return Encoding.Unicode.GetString(deviceOutput, 0, deviceOutput.Length);
}
[DllImport("coredll.dll")]
private extern static int GetDeviceUniqueID([In, Out] byte[] appdata,
int cbApplictionData,
int dwDeviceIDVersion,
[In, Out] byte[] deviceIDOuput,
out uint pcbDeviceIDOutput);
/// <summary>
///
/// </summary>
public class MEMORYSTATUS
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
/// <summary>
///
/// </summary>
/// <param name="lpBuffer"></param>
[DllImport("CoreDll.dll")]
public static extern void GlobalMemoryStatus(MEMORYSTATUS lpBuffer);
/// <summary>
///
/// </summary>
/// <param name="lpdwStorePages"></param>
/// <param name="lpdwRamPages"></param>
/// <param name="lpdwPageSize"></param>
/// <returns></returns>
[DllImport("CoreDll.dll")]
public static extern int GetSystemMemoryDivision(ref uint lpdwStorePages,
ref uint lpdwRamPages,
ref uint lpdwPageSize);
/// <summary>
/// used for interprocess communication. sends message to the target window
/// </summary>
/// <param name="window"></param>
/// <param name="message"></param>
/// <param name="wparam"></param>
/// <param name="lparam"></param>
/// <returns></returns>
[DllImport("coredll.dll")]
public static extern int SendMessage(IntPtr window, int message, int wparam, int lparam);
/// <summary>
/// gets a pointer to the windows searched by name
/// </summary>
/// <param name="parent"></param>
/// <param name="childAfter"></param>
/// <param name="className"></param>
/// <param name="windowName"></param>
/// <returns></returns>
[DllImport("coredll.dll")]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("coredll.dll")]
public static extern bool SetUserDefaultUILanguage(Int16 languageId);
[DllImport("coredll.dll")]
public static extern Int16 GetUserDefaultUILanguage();
public enum LocaleFlags : uint { Installed = 1, Supported = 2 }
public delegate int LOCALE_ENUMPROC(String s);
[DllImport("coredll.dll")]
public static extern Int16 EnumSystemLocales(LOCALE_ENUMPROC l, LocaleFlags lf);
[DllImport("coredll.dll")]
public static extern bool SetUserDefaultLCID(Int16 languageId);
[DllImport("coredll.dll")]
public static extern Int16 GetUserDefaultLCID();
/// <summary>
/// the method needed to be called to actually reset the device.
/// </summary>
public static void ResetDevice()
{
ExitWindowsEx(2, 0);
}
[DllImport("aygshell.dll")]
public static extern bool ExitWindowsEx(uint flags, uint reason);
#region Screen shot
[DllImport("coredll.dll")]
public static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
#endregion
#region Webbrowser bug fix
[StructLayout(LayoutKind.Sequential)]
public struct MSG
{
IntPtr hwnd;
int message;
IntPtr wParam;
IntPtr lParam;
int time;
int x;
int y;
}
[DllImport("coredll")]
public extern static int DispatchMessage(ref MSG msg);
[DllImport("coredll")]
public extern static int TranslateMessage(ref MSG msg);
[DllImport("coredll")]
public extern static int GetMessage(out MSG msg, IntPtr handle, int filterMin, int filterMax);
[DllImport("coredll")]
public extern static bool PeekMessage(out MSG msg, IntPtr handle, int filterMin, int filterMax, int wRemoveMsg);
[DllImport("coredll")]
public extern static void PostQuitMessage(int nExitCode);
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -