Skip to content

Commit

Permalink
feat: 添加了剪切板图像复制
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyfairy committed Jan 22, 2024
1 parent 6c1e622 commit f004e93
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 104 deletions.
17 changes: 8 additions & 9 deletions src/CurvaLauncher/Apis/ClipboardApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
using System.Windows.Media;
using CurvaLauncher.Utilities;

namespace CurvaLauncher.Apis
namespace CurvaLauncher.Apis;

public class ClipboardApi : IClipboardApi
{
public class ClipboardApi : IClipboardApi
{
private ClipboardApi() { }
public static ClipboardApi Instance { get; } = new();
private ClipboardApi() { }
public static ClipboardApi Instance { get; } = new();

public void SetImage(ImageSource image) => ClipboardUtils.SetBitmap(image);
public void SetImage(Image image) => ClipboardUtils.SetBitmap(image);
public void SetText(string text) => ClipboardUtils.SetText(text);
}
public void SetImage(ImageSource image) => ClipboardUtils.SetBitmap(image);
public void SetImage(Image image) => ClipboardUtils.SetBitmap(image);
public void SetText(string text) => ClipboardUtils.SetText(text);
}
103 changes: 94 additions & 9 deletions src/CurvaLauncher/Utilities/ClipboardUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Drawing;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Media;
Expand All @@ -11,22 +12,41 @@ namespace CurvaLauncher.Utilities;
static class ClipboardUtils
{
[DllImport("User32")]
static extern bool OpenClipboard(IntPtr hWndNewOwner);
private static extern bool OpenClipboard(IntPtr hWndNewOwner);

[DllImport("User32")]
static extern bool CloseClipboard();
private static extern bool CloseClipboard();

[DllImport("User32")]
static extern bool EmptyClipboard();
private static extern bool EmptyClipboard();

[DllImport("User32")]
static extern bool IsClipboardFormatAvailable(int format);
private static extern bool IsClipboardFormatAvailable(int format);

[DllImport("User32")]
static extern IntPtr GetClipboardData(int uFormat);
private static extern IntPtr GetClipboardData(int uFormat);

[DllImport("User32", CharSet = CharSet.Unicode)]
static extern IntPtr SetClipboardData(int uFormat, IntPtr hMem);
private static extern IntPtr SetClipboardData(int uFormat, IntPtr hMem);

[DllImport("gdi32.dll")]
private static extern int GetObject(nint hObject, int c, out nint resultPtr);

[DllImport("gdi32.dll")]
private static extern bool DeleteObject(nint hObject);

[DllImport("gdi32.dll")]
private static extern nint CreateDIBitmap(nint hdc, in BITMAPINFOHEADER pbmih, uint flInit, nint pjBits, in byte pbmi, uint iUsage);

[DllImport("user32.dll")]
private static extern nint GetDC(nint hwnd);

[DllImport("user32.dll")]
private static extern int ReleaseDC(nint hwnd, nint hdc);





static void CoreSetText(string text, int remainCount)
{
Expand Down Expand Up @@ -123,15 +143,80 @@ public static void SetBitmap(Bitmap bitmap)
{
var hBitmap = bitmap.GetHbitmap();

CoreSetBitmap(hBitmap, 3);
SetBitmap(hBitmap);
DeleteObject(hBitmap);
}

/// <summary>
/// 向剪切板中添加图片
/// </summary>
/// <param name="hBitmap">位图句柄</param>
public static void SetBitmap(nint hBitmap)
private static unsafe void SetBitmap(nint hBitmap)
{
CoreSetBitmap(hBitmap, 3);
DIBSECTION ds;
GetObject(hBitmap, sizeof(DIBSECTION), out *(nint*)&ds);

ds.dsBmih.biCompression = 0;
var hdc = GetDC(0);
nint hbitmap_ddb = CreateDIBitmap(hdc, in ds.dsBmih, 0x4/*CBM_INIT*/, ds.dsBm.bmBits, Unsafe.As<BITMAPINFOHEADER, byte>(ref ds.dsBmih), /*DIB_RGB_COLORS*/ 0);
ReleaseDC(0, hdc);

CoreSetBitmap(hbitmap_ddb, 3);
DeleteObject(hbitmap_ddb);
}





private struct BITMAP
{
public int bmType;
public int bmWidth;
public int bmHeight;
public int bmWidthBytes;
public ushort bmPlanes;
public ushort bmBitsPixel;
public nint bmBits;
}

private struct DIBSECTION
{
public BITMAP dsBm;
public BITMAPINFOHEADER dsBmih;
public uint dsBitfields1;
public uint dsBitfields2;
public uint dsBitfields3;
public nint dshSection;
public uint dsOffset;
}

private struct BITMAPINFOHEADER
{
public uint biSize;
public int biWidth;
public int biHeight;
public ushort biPlanes;
public ushort biBitCount;
public uint biCompression;
public uint biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;
}

private struct BITMAPINFO
{
public BITMAPINFOHEADER bmiHeader;
public RGBQUAD bmiColors;
}

private struct RGBQUAD
{
public byte rgbBlue;
public byte rgbGreen;
public byte rgbRed;
public byte rgbReserved;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);NETSTANDARD2_0</DefineConstants>
<DefineConstants>$(DefineConstants);NETSTANDARD;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public Task InvokeAsync(CancellationToken cancellationToken)
Width = Width,
Height = Height,
Margin = Margin,
}
Hints = { { EncodeHintType.CHARACTER_SET, "UTF-8" } }
},
};

using var image = writer.Write(Content);
Expand Down

0 comments on commit f004e93

Please sign in to comment.