Skip to content

Commit

Permalink
feat: 支持 Everything 打开属性窗口, 或者打开所在文件夹
Browse files Browse the repository at this point in the history
  • Loading branch information
SlimeNull committed Mar 27, 2024
1 parent 6f27d76 commit 5428b61
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/CurvaLauncher.Common/Apis/ICommonApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public interface ICommonApi
public void Open(string name);
public void OpenExecutable(string file);

public void ShowInFileExplorer(string path);
public void ShowPropertiesWindow(string path);

public void ShowText(string text, TextOptions options);
public void ShowImage(ImageSource image, ImageOptions options);
}
42 changes: 41 additions & 1 deletion src/CurvaLauncher/Apis/CommonApi.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Media;
using CurvaLauncher.Views.Dialogs;

namespace CurvaLauncher.Apis
{
public class CommonApi : ICommonApi
{
[DllImport("shell32.dll", ExactSpelling = true)]
static extern void ILFree(IntPtr pidlList);

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr ILCreateFromPathW([MarshalAs(UnmanagedType.LPWStr)] string pszPath);

[DllImport("shell32.dll")]
static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cidl, IntPtr[]? apidl, uint dwFlags);

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
public static extern bool SHObjectProperties(IntPtr hwnd, uint shopObjectType, string pszObjectName, string? pszPropertyPage);

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
public static extern int ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string? lpParameters, string? lpDirectory, int nShowCmd);

private CommonApi() { }

public static CommonApi Instance { get; } = new();
Expand All @@ -24,6 +41,29 @@ public void OpenExecutable(string file) => Process.Start(
UseShellExecute = false
});

public void ShowInFileExplorer(string path)
{
IntPtr pidlList = ILCreateFromPathW(path);
if (pidlList == IntPtr.Zero)
{
throw new ArgumentException("Invalid path");
}

try
{
SHOpenFolderAndSelectItems(pidlList, 0, null, 0);
}
finally
{
ILFree(pidlList);
}
}

public void ShowPropertiesWindow(string path)
{
bool invoked = SHObjectProperties(IntPtr.Zero, 2, path, null);
}

public void ShowImage(ImageSource image, ImageOptions options)
{
new SimpleImageDialog(image, options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public void Invoke()
{
if (_plugin.HostContext.IsAltKeyPressed())
{

_plugin.HostContext.Api.ShowPropertiesWindow(_itemFullPath);
}
else if (_plugin.HostContext.IsCtrlKeyPressed())
{

_plugin.HostContext.Api.ShowInFileExplorer(_itemFullPath);
}
else
{
Expand Down
42 changes: 42 additions & 0 deletions src/TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,48 @@
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using System;
using System.Runtime.InteropServices;


[DllImport("shell32.dll", ExactSpelling = true)]
static extern void ILFree(IntPtr pidlList);

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr ILCreateFromPathW([MarshalAs(UnmanagedType.LPWStr)] string pszPath);

[DllImport("shell32.dll")]
static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cidl, IntPtr[] apidl, uint dwFlags);

static void ShowInFileExplorer(string path)
{
IntPtr pidlList = ILCreateFromPathW(path);
if (pidlList == IntPtr.Zero)
{
throw new ArgumentException("Invalid path");
}

try
{
SHOpenFolderAndSelectItems(pidlList, 0, null, 0);
}
finally
{
ILFree(pidlList);
}
}

string filePath = @"C:\Users";

// Example usage
try
{
ShowInFileExplorer(filePath);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}

var w = new QRCodeWriter();
var b = w.encode("Fuck you world", BarcodeFormat.QR_CODE, 100, 100);
Expand Down

0 comments on commit 5428b61

Please sign in to comment.