RaUI/Source/ryControls/Controls/NativeMethods.cs
2020-11-28 15:03:57 +08:00

226 lines
7.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace ryControls.Pram
{
/// <summary>
///
/// </summary>
public class NativeMethods
{
internal const int WS_EX_CLIENTEDGE = 512 /*0x0200*/;
internal const int WS_EX_WINDOWEDGE = 0x0100;
internal const int WM_PAINT = 15; // 0x000f
internal const int WM_NCPAINT = 133; // 0x0085
internal const int RGN_COPY = 5;
internal const int RGN_AND = 1;
/// <summary>
///
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
/// <summary>
///
/// </summary>
public int left;
/// <summary>
///
/// </summary>
public int top;
/// <summary>
///
/// </summary>
public int right;
/// <summary>
///
/// </summary>
public int bottom;
/// <summary>
///
/// </summary>
/// <param name="rect"></param>
public RECT(Rectangle rect)
{
this.bottom = rect.Bottom;
this.left = rect.Left;
this.right = rect.Right;
this.top = rect.Top;
}
/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="top"></param>
/// <param name="right"></param>
/// <param name="bottom"></param>
public RECT(int left, int top, int right, int bottom)
{
this.bottom = bottom;
this.left = left;
this.right = right;
this.top = top;
}
/// <summary>
///
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static RECT FromXYWH(int x, int y, int width, int height)
{
return new RECT(x, y, x + width, y + height);
}
/// <summary>
///
/// </summary>
public int Width
{
get
{
return this.right - this.left;
}
}
/// <summary>
///
/// </summary>
public int Height
{
get
{
return this.bottom - this.top;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override /*Object*/ string ToString()
{
return String.Concat(
"Left = ",
this.left,
" Top ",
this.top,
" Right = ",
this.right,
" Bottom = ",
this.bottom);
}
/// <summary>
///
/// </summary>
/// <param name="rect"></param>
public static implicit operator Rectangle(RECT rect)
{
return Rectangle.FromLTRB(rect.left, rect.top, rect.right, rect.bottom);
}
}
[DllImport("user32.dll")]
internal static extern bool GetWindowRect(IntPtr hwnd, ref RECT lpRect);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
internal static extern IntPtr CreateRectRgn(int x1, int y1, int x2, int y2);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
internal static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
internal static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
internal static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int width, int height);
[DllImport("gdi32", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
internal static extern int CombineRgn(IntPtr hRgn, IntPtr hRgn1, IntPtr hRgn2, int nCombineMode);
/// <summary>
///
/// </summary>
/// <param name="hrgn"></param>
/// <param name="nXOffset"></param>
/// <param name="nYOffset"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern int OffsetRgn(IntPtr hrgn, int nXOffset, int nYOffset);
[DllImport("gdi32")]
internal static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
internal static extern int SelectClipRgn(IntPtr hDC, IntPtr hRgn);
/// <summary>
///
/// </summary>
/// <param name="hdc"></param>
/// <param name="nLeft"></param>
/// <param name="nTop"></param>
/// <param name="nRight"></param>
/// <param name="nBottom"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern int ExcludeClipRect(IntPtr hdc, int nLeft, int nTop, int nRight, int nBottom);
[DllImport("gdi32.dll")]
internal static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, Int32 dwRop);
[DllImport("gdi32", EntryPoint = "DeleteDC", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern bool DeleteDC(IntPtr hDC);
}
/// <summary>
///
/// </summary>
public class Pram
{
/// <summary>
/// 计算两个日期的时间间隔,返回的是日期值+
/// </summary>
/// <param name="date1">时间值,将显示在返回的值里</param>
/// <param name="referTime">参照时间</param>
/// <returns></returns>
public static string DateDiff(DateTime date1, DateTime referTime)
{
string dateDiff = "";
TimeSpan ts1 = new TimeSpan(date1.Ticks);
TimeSpan ts2 = new TimeSpan(referTime.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
//显示时间
if (ts.Days != 0)
{
int day = ts.Days;
if (day == 1) { dateDiff = "昨天 "+ date1.ToString("HH:mm:ss"); }
else if (day == 2) { dateDiff = "前天 " + date1.ToString("HH:mm:ss"); };
}
else
{
if (ts.Hours != 0)
{
dateDiff += ts.Hours.ToString() + "小时";
}
if (ts.Minutes != 0 || dateDiff == "")
{
dateDiff += ts.Minutes.ToString() + "分钟";
}
if (dateDiff != "") { dateDiff += "前"; }
}
if (dateDiff == "") { dateDiff = date1.ToString("yyyy-MM-dd HH:mm:ss"); }
return dateDiff;
}
}
}