Advertisement
Guest User

Network Utility Tools

a guest
Mar 27th, 2022
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace NetworkUtilityTools
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         //Color colorBackgroundDark = Color.FromArgb(255, 32, 32, 32);
  21.         Color colorBackgroundDark = Color.FromArgb(255, 39, 39, 39);
  22.         //Color colorBackgroundDark = Color.Blue;
  23.         //Color colorBackgroundDark = SystemColors.ControlDarkDark;
  24.         [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
  25.         public partial class TabControlPaint : TabControl
  26.         {
  27.             public enum HRESULT : int
  28.             {
  29.                 S_OK = 0,
  30.                 S_FALSE = 1,
  31.                 E_NOINTERFACE = unchecked((int)0x80004002),
  32.                 E_NOTIMPL = unchecked((int)0x80004001),
  33.                 E_FAIL = unchecked((int)0x80004005),
  34.                 E_UNEXPECTED = unchecked((int)0x8000FFFF),
  35.                 E_OUTOFMEMORY = unchecked((int)0x8007000E)
  36.             }
  37.             [DllImport("UxTheme.dll", SetLastError = true, CharSet = CharSet.Auto)]
  38.             public static extern IntPtr BeginBufferedPaint(IntPtr hdcTarget, ref RECT prcTarget, BP_BUFFERFORMAT dwFormat, IntPtr pPaintParams, out IntPtr phdc);
  39.             public enum BP_BUFFERFORMAT
  40.             {
  41.                 BPBF_COMPATIBLEBITMAP,
  42.                 BPBF_DIB,
  43.                 BPBF_TOPDOWNDIB,
  44.                 BPBF_TOPDOWNMONODIB
  45.             }
  46.             [DllImport("UxTheme.dll", SetLastError = true, CharSet = CharSet.Auto)]
  47.             public static extern HRESULT EndBufferedPaint(IntPtr hBufferedPaint, bool fUpdateTarget);
  48.             [DllImport("UxTheme.dll", SetLastError = true, CharSet = CharSet.Auto)]
  49.             public static extern HRESULT BufferedPaintInit();
  50.             [DllImport("UxTheme.dll", SetLastError = true, CharSet = CharSet.Auto)]
  51.             public static extern HRESULT BufferedPaintUnInit();
  52.             [DllImport("User32.dll", SetLastError = true)]
  53.             public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
  54.             [StructLayout(LayoutKind.Sequential)]
  55.             public struct RECT
  56.             {
  57.                 public int left;
  58.                 public int top;
  59.                 public int right;
  60.                 public int bottom;
  61.                 public RECT(int Left, int Top, int Right, int Bottom)
  62.                 {
  63.                     left = Left;
  64.                     top = Top;
  65.                     right = Right;
  66.                     bottom = Bottom;
  67.                 }
  68.             }
  69.             [DllImport("Gdi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  70.             public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  71.             [DllImport("Gdi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  72.             public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
  73.             [DllImport("Gdi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  74.             public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  75.             [DllImport("Gdi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  76.             public static extern bool DeleteDC(IntPtr hDC);
  77.             [DllImport("Gdi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  78.             public static extern bool DeleteObject(IntPtr ho);
  79.             [DllImport("Gdi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  80.             public static extern IntPtr CreateSolidBrush(int crColor);
  81.             [DllImport("Msimg32.dll", SetLastError = true)]
  82.             public static extern bool TransparentBlt(IntPtr hdcDest, int xoriginDest, int yoriginDest, int wDest, int hDest, IntPtr hdcSrc, int xoriginSrc, int yoriginSrc, int wSrc, int hSrc, uint crTransparent);
  83.             [DllImport("Gdi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  84.             public static extern bool BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
  85.             public const int SRCCOPY = 0x00CC0020;
  86.             [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  87.             public static extern bool FillRect(IntPtr hdc, [In] ref RECT rect, IntPtr hbrush);
  88.             [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  89.             public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
  90.             public const int WM_PRINT = 0x0317;
  91.             public const int WM_PRINTCLIENT = 0x0318;
  92.             public const int PRF_CHECKVISIBLE = 0x00000001;
  93.             public const int PRF_NONCLIENT = 0x00000002;
  94.             public const int PRF_CLIENT = 0x00000004;
  95.             public const int PRF_ERASEBKGND = 0x00000008;
  96.             public const int PRF_CHILDREN = 0x00000010;
  97.             public const int PRF_OWNED = 0x00000020;
  98.             [DllImport("User32.dll", SetLastError = true, ExactSpelling = true, EntryPoint = "BeginPaint", CharSet = CharSet.Auto)]
  99.             private static extern int BeginPaint(IntPtr hWnd, [In, Out] ref PAINTSTRUCT lpPaint);
  100.             [DllImport("User32.dll", SetLastError = true, ExactSpelling = true, EntryPoint = "EndPaint", CharSet = CharSet.Auto)]
  101.             private static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint);
  102.             [StructLayout(LayoutKind.Sequential)]
  103.             public struct PAINTSTRUCT
  104.             {
  105.                 public IntPtr hdc;
  106.                 public bool fErase;
  107.                 public int rcPaint_left;
  108.                 public int rcPaint_top;
  109.                 public int rcPaint_right;
  110.                 public int rcPaint_bottom;
  111.                 public bool fRestore;
  112.                 public bool fIncUpdate;
  113.                 public int reserved1;
  114.                 public int reserved2;
  115.                 public int reserved3;
  116.                 public int reserved4;
  117.                 public int reserved5;
  118.                 public int reserved6;
  119.                 public int reserved7;
  120.                 public int reserved8;
  121.             }
  122.             public const int WM_PAINT = 0x000F;
  123.             public TabControlPaint()
  124.             {
  125.                 BufferedPaintInit();
  126.                 //this.SetStyle(ControlStyles.UserPaint, true);
  127.                 this.DrawMode = TabDrawMode.OwnerDrawFixed;
  128.                 this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(tabControl_DrawItem);
  129.             }
  130.             protected override void OnCreateControl()
  131.             {
  132.                 base.OnCreateControl();
  133.                 ((Form)this.Parent).Closing += new CancelEventHandler(Parent_Closing);
  134.                 ((Form)this.Parent).Load += Parent_Load;
  135.             }
  136.             private void Parent_Load(object sender, EventArgs e)
  137.             {
  138.                 foreach (TabPage tp in this.TabPages)
  139.                 {
  140.                     tp.BackColor = Color.FromArgb(255, 39, 39, 39);
  141.                 }
  142.             }
  143.             private void Parent_Closing(object sender, CancelEventArgs e)
  144.             {
  145.                 BufferedPaintUnInit();
  146.             }
  147.             private void tabControl_DrawItem(object sender, DrawItemEventArgs e)
  148.             {
  149.                 TabControl tc = (TabControl)sender;
  150.                 TabPage tp = tc.TabPages[e.Index];
  151.                 SolidBrush backBrush = (e.State == DrawItemState.Selected) ? new SolidBrush(tp.BackColor) : new SolidBrush(Color.Black);
  152.                 e.Graphics.FillRectangle(backBrush, e.Bounds);
  153.                 Rectangle rectBounds = e.Bounds;
  154.                 int nYOffset = (e.State == DrawItemState.Selected) ? -1 : 1;
  155.                 rectBounds.Offset(1, nYOffset);
  156.                 Color textColor = (e.State == DrawItemState.Selected) ? Color.White : Color.LightGray;
  157.                 Color backColor = Color.FromArgb(255, 39, 39, 39);
  158.                 if (backColor == SystemColors.Control)
  159.                     textColor = (e.State == DrawItemState.Selected) ? Color.Black : Color.LightGray;
  160.                 TextRenderer.DrawText(e.Graphics, tp.Text, e.Font, rectBounds, textColor);
  161.             }
  162.             protected override void WndProc(ref Message m)
  163.             {
  164.                 if (m.Msg == WM_PAINT)
  165.                 {
  166.                     PAINTSTRUCT ps = new PAINTSTRUCT();
  167.                     IntPtr hDC = (IntPtr)BeginPaint(m.HWnd, ref ps);
  168.                     IntPtr targetDC = hDC;
  169.                     IntPtr bufferedDC = IntPtr.Zero;
  170.                     HRESULT hr = HRESULT.S_OK;
  171.                     RECT rectClient;
  172.                     GetClientRect(this.Handle, out rectClient);
  173.                     IntPtr pb = BeginBufferedPaint(targetDC, ref rectClient, BP_BUFFERFORMAT.BPBF_TOPDOWNDIB, IntPtr.Zero, out bufferedDC);
  174.                     IntPtr hDCMem = CreateCompatibleDC(hDC);
  175.                     IntPtr hBitmap = CreateCompatibleBitmap(hDC, rectClient.right - rectClient.left, rectClient.bottom - rectClient.top);
  176.                     IntPtr hBitmapOld = SelectObject(hDCMem, hBitmap);
  177.                     // Copie control image into Memory DC with custom color as background
  178.                     IntPtr hBrush1 = CreateSolidBrush(ColorTranslator.ToWin32(Color.FromArgb(255, 255, 254, 255)));
  179.                     FillRect(hDCMem, ref rectClient, hBrush1);
  180.                     DeleteObject(hBrush1);
  181.                     SendMessage(this.Handle, WM_PRINTCLIENT, (int)hDCMem, (IntPtr)PRF_CLIENT);
  182.                     IntPtr hDCMem2 = CreateCompatibleDC(hDC);
  183.                     IntPtr hBitmap2 = CreateCompatibleBitmap(hDC, rectClient.right - rectClient.left, rectClient.bottom - rectClient.top);
  184.                     IntPtr hBitmapOld2 = SelectObject(hDCMem2, hBitmap2);
  185.                     // Custom color => Back color
  186.                     IntPtr hBrush2 = CreateSolidBrush(ColorTranslator.ToWin32(this.Parent.BackColor));
  187.                     FillRect(hDCMem2, ref rectClient, hBrush2);
  188.                     DeleteObject(hBrush2);
  189.                     TransparentBlt(hDCMem2, 0, 0, rectClient.right - rectClient.left, rectClient.bottom - rectClient.top, hDCMem, 0, 0,
  190.                         rectClient.right - rectClient.left, rectClient.bottom - rectClient.top, (uint)ColorTranslator.ToWin32(Color.FromArgb(255, 255, 254, 255)));
  191.                     BitBlt(bufferedDC, 0, 0, rectClient.right - rectClient.left, rectClient.bottom - rectClient.top, hDCMem2, 0, 0, SRCCOPY);
  192.                     SelectObject(hDCMem, hBitmapOld);
  193.                     DeleteObject(hBitmap);
  194.                     DeleteDC(hDCMem);
  195.                     SelectObject(hDCMem2, hBitmapOld2);
  196.                     DeleteObject(hBitmap2);
  197.                     DeleteDC(hDCMem2);
  198.                     hr = EndBufferedPaint(pb, true);
  199.                     EndPaint(m.HWnd, ref ps);
  200.                     m.Result = IntPtr.Zero;
  201.                 }
  202.                 else
  203.                     base.WndProc(ref m);
  204.             }
  205.         }
  206.  
  207.         private void activate_dtheme(object sender, EventArgs e)
  208.         {
  209.             this.BackColor = Color.FromArgb(255, 39, 39, 39);
  210.             tabPage1.BackColor = Color.FromArgb(255, 39, 39, 39);
  211.         }
  212.     }
  213.  
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement