Bug
Through thorns to stars
Администратор
Bug
Through thorns to stars
Администратор
- Сообщения
- 1,464
- Реакции
- 2,885
C#:
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using Microsoft.VisualBasic.CompilerServices;
namespace GlassEffect
{
public class GlassEffect
{
public GlassEffect()
{
this.WindowListener = new APIs.HookWindow();
this.IsGlassEnabled = GlassEnabled;
this.mUseHandCursorOnTitle = true;
this.Last = Point.Empty;
}
public event GlassEffectEnabledEventHandler GlassEffectEnabled
{
[DebuggerNonUserCode]
[MethodImpl(MethodImplOptions.Synchronized)]
add
{
this.GlassEffectEnabledEvent = (GlassEffectEnabledEventHandler)Delegate.Combine(this.GlassEffectEnabledEvent, value);
}
[DebuggerNonUserCode]
[MethodImpl(MethodImplOptions.Synchronized)]
remove
{
this.GlassEffectEnabledEvent = (GlassEffectEnabledEventHandler)Delegate.Remove(this.GlassEffectEnabledEvent, value);
}
}
public event GlassEffectDisabledEventHandler GlassEffectDisabled
{
[DebuggerNonUserCode]
[MethodImpl(MethodImplOptions.Synchronized)]
add
{
this.GlassEffectDisabledEvent = (GlassEffectDisabledEventHandler)Delegate.Combine(this.GlassEffectDisabledEvent, value);
}
[DebuggerNonUserCode]
[MethodImpl(MethodImplOptions.Synchronized)]
remove
{
this.GlassEffectDisabledEvent = (GlassEffectDisabledEventHandler)Delegate.Remove(this.GlassEffectDisabledEvent, value);
}
}
private APIs.HookWindow WindowListener
{
[DebuggerNonUserCode]
get
{
return this._WindowListener;
}
[DebuggerNonUserCode]
[MethodImpl(MethodImplOptions.Synchronized)]
set
{
APIs.HookWindow.MessageArrivedEventHandler obj = new APIs.HookWindow.MessageArrivedEventHandler(this.WindowListener_MessageArrived);
if (this._WindowListener != null)
{
this._WindowListener.MessageArrived -= obj;
}
this._WindowListener = value;
if (this._WindowListener != null)
{
this._WindowListener.MessageArrived += obj;
}
}
}
private Form mParentForm
{
[DebuggerNonUserCode]
get
{
return this._mParentForm;
}
[DebuggerNonUserCode]
[MethodImpl(MethodImplOptions.Synchronized)]
set
{
PaintEventHandler value2 = new PaintEventHandler(this.Parent_Paint);
MouseEventHandler value3 = new MouseEventHandler(this.Parent_MouseDown);
EventHandler value4 = new EventHandler(this.ParentForm_Resize);
MouseEventHandler value5 = new MouseEventHandler(this.Parent_MouseMove);
if (this._mParentForm != null)
{
this._mParentForm.Paint -= value2;
this._mParentForm.MouseDown -= value3;
this._mParentForm.Resize -= value4;
this._mParentForm.MouseMove -= value5;
}
this._mParentForm = value;
if (this._mParentForm != null)
{
this._mParentForm.Paint += value2;
this._mParentForm.MouseDown += value3;
this._mParentForm.Resize += value4;
this._mParentForm.MouseMove += value5;
}
}
}
public Form ParentForm
{
get
{
return this.mParentForm;
}
set
{
this.mParentForm = value;
}
}
public PictureBox HeaderImage
{
get
{
return this.mHeaderImage;
}
set
{
this.mHeaderImage = value;
}
}
public int TopBarSize
{
get
{
return this.mTopBarSize;
}
set
{
this.mTopBarSize = value;
}
}
public int BottomBarSize
{
get
{
return this.mBottomBarSize;
}
set
{
this.mBottomBarSize = value;
}
}
public int LeftBarSize
{
get
{
return this.mLeftBarSize;
}
set
{
this.mLeftBarSize = value;
}
}
public int RightBarSize
{
get
{
return this.mRightBarSize;
}
set
{
this.mRightBarSize = value;
}
}
public Label HeaderLabel
{
get
{
return this.mHeaderLabel;
}
set
{
this.mHeaderLabel = value;
}
}
public bool UseHandCursorOnTitle
{
get
{
return this.mUseHandCursorOnTitle;
}
set
{
this.mUseHandCursorOnTitle = value;
}
}
public void ShowEffect(Form Parent, Label HeaderLabel, PictureBox HeaderImage)
{
this.ParentForm = Parent;
this.HeaderLabel = HeaderLabel;
this.HeaderImage = HeaderImage;
SetGlassEffect(this.ParentForm, this.mTopBarSize, this.mRightBarSize, this.mBottomBarSize, this.mLeftBarSize);
}
public void ShowEffect(Form Parent, Label HeaderLabel)
{
this.ParentForm = Parent;
this.HeaderLabel = HeaderLabel;
SetGlassEffect(this.ParentForm, this.mTopBarSize, this.mRightBarSize, this.mBottomBarSize, this.mLeftBarSize);
}
public void ShowEffect(Form Parent, PictureBox HeaderImage)
{
this.ParentForm = Parent;
this.HeaderImage = HeaderImage;
SetGlassEffect(this.ParentForm, this.mTopBarSize, this.mRightBarSize, this.mBottomBarSize, this.mLeftBarSize);
}
public void ShowEffect(Form Parent)
{
this.ParentForm = Parent;
SetGlassEffect(this.ParentForm, this.mTopBarSize, this.mRightBarSize, this.mBottomBarSize, this.mLeftBarSize);
}
public bool SetGlassEffect(int fromTop = 0, int fromRight = 0, int fromBottom = 0, int fromLeft = 0)
{
bool flag = false;
SetGlassEffect(this.ParentForm, fromTop, fromRight, fromBottom, fromLeft);
this.ParentForm.Invalidate();
return flag;
}
public static bool GlassEnabled
{
get
{
bool flag = Environment.OSVersion.Version.Major >= 6;
if (flag)
{
bool result = true;
APIs.DwmIsCompositionEnabled(ref result);
return result;
}
return false;
}
}
public static bool SetGlassEffect(Form Frm, int fromTop = 0, int fromRight = 0, int fromBottom = 0, int fromLeft = 0)
{
bool flag = false;
if (GlassEnabled && (Frm != null))
{
APIs.MARGINS margins = new APIs.MARGINS
{
Top = fromTop,
Right = fromRight,
Left = fromLeft,
Bottom = fromBottom
};
APIs.DwmExtendFrameIntoClientArea(Frm.Handle, ref margins);
Frm.Invalidate();
}
return flag;
}
public static void DrawTextGlow(Graphics Graphics, string text, Font fnt, Rectangle bounds, Color Clr, TextFormatFlags flags)
{
IntPtr hObject = IntPtr.Zero;
IntPtr hObject2 = IntPtr.Zero;
IntPtr hdc = Graphics.GetHdc();
IntPtr intPtr = APIs.CreateCompatibleDC(hdc);
APIs.BITMAPINFO bITMAPINFO = default(APIs.BITMAPINFO);
checked
{
APIs.RECT rECT = new APIs.RECT(0, 0, bounds.Right - bounds.Left + 30, bounds.Bottom - bounds.Top + 30);
APIs.RECT rECT2 = new APIs.RECT(bounds.Left - 15, bounds.Top - 15, bounds.Right + 15, bounds.Bottom + 15);
IntPtr hObject3 = fnt.ToHfont();
try
{
VisualStyleRenderer visualStyleRenderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active);
bITMAPINFO.bmiHeader.biSize = Marshal.SizeOf(bITMAPINFO.bmiHeader);
bITMAPINFO.bmiHeader.biWidth = bounds.Width + 30;
bITMAPINFO.bmiHeader.biHeight = 0 - bounds.Height - 30;
bITMAPINFO.bmiHeader.biPlanes = 1;
bITMAPINFO.bmiHeader.biBitCount = 32;
IntPtr hObject4 = APIs.CreateDIBSection(hdc, ref bITMAPINFO, 0u, 0, IntPtr.Zero, 0u);
hObject = APIs.SelectObject(intPtr, hObject4);
hObject2 = APIs.SelectObject(intPtr, hObject3);
APIs.S_DTTOPTS s_DTTOPTS = default(APIs.S_DTTOPTS);
s_DTTOPTS.dwSize = Marshal.SizeOf(s_DTTOPTS);
s_DTTOPTS.dwFlags = 10241;
s_DTTOPTS.crText = ColorTranslator.ToWin32(Clr);
s_DTTOPTS.iGlowSize = 18;
APIs.DrawThemeTextEx(visualStyleRenderer.Handle, intPtr, 0, 0, text, -1, (int)flags, ref rECT, ref s_DTTOPTS);
APIs.BitBlt(hdc, rECT2.Left, rECT2.Top, rECT2.Right - rECT2.Left, rECT2.Bottom - rECT2.Top, intPtr, 0, 0, 13369376);
APIs.SelectObject(intPtr, hObject2);
APIs.SelectObject(intPtr, hObject);
APIs.DeleteDC(intPtr);
APIs.DeleteObject(hObject3);
APIs.DeleteObject(hObject4);
Graphics.ReleaseHdc(hdc);
}
catch (Exception expr_1E0)
{
ProjectData.SetProjectError(expr_1E0);
ProjectData.ClearProjectError();
}
}
}
private void Parent_Paint(object sender, PaintEventArgs e)
{
checked
{
try
{
if (GlassEnabled)
{
if (this.mTopBarSize > 0)
{
Graphics arg_41_0 = e.Graphics;
Brush arg_41_1 = Brushes.Black;
Rectangle rect = new Rectangle(0, 0, this.ParentForm.ClientSize.Width, this.mTopBarSize);
arg_41_0.FillRectangle(arg_41_1, rect);
}
if (this.mBottomBarSize > 0)
{
Graphics arg_96_0 = e.Graphics;
Brush arg_96_1 = Brushes.Black;
Rectangle rect = new Rectangle(0, this.ParentForm.ClientSize.Height - this.mBottomBarSize, this.ParentForm.ClientSize.Width, this.mBottomBarSize);
arg_96_0.FillRectangle(arg_96_1, rect);
}
if (this.mRightBarSize > 0)
{
Graphics arg_EB_0 = e.Graphics;
Brush arg_EB_1 = Brushes.Black;
Rectangle rect = new Rectangle(this.ParentForm.ClientSize.Width - this.mRightBarSize, 0, this.mRightBarSize, this.ParentForm.ClientSize.Height);
arg_EB_0.FillRectangle(arg_EB_1, rect);
}
if (this.mLeftBarSize > 0)
{
Graphics arg_127_0 = e.Graphics;
Brush arg_127_1 = Brushes.Black;
Rectangle rect = new Rectangle(0, 0, this.mLeftBarSize, this.ParentForm.ClientSize.Height);
arg_127_0.FillRectangle(arg_127_1, rect);
}
if (this.HeaderLabel != null && this.HeaderLabel.Text.Length > 0)
{
this.HeaderLabel.Visible = false;
DrawTextGlow(e.Graphics, this.HeaderLabel.Text, this.HeaderLabel.Font, this.HeaderLabel.Bounds, this.HeaderLabel.ForeColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter);
}
if (this.HeaderImage != null && this.HeaderImage.Image != null)
{
this.HeaderImage.Visible = false;
e.Graphics.DrawImage(this.mHeaderImage.Image, this.mHeaderImage.Bounds);
}
}
}
catch (Exception expr_1D3)
{
ProjectData.SetProjectError(expr_1D3);
ProjectData.ClearProjectError();
}
}
}
private void Parent_MouseDown(object sender, MouseEventArgs e)
{
if (e.Location.Y <= this.mTopBarSize)
{
this.Last = e.Location;
}
else
{
this.Last = Point.Empty;
}
}
private void Parent_MouseMove(object sender, MouseEventArgs e)
{
if (!this.Last.Equals(Point.Empty) && e.Button == MouseButtons.Left)
{
Form arg_84_0 = this.ParentForm;
Point location = checked(new Point(this.ParentForm.Left + e.Location.X - this.Last.X, this.ParentForm.Top + e.Location.Y - this.Last.Y));
arg_84_0.Location = location;
}
if (this.mUseHandCursorOnTitle)
{
Point location = e.Location;
if (location.Y < this.mTopBarSize)
{
if (!this.ParentForm.Cursor.Equals(Cursors.Hand))
{
this.ParentForm.Cursor = Cursors.Hand;
}
}
else if (!this.ParentForm.Cursor.Equals(Cursors.Default))
{
this.ParentForm.Cursor = Cursors.Default;
}
}
}
private void ParentForm_Resize(object sender, EventArgs e)
{
this.ParentForm.Invalidate();
}
private void WindowListener_MessageArrived(object sender, EventArgs e)
{
bool glassEnabled = GlassEnabled;
if (glassEnabled && !this.IsGlassEnabled)
{
GlassEffectEnabledEventHandler glassEffectEnabledEvent = this.GlassEffectEnabledEvent;
if (glassEffectEnabledEvent != null)
{
glassEffectEnabledEvent(this, new EventArgs());
}
}
else if (!glassEnabled && this.IsGlassEnabled)
{
GlassEffectDisabledEventHandler glassEffectDisabledEvent = this.GlassEffectDisabledEvent;
if (glassEffectDisabledEvent != null)
{
glassEffectDisabledEvent(this, new EventArgs());
}
}
this.IsGlassEnabled = glassEnabled;
}
private GlassEffectEnabledEventHandler GlassEffectEnabledEvent;
private GlassEffectDisabledEventHandler GlassEffectDisabledEvent;
[AccessedThroughProperty("WindowListener")]
private APIs.HookWindow _WindowListener;
private bool IsGlassEnabled;
[AccessedThroughProperty("mParentForm")]
private Form _mParentForm;
private PictureBox mHeaderImage;
private int mTopBarSize;
private int mBottomBarSize;
private int mLeftBarSize;
private int mRightBarSize;
private Label mHeaderLabel;
private bool mUseHandCursorOnTitle;
private Point Last;
public delegate void GlassEffectEnabledEventHandler(object sender, EventArgs e);
public delegate void GlassEffectDisabledEventHandler(object sender, EventArgs e);
public class APIs
{
public APIs()
{
}
[DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateDIBSection(IntPtr hdc, ref APIs.BITMAPINFO pbmi, uint iUsage, int ppvBits, IntPtr hSection, uint dwOffset);
[DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern bool DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
[DllImport("dwmapi.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref APIs.MARGINS margins);
[DllImport("dwmapi.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern void DwmIsCompositionEnabled(ref bool IsIt);
[DllImport("UxTheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
public static extern int DrawThemeTextEx(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, string text, int iCharCount, int dwFlags, ref APIs.RECT pRect, ref APIs.S_DTTOPTS pOptions);
public const int DTT_COMPOSITED = 8192;
public const int DTT_GLOWSIZE = 2048;
public const int DTT_TEXTCOLOR = 1;
public const int SRCCOPY = 13369376;
public const int WM_SYSCOLORCHANGE = 21;
public struct MARGINS
{
public int Left;
public int Right;
public int Top;
public int Bottom;
}
public struct RECT
{
public RECT(int iLeft, int iTop, int iRight, int iBottom)
{
this = default(APIs.RECT);
this.Left = iLeft;
this.Top = iTop;
this.Right = iRight;
this.Bottom = iBottom;
}
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public struct BITMAPINFOHEADER
{
public int biSize;
public int biWidth;
public int biHeight;
public short biPlanes;
public short biBitCount;
public int biCompression;
public int biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public int biClrUsed;
public int biClrImportant;
}
public struct RGBQUAD
{
public byte rgbBlue;
public byte rgbGreen;
public byte rgbRed;
public byte rgbReserved;
}
public struct BITMAPINFO
{
public APIs.BITMAPINFOHEADER bmiHeader;
public APIs.RGBQUAD bmiColors;
}
public struct S_DTTOPTS
{
public int dwSize;
public int dwFlags;
public int crText;
public int crBorder;
public int crShadow;
public int iTextShadowType;
public Point ptShadowOffset;
public int iBorderSize;
public int iFontPropId;
public int iColorPropId;
public int iStateId;
public bool fApplyOverlay;
public int iGlowSize;
public int pfnDrawTextCallback;
public IntPtr lParam;
}
public class HookWindow : NativeWindow
{
public HookWindow()
{
CreateParams cp = new CreateParams();
this.CreateHandle(cp);
}
public event APIs.HookWindow.MessageArrivedEventHandler MessageArrived
{
[DebuggerNonUserCode]
[MethodImpl(MethodImplOptions.Synchronized)]
add
{
this.MessageArrivedEvent = (APIs.HookWindow.MessageArrivedEventHandler)Delegate.Combine(this.MessageArrivedEvent, value);
}
[DebuggerNonUserCode]
[MethodImpl(MethodImplOptions.Synchronized)]
remove
{
this.MessageArrivedEvent = (APIs.HookWindow.MessageArrivedEventHandler)Delegate.Remove(this.MessageArrivedEvent, value);
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 21)
{
APIs.HookWindow.MessageArrivedEventHandler messageArrivedEvent = this.MessageArrivedEvent;
if (messageArrivedEvent != null)
{
messageArrivedEvent(this, new EventArgs());
}
}
base.WndProc(ref m);
}
protected void Finalize()
{
this.DestroyHandle();
}
private APIs.HookWindow.MessageArrivedEventHandler MessageArrivedEvent;
public delegate void MessageArrivedEventHandler(object sender, EventArgs e);
}
}
}
}