Rank: Advanced Member
Groups: Moderators, Approved Joined: 4/23/2020(UTC) Posts: 161 Thanks: 46 times Was thanked: 23 time(s) in 17 post(s)
|
It will popup a Showbox near the mouse to preview the selected picture like PNG, JPG, BMP, GIF on the Explorer or Desktop. There is no border, and you can drag to move, scroll to zoom in/out, and it will auto close when you click the left button at the blank area or double left click the image Showbox. Then auto previews the next picture if you select another one. There are two versions. The first Updated one keeps the focus on the Explorer of the selected picture, and you can copy, cut or rename it for free, also it will show the Max Width 450 of the picture in the Showbox and within the screen. The second Old one uses the LostFocus method to the Showbox to close it, and there are some inconveniences. Update: 1. Fix the Showbox will not auto close when without selecting on the Desktop. 2. Fix drag the Showbox will auto close. Put one of the two versions code into Global Actions-Load/Unload1. Updated
Code: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");
// Define PInvoke method for GetDpiForWindow (Windows 10 1607 or greater)
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);
//ImageWithMenu.Show(System.Drawing.Image.FromStream(new clr.System.IO.MemoryStream(fileBytes)), currentMouseLocation, 1);
}
} 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();
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 = img.Width;
var foreHeight = img.Height;
var afterHeight = parseInt(img.Height*450/img.Width);
var form = new Forms.Form;
form.StartPosition = Forms.FormStartPosition.Manual;
form.FormBorderStyle = (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);
//No Borders
form.BackColor = Color.LimeGreen;
form.TransparencyKey = Color.LimeGreen;
var pic = new Forms.PictureBox;
pic.Name = 'pic';
pic.SizeMode = Forms.PictureBoxSizeMode.Zoom;
if(img.Width > 450) {
pic.Size = new Size(450, parseInt(img.Height*450/img.Width));
} else {
pic.Size = new Size(img.Width, img.Height);
}
pic.BackColor = Color.Transparent;
if (border != 0) { pic.Location = new Point(border, border); }
pic.Image = img;
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();
});
var form_doubleClick =
form.DoubleClick.connect(
function (sender, args) {
sp.StoreBool('shown', false);
sp.DeleteStoredHandle('formHWD');
form.Close();
});
var form_lostFocus =
form.LostFocus.connect(
function (sender, args) {
//form.Close();
//sp.StoreBool('shown', false);
});
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];
}
`
);
2. Old
Code: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;
if(wndHandle.ToInt32() == desktopHandle.ToInt32() || sp.LastFocusControl().HWnd.ToInt32() == desktopHandle.ToInt32()) {
//Desktop
files = sp.GetSelectedFilesOnDesktop();
isDesktop = true;
} else {
//Not Desktop
files = sp.GetSelectedFilesInExplorer(wndHandle);
}
if(files.Length == 1 && (files[0].indexOf("png") > -1 || files[0].indexOf("PNG") > -1 ||
files[0].indexOf("jpg") > -1 || files[0].indexOf("JPG") > -1 ||
files[0].indexOf("jpeg") > -1 || files[0].indexOf("JPEG") > -1 ||
files[0].indexOf("bmp") > -1 || files[0].indexOf("BMP") > -1 ||
files[0].indexOf("gif") > -1 || files[0].indexOf("GIF") > -1)) {
fullPath = files[0];
if(isDesktop) {
fullPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\\\" + fullPath;
}
if(!sp.GetStoredBool('shown')) {
ShowImage(fullPath, currentMouseLocation, 1);
}
}
function ShowImage(img, pt, border) {
if (img == null) return;
var fs = new clr.System.IO.FileStream(img, clr.System.IO.FileMode.Open, clr.System.IO.FileAccess.ReadWrite, clr.System.IO.FileShare.None);
var fileBytes = host.newArr(System.Byte, fs.Length);
fs.Read(fileBytes, 0, fs.Length);
fs.Close();
var Forms = forms.System.Windows.Forms;
if (border == null) border = 0;
if (border != 0) { pt = new Point(pt.X - border, pt.Y - border); }
var form = new Forms.Form;
form.StartPosition = Forms.FormStartPosition.Manual;
form.FormBorderStyle = (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);
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;
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 = drawing.System.Drawing.SystemColors.Control;
pic.BackColor = Color.Transparent;
if (border != 0) { pic.Location = new Point(border, border); }
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;
form.Activate();
sp.StoreBool('shown', true);
});
var pic_doubleClick =
pic.DoubleClick.connect(
function (sender, args) {
form.Close();
sp.StoreBool('shown', false);
});
var form_lostFocus =
form.LostFocus.connect(
function (sender, args) {
form.Close();
sp.StoreBool('shown', false);
});
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);
}
});
return [pic_mouseMove];
}
`
);
Edited by user Sunday, March 6, 2022 7:34:57 AM(UTC)
| Reason: Not specified
|