Skip to content

Commit

Permalink
Fixed crash issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lich426 committed Mar 5, 2023
1 parent 48717ca commit c977ce2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 59 deletions.
3 changes: 2 additions & 1 deletion FanCtrl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PublisherName>Lich</PublisherName>
<SuiteName>FanCtrl</SuiteName>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.5.8.0</ApplicationVersion>
<ApplicationVersion>1.5.9.0</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<CreateDesktopShortcut>true</CreateDesktopShortcut>
<PublishWizardCompleted>true</PublishWizardCompleted>
Expand Down Expand Up @@ -310,6 +310,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>StringLib.ko.resx</DependentUpon>
</Compile>
<Compile Include="src\UI\Theme\Theme.cs" />
<Compile Include="src\UI\Theme\ThemeListView.cs">
<SubType>Component</SubType>
</Compile>
Expand Down
6 changes: 3 additions & 3 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
//
// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를
// 기본값으로 할 수 있습니다.
[assembly: AssemblyVersion("1.5.8")]
[assembly: AssemblyFileVersion("1.5.8")]
[assembly: AssemblyInformationalVersion("1.5.8")]
[assembly: AssemblyVersion("1.5.9")]
[assembly: AssemblyFileVersion("1.5.9")]
[assembly: AssemblyInformationalVersion("1.5.9")]
Binary file modified dll/DarkUI.dll
Binary file not shown.
11 changes: 3 additions & 8 deletions src/UI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,9 @@ protected void setTheme()
{
ThemeProvider.Theme = new LightTheme();
}
var preference = Convert.ToInt32(type == THEME_TYPE.DARK);
DwmSetWindowAttribute(this.Handle,
DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE,
ref preference, sizeof(uint));
BackColor = ThemeProvider.Theme.Colors.GreyBackground;

Theme.setTheme(this.Handle, (type == THEME_TYPE.DARK));
}

protected override void OnLoad(EventArgs e)
Expand Down Expand Up @@ -456,10 +454,7 @@ private void onTrayMenuShow(object sender, EventArgs e)
}

var type = OptionManager.getInstance().getNowTheme();
var preference = Convert.ToInt32(type == THEME_TYPE.DARK);
DwmSetWindowAttribute(this.Handle,
DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE,
ref preference, sizeof(uint));
Theme.setTheme(this.Handle, (type == THEME_TYPE.DARK));
}

private void onTrayMenuExit(object sender, EventArgs e)
Expand Down
50 changes: 50 additions & 0 deletions src/UI/Theme/Theme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FanCtrl
{
public enum THEME_TYPE
{
SYSTEM,
LIGHT,
DARK,
}

public enum DWMWINDOWATTRIBUTE
{
DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19,
DWMWA_USE_IMMERSIVE_DARK_MODE = 20,
}

class Theme
{
public static bool isWindows10OrGreater(int build = -1)
{
var version = Environment.OSVersion.Version;
return version.Major >= 10 && version.Build >= build;
}

public static void setTheme(IntPtr handle, bool isDarkMode)
{
if (isWindows10OrGreater(17763))
{
int attribute = (int)DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
if (isWindows10OrGreater(18985))
{
attribute = (int)DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE;
}

int useImmersiveDarkMode = isDarkMode ? 1 : 0;
DwmSetWindowAttribute(handle, attribute, ref useImmersiveDarkMode, sizeof(int));
}
}

[DllImport("dwmapi.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
}
}
49 changes: 2 additions & 47 deletions src/UI/Theme/ThemeForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,12 @@

namespace FanCtrl
{
public enum THEME_TYPE
{
SYSTEM,
LIGHT,
DARK,
}

public enum DWMWINDOWATTRIBUTE : uint
{
DWMWA_NCRENDERING_ENABLED,
DWMWA_NCRENDERING_POLICY,
DWMWA_TRANSITIONS_FORCEDISABLED,
DWMWA_ALLOW_NCPAINT,
DWMWA_CAPTION_BUTTON_BOUNDS,
DWMWA_NONCLIENT_RTL_LAYOUT,
DWMWA_FORCE_ICONIC_REPRESENTATION,
DWMWA_FLIP3D_POLICY,
DWMWA_EXTENDED_FRAME_BOUNDS,
DWMWA_HAS_ICONIC_BITMAP,
DWMWA_DISALLOW_PEEK,
DWMWA_EXCLUDED_FROM_PEEK,
DWMWA_CLOAK,
DWMWA_CLOAKED,
DWMWA_FREEZE_REPRESENTATION,
DWMWA_PASSIVE_UPDATE_MODE,
DWMWA_USE_HOSTBACKDROPBRUSH,
DWMWA_USE_IMMERSIVE_DARK_MODE = 20,
DWMWA_WINDOW_CORNER_PREFERENCE = 33,
DWMWA_BORDER_COLOR,
DWMWA_CAPTION_COLOR,
DWMWA_TEXT_COLOR,
DWMWA_VISIBLE_FRAME_BORDER_THICKNESS,
DWMWA_SYSTEMBACKDROP_TYPE,
DWMWA_LAST
}

public partial class ThemeForm : DarkForm
{
public ThemeForm()
{
var type = OptionManager.getInstance().getNowTheme();
var preference = Convert.ToInt32(type == THEME_TYPE.DARK);
DwmSetWindowAttribute(this.Handle,
DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE,
ref preference, sizeof(uint));
var type = OptionManager.getInstance().getNowTheme();
Theme.setTheme(this.Handle, (type == THEME_TYPE.DARK));
}

[DllImport("dwmapi.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern void DwmSetWindowAttribute(IntPtr hwnd,
DWMWINDOWATTRIBUTE attribute,
ref int pvAttribute,
uint cbAttribute);
}
}

0 comments on commit c977ce2

Please sign in to comment.