|
分享源码
界面截图: |
|
是否带模块: |
纯源码 |
备注说明: |
- |
本帖最后由 zxxiaopi 于 2024-9-27 19:09 编辑
最近听说Cursor很好用,这几天捣鼓了下,发现真不错!用这AI在delphi里写了dll,vsstudio里也写了,都可以被易调用,相当好用啊!
这里来个例子,C#的类库dll,测试了几个易组件没问题,理论应该都可以。
下面是C#源码,需要安装.net4.0,没办法,c#类库就这德性
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace AutoScalingDLL
{
public class AutoScaler
{
[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
private static extern uint GetCurrentThreadId();
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
private const int WH_CALLWNDPROC = 4;
private const int WM_SIZE = 0x0005;
private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
private struct CWPSTRUCT
{
public IntPtr lParam;
public IntPtr wParam;
public int message;
public IntPtr hwnd;
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private static IntPtr _hookHandle = IntPtr.Zero;
private static HookProc _hookProc;
private static IntPtr _mainWindowHandle;
private static Dictionary<IntPtr, ControlInfo> _controls = new Dictionary<IntPtr, ControlInfo>();
private static RECT _originalWindowRect;
private class ControlInfo
{
public float LeftRatio;
public float TopRatio;
public float WidthRatio;
public float HeightRatio;
}
[DllExport("InitializeAutoScaling", CallingConvention = CallingConvention.StdCall)]
public static bool InitializeAutoScaling(IntPtr hWnd)
{
if (_hookHandle != IntPtr.Zero)
{
return false; // Already initialized
}
_mainWindowHandle = hWnd;
GetWindowRect(_mainWindowHandle, out _originalWindowRect);
_hookProc = new HookProc(WindowProc);
_hookHandle = SetWindowsHookEx(WH_CALLWNDPROC, _hookProc, IntPtr.Zero, GetCurrentThreadId());
return _hookHandle != IntPtr.Zero;
}
[DllExport("AddControlToAutoScale", CallingConvention = CallingConvention.StdCall)]
public static bool AddControlToAutoScale(IntPtr hWnd)
{
if (_hookHandle == IntPtr.Zero || _controls.ContainsKey(hWnd))
{
return false;
}
RECT controlRect;
GetWindowRect(hWnd, out controlRect);
int windowWidth = _originalWindowRect.Right - _originalWindowRect.Left;
int windowHeight = _originalWindowRect.Bottom - _originalWindowRect.Top;
ControlInfo info = new ControlInfo
{
LeftRatio = (float)(controlRect.Left - _originalWindowRect.Left) / windowWidth,
TopRatio = (float)(controlRect.Top - _originalWindowRect.Top) / windowHeight,
WidthRatio = (float)(controlRect.Right - controlRect.Left) / windowWidth,
HeightRatio = (float)(controlRect.Bottom - controlRect.Top) / windowHeight
};
_controls.Add(hWnd, info);
return true;
}
private static IntPtr WindowProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
CWPSTRUCT cwp = (CWPSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPSTRUCT));
if (cwp.message == WM_SIZE && cwp.hwnd == _mainWindowHandle)
{
RECT newRect;
GetWindowRect(_mainWindowHandle, out newRect);
int newWidth = newRect.Right - newRect.Left;
int newHeight = newRect.Bottom - newRect.Top;
foreach (var kvp in _controls)
{
IntPtr controlHandle = kvp.Key;
ControlInfo info = kvp.Value;
int newLeft = (int)(info.LeftRatio * newWidth);
int newTop = (int)(info.TopRatio * newHeight);
int newControlWidth = (int)(info.WidthRatio * newWidth);
int newControlHeight = (int)(info.HeightRatio * newHeight);
MoveWindow(controlHandle, newLeft, newTop, newControlWidth, newControlHeight, true);
}
}
}
return CallNextHookEx(_hookHandle, nCode, wParam, lParam);
}
[DllExport("FinalizeAutoScaling", CallingConvention = CallingConvention.StdCall)]
public static void FinalizeAutoScaling()
{
if (_hookHandle != IntPtr.Zero)
{
UnhookWindowsHookEx(_hookHandle);
_hookHandle = IntPtr.Zero;
}
_controls.Clear();
}
}
}
下面是易例子源码
窗口程序集名 | 保 留 | 保 留 | 备 注 | 窗口程序集_启动窗口 | | | | 调试输出 (初始化自动缩放 (取窗口句柄 ()) )添加自动缩放组件 (编辑框1. 取窗口句柄 ()) 添加自动缩放组件 (按钮1. 取窗口句柄 ()) 添加自动缩放组件 (图片框1. 取窗口句柄 ()) 释放自动缩放 ()
|
评分
-
查看全部评分
|