Moved PInvoke stuff to seperate class, Added jank force refresh of window on theme change

This commit is contained in:
Usman Shafiq
2022-03-23 22:12:35 -04:00
parent c9bd9cd9d3
commit 8b66fd5825
7 changed files with 303 additions and 18 deletions
+46
View File
@@ -0,0 +1,46 @@
namespace VRCX
{
partial class InvisPopup
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Popup
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(148, 0);
this.Name = "Popup";
this.Text = "Popup";
this.ResumeLayout(false);
}
#endregion
}
}
+26
View File
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace VRCX
{
public partial class InvisPopup : Form
{
public InvisPopup()
{
InitializeComponent();
}
protected override void SetVisibleCore(bool value)
{
this.WindowState = FormWindowState.Minimized;
base.SetVisibleCore(value);
}
}
}
+120
View File
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
+35 -7
View File
@@ -39,34 +39,37 @@ namespace VRCX
this.TrayMenu_Separator = new System.Windows.Forms.ToolStripSeparator();
this.TrayMenu_Quit = new System.Windows.Forms.ToolStripMenuItem();
this.TrayIcon = new System.Windows.Forms.NotifyIcon(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.TrayMenu.SuspendLayout();
this.SuspendLayout();
//
// TrayMenu
//
this.TrayMenu.ImageScalingSize = new System.Drawing.Size(20, 20);
this.TrayMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.TrayMenu_Open,
this.TrayMenu_Separator,
this.TrayMenu_Quit});
this.TrayMenu.Name = "TrayMenu";
this.TrayMenu.Size = new System.Drawing.Size(132, 54);
this.TrayMenu.Size = new System.Drawing.Size(147, 58);
//
// TrayMenu_Open
//
this.TrayMenu_Open.Name = "TrayMenu_Open";
this.TrayMenu_Open.Size = new System.Drawing.Size(131, 22);
this.TrayMenu_Open.Size = new System.Drawing.Size(146, 24);
this.TrayMenu_Open.Text = "Open";
this.TrayMenu_Open.Click += new System.EventHandler(this.TrayMenu_Open_Click);
//
// TrayMenu_Separator
//
this.TrayMenu_Separator.Name = "TrayMenu_Separator";
this.TrayMenu_Separator.Size = new System.Drawing.Size(128, 6);
this.TrayMenu_Separator.Size = new System.Drawing.Size(143, 6);
//
// TrayMenu_Quit
//
this.TrayMenu_Quit.Name = "TrayMenu_Quit";
this.TrayMenu_Quit.Size = new System.Drawing.Size(131, 22);
this.TrayMenu_Quit.Size = new System.Drawing.Size(146, 24);
this.TrayMenu_Quit.Text = "Quit VRCX";
this.TrayMenu_Quit.Click += new System.EventHandler(this.TrayMenu_Quit_Click);
//
@@ -77,12 +80,35 @@ namespace VRCX
this.TrayIcon.Visible = true;
this.TrayIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.TrayIcon_MouseDoubleClick);
//
// button1
//
this.button1.Location = new System.Drawing.Point(91, 81);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Dark";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(196, 81);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "Light";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleDimensions = new System.Drawing.SizeF(120F, 120F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.ClientSize = new System.Drawing.Size(842, 561);
this.MinimumSize = new System.Drawing.Size(320, 240);
this.ClientSize = new System.Drawing.Size(1052, 701);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.MinimumSize = new System.Drawing.Size(396, 288);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "VRCX";
@@ -103,5 +129,7 @@ namespace VRCX
private System.Windows.Forms.ToolStripSeparator TrayMenu_Separator;
private System.Windows.Forms.ToolStripMenuItem TrayMenu_Quit;
private System.Windows.Forms.NotifyIcon TrayIcon;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
+10
View File
@@ -169,5 +169,15 @@ namespace VRCX
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
WinformThemer.SetGlobalTheme(1);
}
private void button2_Click(object sender, EventArgs e)
{
WinformThemer.SetGlobalTheme(0);
}
}
}
+9
View File
@@ -83,6 +83,12 @@
<Compile Include="AssetBundleCacher.cs" />
<Compile Include="CefCustomMenuHandler.cs" />
<Compile Include="ImageCache.cs" />
<Compile Include="InvisPopup.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="InvisPopup.Designer.cs">
<DependentUpon>InvisPopup.cs</DependentUpon>
</Compile>
<Compile Include="VRCEventDeserialization.cs" />
<Compile Include="IPCClient.cs" />
<Compile Include="IPCServer.cs" />
@@ -123,6 +129,9 @@
<SubType>Form</SubType>
</Compile>
<Compile Include="WinformThemer.cs" />
<EmbeddedResource Include="InvisPopup.resx">
<DependentUpon>InvisPopup.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="VRForm.resx">
<DependentUpon>VRForm.cs</DependentUpon>
</EmbeddedResource>
+57 -11
View File
@@ -13,12 +13,6 @@ namespace VRCX
internal static class WinformThemer
{
[DllImport("DwmApi")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int dwAttribute, int[] pvAttribute, int cbAttribute);
[DllImport("DwmApi")]
private static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, IntPtr pvAttribute, int cbAttribute);
private static int currentTheme = 0;
/// <summary>
@@ -29,7 +23,14 @@ namespace VRCX
public static void SetGlobalTheme(int theme)
{
currentTheme = theme;
foreach (Form form in Application.OpenForms)
List<Form> forms = new List<Form>();
foreach(Form form in Application.OpenForms)
{
forms.Add(form);
}
foreach (Form form in forms)
{
SetThemeToGlobal(form);
}
@@ -45,14 +46,32 @@ namespace VRCX
public static void SetThemeToGlobal(Form form)
{
SetThemeToGlobal(form.Handle);
InvisPopup thisJankThing = new InvisPopup();
thisJankThing.Show();
if (form.WindowState != FormWindowState.Minimized)
{
//attempting to refresh this god forsaken title bar
//Minimize, Downside: shows animation
//PInvokeFun.ShowWindow(form.Handle, (int)PInvokeFun.SW_TYPES.SW_MINIMIZE);
//PInvokeFun.ShowWindow(form.Handle, (int)PInvokeFun.SW_TYPES.SW_RESTORE);
//Hide, Downside: reorders window to last in taskbar if not pinned
PInvokeFun.ShowWindow(form.Handle, (int)PInvokeFun.SW_TYPES.SW_HIDE);
PInvokeFun.ShowWindow(form.Handle, (int)PInvokeFun.SW_TYPES.SW_SHOW);
}
thisJankThing.Close();
}
private static void SetThemeToGlobal(IntPtr handle)
{
if (GetTheme(handle) != currentTheme)
{
if (DwmSetWindowAttribute(handle, 19, new[] { currentTheme }, 4) != 0)
DwmSetWindowAttribute(handle, 20, new[] { currentTheme }, 4);
if (PInvokeFun.DwmSetWindowAttribute(handle, 19, new[] { currentTheme }, 4) != 0)
PInvokeFun.DwmSetWindowAttribute(handle, 20, new[] { currentTheme }, 4);
}
}
@@ -62,8 +81,8 @@ namespace VRCX
IntPtr curThemePtr = Marshal.AllocHGlobal(4);
//See what window state it currently is
if (DwmGetWindowAttribute(handle, 19, curThemePtr, 4) != 0)
DwmGetWindowAttribute(handle, 20, curThemePtr, 4);
if (PInvokeFun.DwmGetWindowAttribute(handle, 19, curThemePtr, 4) != 0)
PInvokeFun.DwmGetWindowAttribute(handle, 20, curThemePtr, 4);
//Read current theme (light = 0, dark = 1)
int theme = Marshal.ReadInt32(curThemePtr);
@@ -73,5 +92,32 @@ namespace VRCX
return theme;
}
internal static class PInvokeFun
{
[DllImport("DwmApi")]
internal static extern int DwmSetWindowAttribute(IntPtr hwnd, int dwAttribute, int[] pvAttribute, int cbAttribute);
[DllImport("DwmApi")]
internal static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, IntPtr pvAttribute, int cbAttribute);
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
internal enum SW_TYPES
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11
}
}
}
}