Skip to content

Commit

Permalink
Port to .NET Framework 4.6
Browse files Browse the repository at this point in the history
  • Loading branch information
qt-kaneko committed Apr 4, 2024
1 parent 91e7bac commit 0f11213
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 146 deletions.
19 changes: 4 additions & 15 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"name": "Launch",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net7.0/ReMOSD.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "externalTerminal",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
"preLaunchTask": "Build",
"program": "${workspaceFolder}/bin/ReMOSD.dll",
"console": "integratedTerminal"
}
]
}
33 changes: 6 additions & 27 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,18 @@
"version": "2.0.0",
"tasks": [
{
"label": "build",
"label": "Build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/ReMOSD.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/ReMOSD.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
"args": ["build"],
"problemMatcher": "$msCompile",
"isBuildCommand": true
},
{
"label": "watch",
"label": "Publish",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/ReMOSD.csproj"
],
"args": ["publish", "-f", "net46"],
"problemMatcher": "$msCompile"
}
]
Expand Down
59 changes: 0 additions & 59 deletions MediaOsd.cs

This file was deleted.

11 changes: 0 additions & 11 deletions NativeMethods.txt

This file was deleted.

27 changes: 8 additions & 19 deletions ReMOSD.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,21 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>

<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>

<PropertyGroup>
<NoWarn>CA1416</NoWarn> <!-- This call site is reachable on all platforms. -->
</PropertyGroup>
<TargetFrameworks>net46</TargetFrameworks>
<GenerateSupportedRuntime>false</GenerateSupportedRuntime>
<PublishRelease>true</PublishRelease>

<PropertyGroup>
<InvariantGlobalization>true</InvariantGlobalization>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<DebugType Condition="'$(Configuration)' == 'Release'">none</DebugType>
<OutputPath>bin</OutputPath>
<PublishDir>bin</PublishDir>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<PublishAot Condition="!($([MSBuild]::IsOSPlatform('OSX')) AND '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64')">true</PublishAot>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.49-beta">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
55 changes: 55 additions & 0 deletions src/MediaOsd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;

using static ReMOSD.PInvoke;

namespace ReMOSD;

class MediaOsd
{
public nint HWnd { get; private set; }

MediaOsd() {}

public void SetRegion(nint value)
{
var result = SetWindowRgn(HWnd, value, true);
if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error());
}

public uint GetDpi()
{
var dpi = GetDpiForWindow(HWnd);
if (dpi == 0) throw new Win32Exception(Marshal.GetLastWin32Error());

return dpi;
}

public static MediaOsd Find()
{
var shellHWnd = GetShellWindow();

// Trigger show volume control window so it is created and can be found later
// (to pack APPCOMMAND lParam https://stackoverflow.com/a/29301152/18449435)
// (https://forums.codeguru.com/showthread.php?147192-How-to-construct-WM_APPCOMMAND-message)
SendMessage(shellHWnd, WM_APPCOMMAND, 0, (int)APPCOMMAND_VOLUME_MUTE << 16);
SendMessage(shellHWnd, WM_APPCOMMAND, 0, (int)APPCOMMAND_VOLUME_MUTE << 16);

var hWnd = default(nint);
for (var attempt = 1; attempt <= 5; ++attempt)
{
hWnd = FindWindowExA(default, default, "NativeHWNDHost\0", default);
if (hWnd != default) break;

Thread.Sleep(250);
}
if (hWnd == default) throw new InvalidOperationException("Media OSD was not found :(");

var osd = new MediaOsd() {
HWnd = hWnd
};
return osd;
}
}
40 changes: 40 additions & 0 deletions src/PInvoke.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Runtime.InteropServices;

namespace ReMOSD;

static partial class PInvoke
{
/// <summary> https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-appcommand </summary>
public const uint WM_APPCOMMAND = 793;

/// <summary> https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-appcommand </summary>
public const uint APPCOMMAND_VOLUME_MUTE = 8;

/// <summary> https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowrgn </summary>
[DllImport("user32.dll")]
public static extern int SetWindowRgn(nint hWnd, nint hRgn, bool bRedraw);

/// <summary> https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdpiforwindow </summary>
[DllImport("user32.dll")]
public static extern uint GetDpiForWindow(nint hWnd);

/// <summary> https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getshellwindow </summary>
[DllImport("user32.dll")]
public static extern nint GetShellWindow();

/// <summary> https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage </summary>
[DllImport("user32.dll")]
public static extern nint SendMessage(nint hWnd, uint Msg, nint wParam, nint lParam);

/// <summary> https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowexa </summary>
[DllImport("user32.dll")]
public static extern nint FindWindowExA(nint hWndParent, nint hWndChildAfter, string lpszClass, string? lpszWindow);

/// <summary> https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createrectrgn </summary>
[DllImport("gdi32.dll")]
public static extern nint CreateRectRgn(int x1, int y1, int x2, int y2);

/// <summary> https://learn.microsoft.com/en-us/windows/console/allocconsole </summary>
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();
}
27 changes: 12 additions & 15 deletions Program.cs → src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
using System;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;

using Windows.Win32.Graphics.Gdi;

using static Windows.Win32.PInvoke;


Size _miniOsdSize = new(65, 140);
using ReMOSD;
using static ReMOSD.PInvoke;

AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

var osd = MediaOsd.Find();

HRGN newOsdRegion;
if (args.Contains("--restore") || args.Contains("-r")) newOsdRegion = new HRGN();
IntPtr newOsdRegion;
if (args.Contains("--restore") || args.Contains("-r"))
{
newOsdRegion = new IntPtr();
}
else
{
var osdScalingCoefficient = osd.Dpi / 96.0f;
var osdScalingCoefficient = osd.GetDpi() / 96.0f;

newOsdRegion = CreateRectRgn(0, 0,
(int)Math.Round(_miniOsdSize.Width * osdScalingCoefficient),
(int)Math.Round(_miniOsdSize.Height * osdScalingCoefficient));
(int)Math.Round(65 * osdScalingCoefficient),
(int)Math.Round(140 * osdScalingCoefficient));
if (newOsdRegion == default) throw new Win32Exception(Marshal.GetLastWin32Error());
}

osd.Region = newOsdRegion;

osd.SetRegion(newOsdRegion);

static void OnUnhandledException(object s, UnhandledExceptionEventArgs e)
{
Expand Down

0 comments on commit 0f11213

Please sign in to comment.