精易论坛

标题: 自动缩放界面组件 [打印本页]

作者: zxxiaopi    时间: 2024-9-27 19:06
标题: 自动缩放界面组件
本帖最后由 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.取窗口句柄 ())
子程序名返回值类型公开备 注
__启动窗口_可否被关闭逻辑型 
释放自动缩放 ()


i支持库列表   支持库注释   
spec特殊功能支持库
  
DLL命令名返回值类型公开备 注
初始化自动缩放逻辑型 
DLL库文件名:
AutoScalingDLL.dll
在DLL库中对应命令名:
InitializeAutoScaling
参数名类 型传址数组备 注
hWnd整数型
DLL命令名返回值类型公开备 注
添加自动缩放组件逻辑型 
DLL库文件名:
AutoScalingDLL.dll
在DLL库中对应命令名:
AddControlToAutoScale
参数名类 型传址数组备 注
hWnd整数型
DLL命令名返回值类型公开备 注
释放自动缩放 
DLL库文件名:
AutoScalingDLL.dll
在DLL库中对应命令名:
FinalizeAutoScaling
参数名类 型传址数组备 注




AutoScalingDLL.rar

3.28 KB, 下载次数: 42, 下载积分: 精币 -2 枚

编译好的x86的dll


作者: 文西哥    时间: 2024-9-27 19:08
谢谢分享 值得学习
作者: tian89    时间: 2024-9-27 19:48
我觉得没有必要用其他语言的代码或插件,你说的功能我没有作到过,不过仔细一想也很容易吧,使用API函数枚举窗口上所有控件在结合窗口变化来控制控件的大小位置这个控制是已经枚举到所有窗口控件ID利用API发送窗口信息调整控件这个只是初步想法
作者: 神一样的人猫腻    时间: 2024-9-27 19:57
好东西感谢分享
作者: pipicool    时间: 2024-9-27 20:40
学习一下
作者: 艾玛克138    时间: 2024-9-27 21:58
好好学习,努力向上
作者: po1718    时间: 2024-9-27 23:33

好好学习,努力向上
作者: wgqxj    时间: 2024-9-28 00:57
谢谢分享
作者: renhe2018    时间: 2024-9-28 06:45
感谢分享,给力啊。
作者: 查过    时间: 2024-9-28 07:11
感谢分享,很给力!~
作者: 豆豆灰常开心    时间: 2024-9-28 07:15
感谢您对论坛的支持!
作者: ctry78985    时间: 2024-9-28 07:20
感谢分享
作者: 杨明煜    时间: 2024-9-28 07:50
真值得学习,感谢!......
作者: hhf4977    时间: 2024-9-28 08:27
感谢分享
作者: 五花牛    时间: 2024-9-28 08:30
真值得学习,感谢!......
作者: 一指温柔    时间: 2024-9-28 08:48
感谢分享
作者: wuqingg    时间: 2024-9-28 09:40

作者: 美味萝卜    时间: 2024-9-28 09:44
  感谢分享,很给力!~
作者: wuqingg    时间: 2024-9-28 10:08
如果窗口组件在分组框里面启动之后窗口组件就会消失

录制_2024_09_28_10_05_34_706.gif (518.1 KB, 下载次数: 2)

录制_2024_09_28_10_05_34_706.gif

作者: wjswzj0    时间: 2024-9-28 11:15
  感谢分享,很给力!~
作者: kantal    时间: 2024-9-28 13:19
感谢发布原创作品,精易因你更精彩!
作者: 我的yyy123    时间: 2024-9-28 14:18
不错不错,学习学习
作者: jtucar    时间: 2024-9-28 15:06
互相讨论,共同进步
作者: wh1234567    时间: 2024-9-28 17:21
感谢分享原创作品。。
作者: 憨憨问号    时间: 2024-9-28 18:01
高级啊,学习学习
作者: year1970    时间: 2024-9-28 18:33
感谢分享
作者: a623539929    时间: 2024-9-28 19:44

感谢分享原创作品。。
作者: 查过    时间: 2024-9-29 07:23
感谢分享,很给力!~
作者: 豆豆灰常开心    时间: 2024-9-29 07:27
感谢发布原创作品,精易因你更精彩!6666666666666
作者: jinxiu9527    时间: 2024-9-29 10:35
作为一个萌新虽然看不懂,但是还是要多支持!
作者: kyo9766    时间: 2024-9-29 13:23

互相讨论,共同进步,感谢分享
作者: spawing    时间: 2024-9-30 17:37

感谢分享,很给力!~
作者: gudujian420    时间: 2024-10-3 11:23
先收藏一下
作者: 胖子葛格    时间: 2024-10-7 18:30
感谢大神分享
作者: ghostw    时间: 2024-10-30 10:18
感谢分享,需要.net环境就不是太好.用Delphi搞一个呢..
作者: 熊不熊    时间: 2024-12-4 10:56
感谢分享,很给力!~




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4