选中图片预览 v1.2
更新:
-
20220211 v0.2
1. 图片宽度大于450,显示图片宽度为450,图片宽度小于450,显示原尺寸,高度不限制
2. 显示图片位置在屏幕内
-
20220217 v1.0
3. 解决预览显示后,资源管理器失焦的问题
4. 解决文件夹点空白处不显示一次最后选中的图片问题
5. 解决无法重命名(F2)、复制、剪切的问题
-
20220219 v1.1
6. 修复桌面显示图片预览后,只能双击预览关闭
-
20220322 v1.2
7. 更新双击关闭/预览关闭后,清除S+内存(见下方加粗提示)
资源管理器、桌面选中图片后,直接在鼠标处显示预览,点击空白处关闭预览,和双击左键关闭(用于切换其他窗口,预览未关闭情况),鼠标拖动,滚动放大缩小,选中下一张图片继续预览
Macbook Air测试,每800毫秒执行一次,如打开预览卡顿,可以设置高
存在小问题:
1. 直接双击预览关闭,会再次显示
2. 图片宽度过大(比如大于5000),预览关闭和选中下一张图片会卡,尝试从缓存中获取图片重绘设置大小,效果不明显
补充添加38# 代码到全局动作-启动/退出-启动
添加到全局动作-启动/退出-启动(开启)
if(!NativeModules.User32)
{
var IntPtrT = host.typeOf(clr.System.IntPtr);
var Int32T = host.typeOf(clr.System.Int32);
var BooleanT = host.typeOf(clr.System.Boolean);
var user32TB = sp.NativeModule().DefineType("User32", "Class,Public,SequentialLayout,Serializable");
user32TB.DefinePInvokeMethod("ShowWindow",
"user32.dll",
[IntPtrT,Int32T],
BooleanT,
"PreserveSig");
user32TB.DefinePInvokeMethod("SetWindowPos",
"user32.dll",
[IntPtrT,Int32T,Int32T,Int32T,Int32T,Int32T,Int32T],
BooleanT,
"PreserveSig");
user32TB.Create();
}
sp.CreateTimer("imagepreview",
0,
800,
`var currentMouseLocation = sp.GetCurrentMousePoint();
var wndHandle = sp.ForegroundWindow().HWnd;
var desktopHandle = sp.DesktopWindowListView().HWnd;
var fullPath = "";
var files;
var isDesktop = false;
var mouseScreen = Screen.FromPoint(currentMouseLocation);
var oldFormHWD = sp.GetStoredHandle('formHWD');
var oldSelFileName = sp.GetStoredString('selFileName');
if(wndHandle.ToInt32() == desktopHandle.ToInt32() || sp.LastFocusControl().HWnd.ToInt32() == desktopHandle.ToInt32() ||
sp.ForegroundWindow().ClassName == 'WorkerW' || sp.ForegroundWindow().ClassName == 'Progman') {
//Desktop
files = sp.GetSelectedFilesOnDesktop();
isDesktop = true;
} else {
//Not Desktop
files = sp.GetSelectedFilesInExplorer(wndHandle);
}
if(files.Length == 1 && (files[0].endsWith("png") || files[0].endsWith("PNG") ||
files[0].endsWith("jpg") || files[0].endsWith("JPG") ||
files[0].endsWith("jpeg") || files[0].endsWith("JPEG") ||
files[0].endsWith("bmp") || files[0].endsWith("BMP") ||
files[0].endsWith("ico") || files[0].endsWith("ICO") ||
files[0].endsWith("gif") || files[0].endsWith("GIF"))) {
fullPath = files[0];
if(isDesktop) {
fullPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\\\" + fullPath;
}
var fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(fullPath);
sp.StoreString('selFileName', fileNameNoExt);
if(sp.GetStoredString('selFileName') != oldSelFileName) {
sp.StoreBool('shown', false);
}
if(!sp.GetStoredBool('shown')) {
//ShowImage(fullPath, currentMouseLocation, 1);
var fs = new clr.System.IO.FileStream(fullPath, clr.System.IO.FileMode.Open, clr.System.IO.FileAccess.Read, clr.System.IO.FileShare.ReadWrite);
var fileBytes = host.newArr(System.Byte, fs.Length);
fs.Read(fileBytes, 0, fs.Length);
fs.Close();
ShowImage(System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)), currentMouseLocation, 1);
//sp.DisplayImage(System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)), true);
}
} else if((files.Length == 0 || files.Length > 1) && sp.ForegroundWindow().ClassName != 'WindowsForms10.Window.8.app.0.13965fa_r6_ad1') {
sp.WindowFromHandle(sp.GetStoredHandle('formHWD')).SendClose();
FlushMemory();
sp.StoreBool('shown', false);
sp.DeleteStoredHandle('formHWD');
}
function ShowImage(img, pt, border) {
if (img == null) return;
sp.WindowFromHandle(oldFormHWD).SendClose();
var Forms = forms.System.Windows.Forms;
if (border == null) border = 0;
if (border != 0) { pt = new Point(pt.X - border, pt.Y - border); }
var foreWidth = System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)).Width;
var foreHeight = System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)).Height;
var afterHeight = parseInt(System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)).Height*420/System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)).Width);
var form = new Forms.Form;
form.StartPosition = Forms.FormStartPosition.Manual;
form.FormBorderStyle = (border != 0) ? //border != 0顶部白框
Forms.FormBorderStyle.None :
Forms.FormBorderStyle.SizableToolWindow ;
form.TopMost = true;
form.ControlBox = false;
form.ShowInTaskbar = false;
form.Margin = new Forms.Padding(0);
if (border != 0) { form.Padding = new Forms.Padding(border); }
form.MinimumSize = new Size(10, 10);
form.Size = new Size(10, 10);
if(foreWidth > 450) {
if(currentMouseLocation.X < mouseScreen.Bounds.Right - 1 - 450 && currentMouseLocation.Y > mouseScreen.Bounds.Bottom - 1 - afterHeight) {
form.Location = new Point(currentMouseLocation.X, mouseScreen.Bounds.Bottom - 41 - afterHeight);
} else if(currentMouseLocation.X > mouseScreen.Bounds.Right - 1 - 450 && currentMouseLocation.Y < mouseScreen.Bounds.Bottom - 1 - afterHeight){
form.Location = new Point(mouseScreen.Bounds.Right - 1 - 450, currentMouseLocation.Y);
} else if(currentMouseLocation.X > mouseScreen.Bounds.Right - 1 - 450 && currentMouseLocation.Y > mouseScreen.Bounds.Bottom - 1 - afterHeight){
form.Location = new Point(mouseScreen.Bounds.Right - 1 - 450, mouseScreen.Bounds.Bottom - 41 - afterHeight);
} else {
form.Location = pt;
}
} else {
if(currentMouseLocation.X < mouseScreen.Bounds.Right - 1 - foreWidth && currentMouseLocation.Y > mouseScreen.Bounds.Bottom - 1 - foreHeight) {
form.Location = new Point(currentMouseLocation.X, mouseScreen.Bounds.Bottom - 41 - foreHeight);
} else if(currentMouseLocation.X > mouseScreen.Bounds.Right - 1 - foreWidth && currentMouseLocation.Y < mouseScreen.Bounds.Bottom - 1 - foreHeight) {
form.Location = new Point(mouseScreen.Bounds.Right - 1 - foreWidth, currentMouseLocation.Y);
} else if(currentMouseLocation.X > mouseScreen.Bounds.Right - 1 - foreWidth && currentMouseLocation.Y > mouseScreen.Bounds.Bottom - 1 - foreHeight) {
form.Location = new Point(mouseScreen.Bounds.Right - 1 - foreWidth, mouseScreen.Bounds.Bottom - 41 - foreHeight);
} else {
form.Location = pt;
}
}
form.AutoSize = true;
form.GetType().GetProperty("DoubleBuffered",
host.flags(clr.System.Reflection.BindingFlags.NonPublic,
clr.System.Reflection.BindingFlags.Instance))
.SetValue(form, true);
//透明边框
form.BackColor = Color.LimeGreen;
form.TransparencyKey = Color.LimeGreen;
var pic = new Forms.PictureBox;
pic.Name = 'pic';
pic.SizeMode = Forms.PictureBoxSizeMode.Zoom;
if(System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)).Width > 450) {
pic.Size = new Size(450, parseInt(System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)).Height*420/System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)).Width));
} else {
pic.Size = new Size(System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)).Width, System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)).Height);
}
pic.BackColor = Color.Transparent;//背景透明
if (border != 0) { pic.Location = new Point(border, border); }
pic.Image = img;
//pic.Image = System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes));
form.Controls.Add(pic);
var autoSize = form.Size;
var t = (form.ClientSize.Width - pic.Width - form.Padding.Horizontal);
autoSize.Width -= t;
autoSize.Height -= t;
form.AutoSize = false;
form.Size = autoSize;
pic.Dock = Forms.DockStyle.Fill;
var form_shown =
form.Shown.connect(
function (sender, args) {
//form.TopMost = true;
var HWND_TOPMOST = -1;
var SWP_NOACTIVATE = 0x0010;
var SW_SHOWNOACTIVATE = 4;
NativeModules.User32.ShowWindow(form.Handle, SW_SHOWNOACTIVATE);
NativeModules.User32.SetWindowPos(form.Handle, HWND_TOPMOST,form.Left, form.Top, form.Width, form.Height,SWP_NOACTIVATE);
form.TopMost = false;
//form.Activate();
sp.StoreBool('shown', true);
sp.StoreHandle('formHWD', form.Handle);
});
var pic_doubleClick =
pic.DoubleClick.connect(
function (sender, args) {
sp.StoreBool('shown', false);
sp.DeleteStoredHandle('formHWD');
form.Close();
FlushMemory();
});
var form_doubleClick =
form.DoubleClick.connect(
function (sender, args) {
sp.StoreBool('shown', false);
sp.DeleteStoredHandle('formHWD');
form.Close();
FlushMemory();
});
dragMoveBlind = ShowImageBindDragMove(form, pic);
wheelResizeBlind = ShowImageBindWheelResize(form, pic);
Forms.Application.Run(form);
}
function ShowImageBindWheelResize(form, pic) {
var form_mouseWheel =
form.MouseWheel.connect(
function (sender, e) {
var factor = e.Delta / 1000;
var ampt = forms.System.Windows.Forms.Control.MousePosition;
var rmpt = new Point();
rmpt.X = ampt.X - form.Left;
rmpt.Y = ampt.Y - form.Top;
var intFunc = Math.floor;
rmpt.X += intFunc(rmpt.X * factor);
rmpt.Y += intFunc(rmpt.Y * factor);
form.SetBounds(ampt.X - rmpt.X, ampt.Y - rmpt.Y,
form.Width + intFunc(form.Width * factor),
form.Height + intFunc(form.Height * factor));
});
return [form_mouseWheel];
}
function ShowImageBindDragMove(form, pic) {
var Cursors = forms.System.Windows.Forms.Cursors;
var ReleaseCapture = sp_config.GetType().Assembly.GetType("API").GetMethod("ReleaseCapture");
var pic_mouseMove =
pic.MouseMove.connect(
function (sender, e) {
var hitResize = form.Height + form.Width - (e.X + e.Y) < 30;
var cursor = hitResize ? Cursors.SizeNWSE : Cursors.Default;
if (pic.Cursor != cursor) pic.Cursor = cursor;
if (e.Button == MouseButtons.Left) {
ReleaseCapture.Invoke(null, null);
sp.WindowFromHandle(form.Handle).SendMessageObj(0x0112,
hitResize ? 0xF008 : 0xF012, 0);
}
form.TopMost = true;
});
return [pic_mouseMove];
}
function FlushMemory()
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
if (System.Environment.OSVersion.Platform == System.PlatformID.Win32NT)
NativeModules.Kernel32.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
`
);