Skip to content

Commit 95b9915

Browse files
committed
feat: 起動時のウィンドウちらつきを軽減する対策
- ウィンドウ生成直後にWin32でウィンドウ色を直接指定してみる
1 parent 2bd8506 commit 95b9915

4 files changed

Lines changed: 32 additions & 20 deletions

File tree

NeeView/App.xaml.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public partial class App : Application
2424

2525

2626
private bool _isSplashScreenVisible;
27-
public SplashScreen? _splashScreen;
2827
private bool _isTerminated;
2928
private readonly int _tickBase = System.Environment.TickCount;
3029
private CommandLineOption? _option;
@@ -365,19 +364,11 @@ public void ShowSplashScreen(BootSetting bootSetting)
365364

366365
using var span = DebugSpan();
367366
var resourceName = "Resources/SplashScreen.png";
368-
_splashScreen = new SplashScreen(resourceName);
369-
_splashScreen.Show(true, true);
367+
var splashScreen = new SplashScreen(resourceName);
368+
splashScreen.Show(true, true);
370369
}
371370
}
372371

373-
/// <summary>
374-
/// Close SplashScreen
375-
/// </summary>
376-
public void CloseSplashScreen()
377-
{
378-
//_splashScreen?.Close(TimeSpan.FromMilliseconds(0));
379-
}
380-
381372
/// <summary>
382373
/// 多重起動用実行可能判定
383374
/// </summary>

NeeView/MainWindow/MainWindow.xaml.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using NeeLaboratory.ComponentModel;
44
using NeeLaboratory.Generators;
5+
using NeeView.Interop;
56
using NeeView.Native;
67
using NeeView.Setting;
78
using NeeView.Threading;
@@ -12,6 +13,7 @@
1213
using System.Linq;
1314
using System.Windows;
1415
using System.Windows.Input;
16+
using System.Windows.Interop;
1517
using System.Windows.Media;
1618

1719
namespace NeeView
@@ -389,12 +391,24 @@ private void OnRendering(object? sender, EventArgs e)
389391
LayoutFrame();
390392
}
391393

392-
393-
// ウィンドウソース初期化後イベント
394-
private void MainWindow_SourceInitialized(object sender, EventArgs e)
394+
// ウィンドウソース初期化後
395+
protected override void OnSourceInitialized(EventArgs e)
395396
{
397+
base.OnSourceInitialized(e);
398+
396399
Debug.WriteLine($"App.MainWindow.SourceInitialized: {App.Current.Stopwatch.ElapsedMilliseconds}ms");
397400

401+
if (App.Current.Resources["Window.Background"] is SolidColorBrush brush)
402+
{
403+
// Win32 の背景ブラシを設定して起動時のちらつきを軽減するテスト
404+
var hwnd = new WindowInteropHelper(this).Handle;
405+
int color = brush.Color.B << 16 | brush.Color.G << 8 | brush.Color.R;
406+
IntPtr blackBrush = NativeMethods.CreateSolidBrush(color);
407+
const int GCLP_HBRBACKGROUND = -10;
408+
NativeMethods.SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND, blackBrush);
409+
NativeMethods.InvalidateRect(hwnd, IntPtr.Zero, true);
410+
}
411+
398412
// Chrome の情報を最新にする
399413
_windowController.Refresh();
400414

@@ -405,13 +419,15 @@ private void MainWindow_SourceInitialized(object sender, EventArgs e)
405419
Debug.WriteLine($"App.MainWindow.SourceInitialized.Done: {App.Current.Stopwatch.ElapsedMilliseconds}ms");
406420
}
407421

422+
private void MainWindow_SourceInitialized(object sender, EventArgs e)
423+
{
424+
}
425+
408426
// ウィンドウ表示開始
409427
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
410428
{
411429
Debug.WriteLine($"App.MainWindow.Loaded: {App.Current.Stopwatch.ElapsedMilliseconds}ms");
412430

413-
App.Current.CloseSplashScreen();
414-
415431
MessageDialog.OwnerWindow = this;
416432

417433
_dpiProvider.SetDipScale(VisualTreeHelper.GetDpi(this));

NeeView/NeeView/Interop/Gid32.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Runtime.InteropServices;
5-
using System.Text;
6-
using System.Threading.Tasks;
73

84
namespace NeeView.Interop
95
{
106
internal static partial class NativeMethods
117
{
128
[DllImport("gdi32.dll")]
139
internal static extern bool DeleteObject(IntPtr hObject);
10+
11+
[DllImport("gdi32.dll")]
12+
internal static extern IntPtr CreateSolidBrush(int color);
1413
}
1514
}

NeeView/NeeView/Interop/User32.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,11 @@ internal static partial class NativeMethods
152152

153153
[DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
154154
internal extern static void RemoveClipboardFormatListener(nint hwnd);
155+
156+
[DllImport("user32", EntryPoint = "SetClassLongPtr")]
157+
internal static extern IntPtr SetClassLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
158+
159+
[DllImport("user32")]
160+
internal static extern bool InvalidateRect(IntPtr hWnd, IntPtr rect, bool erase);
155161
}
156162
}

0 commit comments

Comments
 (0)