/
frmMain.cs
4105 lines (3283 loc) Β· 145 KB
/
frmMain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
ο»Ώ/*
ImageGlass Project - Image viewer for Windows
Copyright (C) 2018 DUONG DIEU PHAP
Project homepage: https://imageglass.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using ImageGlass.Core;
using ImageGlass.Library.Image;
using ImageGlass.Library.Comparer;
using System.IO;
using System.Diagnostics;
using ImageGlass.Services.Configuration;
using ImageGlass.Library;
using System.Collections.Specialized;
using ImageGlass.Services.InstanceManagement;
using System.Drawing.Imaging;
using ImageGlass.Theme;
using System.Threading.Tasks;
using ImageGlass.Library.WinAPI;
using System.Collections.Concurrent;
using FileWatcherEx;
namespace ImageGlass
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
//Get DPI Scaling ratio
//NOTE: the this.DeviceDpi property is not accurate
DPIScaling.CurrentDPI = DPIScaling.GetSystemDpi();
//Remove white line under tool strip
toolMain.Renderer = new Theme.ToolStripRenderer();
//Load UI Configs
LoadConfig(isLoadUI: true, isLoadOthers: false);
Application.DoEvents();
//Update form with new DPI
OnDpiChanged();
}
#region Local variables
// window size value before resizing
private Size _windowSize = new Size(1000, 800);
// determine if the image is zoomed
private bool _isManuallyZoomed = false;
// determine if toolbar is shown (fullscreen / slideshow)
private bool _isShowToolbar = true;
// determine if thumbnail is shown (fullscreen / slideshow)
private bool _isShowThumbnail = true;
// determine if Windows key is pressed
private bool _isWindowsKeyPressed = false;
// determine if user is dragging an image file
private bool _isDraggingImage = false;
/***********************************
* Variables for FileWatcherEx
***********************************/
// the list of local deleted files, need to be deleted in the memory list
private List<string> _queueListForDeleting = new List<string>();
// File system watcher
private FileWatcherEx.FileWatcherEx _fileWatcher = new FileWatcherEx.FileWatcherEx();
#endregion
#region Drag - drop
private void picMain_DragOver(object sender, DragEventArgs e)
{
string filePath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
// Drag file from DESKTOP to APP
if (GlobalSetting.ImageList.IndexOf(filePath) == -1)
{
e.Effect = DragDropEffects.Move;
}
// Drag file from APP to DESKTOP
else
{
e.Effect = DragDropEffects.Copy;
}
}
private void picMain_DragDrop(object sender, DragEventArgs e)
{
// Drag file from DESKTOP to APP
string filePath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
if (Path.GetExtension(filePath).ToLower() == ".lnk")
filePath = Shortcuts.FolderFromShortcut(filePath);
int imageIndex = GlobalSetting.ImageList.IndexOf(filePath);
// The file is located another folder, load the entire folder
if (imageIndex == -1)
{
Prepare(filePath);
}
// The file is in current folder AND it is the viewing image
else if (GlobalSetting.CurrentIndex == imageIndex)
{
//do nothing
}
// The file is in current folder AND it is NOT the viewing image
else
{
GlobalSetting.CurrentIndex = imageIndex;
NextPic(0);
}
}
private void picMain_MouseDown(object sender, MouseEventArgs e)
{
if (_isDraggingImage)
{
string[] paths = new string[1];
paths[0] = GlobalSetting.ImageList.GetFileName(GlobalSetting.CurrentIndex);
var data = new DataObject(DataFormats.FileDrop, paths);
picMain.DoDragDrop(data, DragDropEffects.Copy);
_isDraggingImage = false;
}
}
#endregion
#region Preparing image
/// <summary>
/// Open an image
/// </summary>
private void OpenFile()
{
OpenFileDialog o = new OpenFileDialog();
o.Filter = GlobalSetting.LangPack.Items["frmMain._OpenFileDialog"] + "|" +
GlobalSetting.AllImageFormats;
if (o.ShowDialog() == DialogResult.OK && File.Exists(o.FileName))
{
Prepare(o.FileName);
}
o.Dispose();
}
/// <summary>
/// Prepare to load image
/// </summary>
/// <param name="path">Path</param>
public void Prepare(string path)
{
if (File.Exists(path) == false && Directory.Exists(path) == false)
return;
//Reset current index
GlobalSetting.CurrentIndex = 0;
string filePath = "";
string dirPath = "";
//Check path is file or directory?
if (File.Exists(path))
{
filePath = path;
// get directory
//dirPath = (Path.GetDirectoryName(path) + "\\").Replace("\\\\", "\\");
//dirPath = path.Substring(0, path.LastIndexOf("\\") + 1);
dirPath = Path.GetDirectoryName(path);
}
else if (Directory.Exists(path))
{
dirPath = path; // (path + "\\").Replace("\\\\", "\\");
}
//Get supported image extensions from directory
var _imageFilenameList = LoadImageFilesFromDirectory(dirPath);
//Dispose all garbage
GlobalSetting.ImageList.Dispose();
//Set filename to image list
GlobalSetting.ImageList = new ImgMan(_imageFilenameList.ToArray());
//Track image loading progress
GlobalSetting.ImageList.OnFinishLoadingImage += ImageList_OnFinishLoadingImage;
//Find the index of current image
if (filePath.Length > 0)
{
GlobalSetting.CurrentIndex = GlobalSetting.ImageList.IndexOf(filePath);
}
else
{
GlobalSetting.CurrentIndex = 0;
}
//Load thumnbnail
LoadThumbnails();
//Cannot find the index
if (GlobalSetting.CurrentIndex == -1)
{
//Mark as Image Error
GlobalSetting.IsImageError = true;
this.Text = $"ImageGlass - {Path.GetFileName(filePath)} - {ImageInfo.GetFileSize(filePath)}";
picMain.Text = GlobalSetting.LangPack.Items["frmMain.picMain._ErrorText"];
picMain.Image = null;
//Exit function
return;
}
//Start loading image
NextPic(0);
//Watch all changes of current path
this._fileWatcher.Stop();
this._fileWatcher = new FileWatcherEx.FileWatcherEx()
{
FolderPath = dirPath,
IncludeSubdirectories = GlobalSetting.IsRecursiveLoading,
// auto Invoke the form if required, no need to invidiually invoke in each event
SynchronizingObject = this
};
this._fileWatcher.OnCreated += FileWatcher_OnCreated;
this._fileWatcher.OnDeleted += FileWatcher_OnDeleted;
this._fileWatcher.OnChanged += FileWatcher_OnChanged;
this._fileWatcher.OnRenamed += FileWatcher_OnRenamed;
this._fileWatcher.Start();
}
private void ImageList_OnFinishLoadingImage(object sender, EventArgs e)
{
//clear text when finishing
DisplayTextMessage("", 0);
}
/// <summary>
/// Select current thumbnail
/// </summary>
private void SelectCurrentThumbnail()
{
if (thumbnailBar.Items.Count > 0)
{
thumbnailBar.ClearSelection();
try
{
thumbnailBar.Items[GlobalSetting.CurrentIndex].Selected = true;
thumbnailBar.Items[GlobalSetting.CurrentIndex].Focused = true;
thumbnailBar.EnsureVisible(GlobalSetting.CurrentIndex);
}
catch (Exception) { }
}
}
/// <summary>
/// Sort and find all supported image from directory
/// </summary>
/// <param name="path">Image folder path</param>
private List<string> LoadImageFilesFromDirectory(string path)
{
//Load image order from config
GlobalSetting.LoadImageOrderConfig();
//Get files from dir
var fileList = DirectoryFinder.FindFiles(path,
GlobalSetting.IsRecursiveLoading,
new Predicate<FileInfo>(delegate (FileInfo fi)
{
// KBR 20180607 Rework predicate to use a FileInfo instead of the filename.
// By doing so, can use the attribute data already loaded into memory,
// instead of fetching it again (via File.GetAttributes). A re-fetch is
// very slow across network paths. For me, improves image load from 4+
// seconds to 0.4 seconds for a specific network path.
if (fi.FullName == null) // KBR not sure why but occasionally getting null filename
return false;
string extension = fi.Extension ?? "";
extension = extension.ToLower(); // Path.GetExtension(f).ToLower() ?? ""; //remove blank extension
// checks if image is hidden and ignores it if so
if (GlobalSetting.IsShowingHiddenImages == false)
{
var attributes = fi.Attributes; // File.GetAttributes(f);
var isHidden = attributes.HasFlag(FileAttributes.Hidden);
if (isHidden)
{
return false;
}
}
if (extension.Length > 0 && GlobalSetting.ImageFormatHashSet.Contains(extension))
{
return true;
}
return false;
}));
var list = SortImageList(fileList);
return list;
}
private List<string> SortImageList(ConcurrentBag<string> fileList)
{
var list = new List<string>();
//Sort image file
if (GlobalSetting.ImageLoadingOrder == ImageOrderBy.Name)
{
var arr = fileList.ToArray();
Array.Sort(arr, new WindowsNaturalSort());
list.AddRange(arr);
//list.AddRange(FileLogicalComparer.Sort(dsFile.ToArray()));
}
else if (GlobalSetting.ImageLoadingOrder == ImageOrderBy.Length)
{
list.AddRange(fileList
.OrderBy(f => new FileInfo(f).Length));
}
else if (GlobalSetting.ImageLoadingOrder == ImageOrderBy.CreationTime)
{
list.AddRange(fileList
.OrderBy(f => new FileInfo(f).CreationTimeUtc));
}
else if (GlobalSetting.ImageLoadingOrder == ImageOrderBy.Extension)
{
list.AddRange(fileList
.OrderBy(f => new FileInfo(f).Extension));
}
else if (GlobalSetting.ImageLoadingOrder == ImageOrderBy.LastAccessTime)
{
list.AddRange(fileList
.OrderBy(f => new FileInfo(f).LastAccessTime));
}
else if (GlobalSetting.ImageLoadingOrder == ImageOrderBy.LastWriteTime)
{
list.AddRange(fileList
.OrderBy(f => new FileInfo(f).LastWriteTime));
}
else if (GlobalSetting.ImageLoadingOrder == ImageOrderBy.Random)
{
list.AddRange(fileList
.OrderBy(f => Guid.NewGuid()));
}
return list;
}
/// <summary>
/// Clear and reload all thumbnail image
/// </summary>
private void LoadThumbnails()
{
thumbnailBar.SuspendLayout();
thumbnailBar.Items.Clear();
thumbnailBar.ThumbnailSize = new Size(GlobalSetting.ThumbnailDimension, GlobalSetting.ThumbnailDimension);
for (int i = 0; i < GlobalSetting.ImageList.Length; i++)
{
ImageListView.ImageListViewItem lvi = new ImageListView.ImageListViewItem(GlobalSetting.ImageList.GetFileName(i));
lvi.Tag = GlobalSetting.ImageList.GetFileName(i);
thumbnailBar.Items.Add(lvi);
}
thumbnailBar.ResumeLayout();
}
/// <summary>
/// Change image
/// </summary>
/// <param name="step">Image step to change. Zero is reload the current image.</param>
private void NextPic(int step)
{
NextPic(step, false);
}
/// <summary>
/// Change image
/// </summary>
/// <param name="step">Image step to change. Zero is reload the current image.</param>
/// <param name="configs">Configuration for the next load</param>
/// <param name="isSkipCache"></param>
private void NextPic(int step, bool isKeepZoomRatio, bool isSkipCache = false)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<int, bool, bool>(NextPic), step, isKeepZoomRatio, isSkipCache);
return;
}
if (picMain.IsAnimating)
{
picMain.StopAnimating();
}
//Save previous image if it was modified
if (File.Exists(LocalSetting.ImageModifiedPath) && GlobalSetting.IsSaveAfterRotating)
{
DisplayTextMessage(GlobalSetting.LangPack.Items["frmMain._SaveChanges"], 2000);
Application.DoEvents();
ImageSaveChange();
//remove the old image data from cache
GlobalSetting.ImageList.Unload(GlobalSetting.CurrentIndex);
//update thumbnail
thumbnailBar.Items[GlobalSetting.CurrentIndex].Update();
}
Application.DoEvents();
picMain.Text = "";
LocalSetting.IsTempMemoryData = false;
if (GlobalSetting.ImageList.Length < 1)
{
Text = $"ImageGlass";
GlobalSetting.IsImageError = true;
picMain.Image = null;
LocalSetting.ImageModifiedPath = "";
return;
}
//temp index
int tempIndex = GlobalSetting.CurrentIndex + step;
if (!GlobalSetting.IsPlaySlideShow && !GlobalSetting.IsLoopBackViewer)
{
//Reach end of list
if (tempIndex >= GlobalSetting.ImageList.Length)
{
DisplayTextMessage(GlobalSetting.LangPack.Items["frmMain._LastItemOfList"], 1000);
return;
}
//Reach the first item of list
if (tempIndex < 0)
{
DisplayTextMessage(GlobalSetting.LangPack.Items["frmMain._FirstItemOfList"], 1000);
return;
}
}
//Check if current index is greater than upper limit
if (tempIndex >= GlobalSetting.ImageList.Length)
tempIndex = 0;
//Check if current index is less than lower limit
if (tempIndex < 0)
tempIndex = GlobalSetting.ImageList.Length - 1;
//Update current index
GlobalSetting.CurrentIndex = tempIndex;
//The image data will load
Image im = null;
try
{
//Read image data
im = GlobalSetting.ImageList.GetImage(GlobalSetting.CurrentIndex, isSkipCache);
GlobalSetting.IsImageError = GlobalSetting.ImageList.IsErrorImage;
//Show image
picMain.Image = im;
//Reset the zoom mode if isKeepZoomRatio = FALSE
if (!isKeepZoomRatio)
{
//reset zoom mode
ApplyZoomMode(GlobalSetting.ZoomMode);
}
//Run in another thread
Parallel.Invoke(() =>
{
//Release unused images
if (GlobalSetting.CurrentIndex - 2 >= 0)
{
GlobalSetting.ImageList.Unload(GlobalSetting.CurrentIndex - 2);
}
if (!GlobalSetting.IsImageBoosterBack && GlobalSetting.CurrentIndex - 1 >= 0)
{
GlobalSetting.ImageList.Unload(GlobalSetting.CurrentIndex - 1);
}
});
}
catch
{
picMain.Image = null;
LocalSetting.ImageModifiedPath = "";
Application.DoEvents();
if (!File.Exists(GlobalSetting.ImageList.GetFileName(GlobalSetting.CurrentIndex)))
{
GlobalSetting.ImageList.Unload(GlobalSetting.CurrentIndex);
}
}
if (GlobalSetting.IsImageError)
{
picMain.Text = GlobalSetting.LangPack.Items["frmMain.picMain._ErrorText"];
picMain.Image = null;
LocalSetting.ImageModifiedPath = "";
}
_isDraggingImage = false;
//Select thumbnail item
SelectCurrentThumbnail();
//Collect system garbage
GC.Collect();
GC.WaitForPendingFinalizers();
}
/// <summary>
/// Update image information on status bar
/// </summary>
private void UpdateStatusBar()
{
string appName = "ImageGlass";
string indexTotal = string.Empty;
string filename = string.Empty;
string zoom = string.Empty;
string imgSize = string.Empty;
string fileSize = string.Empty;
string fileDate = string.Empty;
if (LocalSetting.IsTempMemoryData)
{
appName += $" | {GlobalSetting.LangPack.Items["frmMain._ImageData"]}";
zoom = $"{picMain.Zoom.ToString()}%";
if (picMain.Image != null)
{
try
{
imgSize = $"{picMain.Image.Width} x {picMain.Image.Height} px";
}
catch { }
//ImageGlass (Image data) | {zoom} | {image size}
this.Text = $"{appName} | {zoom} | {imgSize}";
}
else
{
this.Text = $"{appName} | {zoom}";
}
}
else
{
if (GlobalSetting.ImageList.Length < 1)
{
this.Text = appName;
return;
}
string currFilePath = GlobalSetting.ImageList.GetFileName(GlobalSetting.CurrentIndex);
// when there is a problem with a file, don't try to show some info
bool moredata = File.Exists(currFilePath);
indexTotal = $"{(GlobalSetting.CurrentIndex + 1)}/{GlobalSetting.ImageList.Length} {GlobalSetting.LangPack.Items["frmMain._Text"]}";
if (moredata)
{
fileSize = ImageInfo.GetFileSize(currFilePath);
fileDate = File.GetCreationTime(currFilePath).ToString("yyyy/MM/dd HH:mm:ss");
}
if (GlobalSetting.IsDisplayBasenameOfImage)
{
filename = Path.GetFileName(GlobalSetting.ImageList.GetFileName(GlobalSetting.CurrentIndex));
}
else
{
//auto ellipsis the filename
//the minimum text to show is Drive letter + basename.
//ex: C:\...\example.jpg
filename = GlobalSetting.ImageList.GetFileName(GlobalSetting.CurrentIndex);
var basename = Path.GetFileName(filename);
var charWidth = this.CreateGraphics().MeasureString("A", this.Font).Width;
var textMaxLength = (this.Width - DPIScaling.TransformNumber(400)) / charWidth;
var maxLength = (int)Math.Max(basename.Length + 8, textMaxLength);
filename = Helper.ShortenPath(filename, maxLength);
}
if (GlobalSetting.IsImageError)
{
//ImageGlass - {index/total} - {filename} | {file size} | {file date}
if (!moredata) // size and date not available
this.Text = $"{appName} - {indexTotal} | {filename}";
else
this.Text = $"{appName} - {indexTotal} | {filename} | {fileSize} | {fileDate}";
}
else
{
zoom = $"{picMain.Zoom.ToString("F2")}%";
if (picMain.Image != null)
{
try
{
imgSize = $"{picMain.Image.Width} x {picMain.Image.Height} px";
}
catch { }
//ImageGlass - {index/total} - {filename} | {zoom} | {image size} | {file size} | {file date}
this.Text = $"{appName} - {indexTotal} | {filename} | {zoom} | {imgSize} | {fileSize} | {fileDate}";
}
else
{
//ImageGlass - {index/total} - {filename} | {zoom} | {file size} | {file date}
this.Text = $"{appName} - {indexTotal} | {filename} | {zoom} | {fileSize} | {fileDate}";
}
}
}
}
#endregion
#region Key event
private void frmMain_KeyDown(object sender, KeyEventArgs e)
{
//this.Text = e.KeyValue.ToString();
#region Register MAIN MENU shortcuts
bool checkMenuShortcut(ToolStripMenuItem mnu)
{
Keys pressed = e.KeyCode;
if (e.Control) pressed = pressed | Keys.Control;
if (e.Shift) pressed = pressed | Keys.Shift;
if (e.Alt) pressed = pressed | Keys.Alt;
if (mnu.ShortcutKeys == pressed)
{
mnu.PerformClick();
return true;
}
foreach (ToolStripMenuItem child in mnu.DropDownItems.OfType<ToolStripMenuItem>())
{
checkMenuShortcut(child);
}
return false;
}
//register context menu shortcuts
foreach (ToolStripMenuItem item in mnuMain.Items.OfType<ToolStripMenuItem>())
{
if (checkMenuShortcut(item))
{
return;
}
}
#endregion
#region Detect WIN logo key
_isWindowsKeyPressed = false;
if (e.KeyData == Keys.LWin || e.KeyData == Keys.RWin)
{
_isWindowsKeyPressed = true;
}
#endregion
//Show main menu
#region Ctrl + `
if (e.KeyValue == 192 && !e.Control && !e.Shift && !e.Alt) // `
{
mnuMain.Show(toolMain, toolMain.Width - mnuMain.Width, toolMain.Height);
}
#endregion
// Rotation Counterclockwise----------------------------------------------------
#region Ctrl + ,
if (e.KeyValue == 188 && e.Control && !e.Shift && !e.Alt)//Ctrl + ,
{
mnuMainRotateCounterclockwise_Click(null, null);
return;
}
#endregion
//Rotate Clockwise--------------------------------------------------------------
#region Ctrl + .
if (e.KeyValue == 190 && e.Control && !e.Shift && !e.Alt)//Ctrl + .
{
mnuMainRotateClockwise_Click(null, null);
return;
}
#endregion
//Clear clipboard----------------------------------------------------------------
#region CTRL + `
if (e.KeyValue == 192 && e.Control && !e.Shift && !e.Alt)//CTRL + `
{
mnuMainClearClipboard_Click(null, null);
return;
}
#endregion
//Zoom + ------------------------------------------------------------------------
#region Ctrl + = / = / + (numPad)
if (((e.KeyValue == 187 && e.Control) || (e.KeyValue == 107 && !e.Control)) && !e.Shift && !e.Alt)// Ctrl + =
{
btnZoomIn_Click(null, null);
return;
}
#endregion
//Zoom - ------------------------------------------------------------------------
#region Ctrl + - / - / - (numPad)
if (((e.KeyValue == 189 && e.Control) || (e.KeyValue == 109 && !e.Control)) && !e.Shift && !e.Alt)// Ctrl + -
{
btnZoomOut_Click(null, null);
return;
}
#endregion
//Zoom to fit--------------------------------------------------------------------
#region CTRL + `
if (e.KeyValue == 191 && e.Control && !e.Shift && !e.Alt)//CTRL + /
{
mnuMainScaleToFit_Click(null, null);
return;
}
#endregion
//Actual size image -------------------------------------------------------------
#region Ctrl + 0 / Ctrl + Num0 / 0 / Num0
if (!e.Shift && !e.Alt && (e.KeyValue == 48 || e.KeyValue == 96)) // 0 || Num0 || Ctrl + 0 || Ctrl + Num0
{
btnActualSize_Click(null, null);
return;
}
#endregion
//Full screen--------------------------------------------------------------------
#region ALT + ENTER
if (e.Alt && e.KeyCode == Keys.Enter && !e.Control && !e.Shift)//Alt + Enter
{
btnFullScreen.PerformClick();
return;
}
#endregion
//ESC ultility------------------------------------------------------------------
#region ESC
if (e.KeyCode == Keys.Escape && !e.Control && !e.Shift && !e.Alt)//ESC
{
//exit slideshow
if (GlobalSetting.IsPlaySlideShow)
{
mnuMainSlideShowExit_Click(null, null);
}
//exit full screen
else if (GlobalSetting.IsFullScreen)
{
btnFullScreen.PerformClick();
}
//Quit ImageGlass
else if (GlobalSetting.IsPressESCToQuit)
{
Application.Exit();
}
return;
}
#endregion
//Ctrl---------------------------------------------------------------------------
#region CTRL (for Zooming)
if (e.Control && !e.Alt && !e.Shift)//Ctrl
{
//Enable dragging viewing image to desktop feature---------------------------
_isDraggingImage = true;
return;
}
#endregion
}
private void frmMain_KeyUp(object sender, KeyEventArgs e)
{
//this.Text = e.KeyValue.ToString();
//Ctrl---------------------------------------------------------------------------
#region CTRL (for Zooming)
if (e.KeyData == Keys.ControlKey && !e.Alt && !e.Shift)//Ctrl
{
//Disable dragging viewing image to desktop feature--------------------------
_isDraggingImage = false;
return;
}
#endregion
//Previous Image----------------------------------------------------------------
#region LEFT ARROW / PAGE UP
if (!_isWindowsKeyPressed && (e.KeyValue == 33 || e.KeyValue == 37) &&
!e.Control && !e.Shift && !e.Alt)//Left arrow / PageUp
{
NextPic(-1);
return;
}
#endregion
//Next Image---------------------------------------------------------------------
#region RIGHT ARROW / PAGE DOWN
if (!_isWindowsKeyPressed && (e.KeyValue == 34 || e.KeyValue == 39) &&
!e.Control && !e.Shift && !e.Alt)//Right arrow / Pagedown
{
NextPic(1);
return;
}
#endregion
//Goto the first Image---------------------------------------------------------------
#region HOME
if (!_isWindowsKeyPressed && e.KeyValue == 36 &&
!e.Control && !e.Shift && !e.Alt)
{
mnuMainGotoFirst_Click(null, e);
return;
}
#endregion
//Goto the last Image---------------------------------------------------------------
#region END
if (!_isWindowsKeyPressed && e.KeyValue == 35 &&
!e.Control && !e.Shift && !e.Alt)
{
mnuMainGotoLast_Click(null, e);
return;
}
#endregion
//Start / stop slideshow---------------------------------------------------------
#region SPACE
if (GlobalSetting.IsPlaySlideShow && e.KeyCode == Keys.Space && !e.Control && !e.Shift && !e.Alt)//SPACE
{
mnuMainSlideShowPause_Click(null, null);
return;
}
#endregion
}
#endregion
#region Private functions
/// <summary>
/// Update editing association app info and icon for Edit Image menu
/// </summary>
private void UpdateEditingAssocAppInfoForMenu()
{
string appName = "";
mnuMainEditImage.Image = null;
//Temporary memory data
if (LocalSetting.IsTempMemoryData)
{ }
else
{
//Find file format
var ext = Path.GetExtension(GlobalSetting.ImageList.GetFileName(GlobalSetting.CurrentIndex)).ToLower();
var assoc = GlobalSetting.GetImageEditingAssociationFromList(ext);
//Get App assoc info
if (assoc != null && File.Exists(assoc.AppPath))
{
appName = $"({ assoc.AppName})";
//Update icon
Icon ico = Icon.ExtractAssociatedIcon(assoc.AppPath);
double scaleFactor = DPIScaling.GetDPIScaleFactor();
int iconWidth = (int)((int)Constants.MENU_ICON_HEIGHT * scaleFactor);
mnuMainEditImage.Image = new Bitmap(ico.ToBitmap(), iconWidth, iconWidth);
}
}
mnuMainEditImage.Text = string.Format(GlobalSetting.LangPack.Items["frmMain.mnuMainEditImage"], appName);
}
/// <summary>
/// Select and Active Zoom Mode, use GlobalSetting.ZoomMode
/// </summary>
private void SelectUIZoomMode()
{
// Reset (Disable) Zoom Lock
GlobalSetting.ZoomLockValue = 100.0;
switch (GlobalSetting.ZoomMode)
{
case ZoomMode.ScaleToFit:
btnScaleToFit.Checked = mnuMainScaleToFit.Checked = true;
btnAutoZoom.Checked = mnuMainAutoZoom.Checked =
btnScaletoWidth.Checked = mnuMainScaleToWidth.Checked =
btnScaletoHeight.Checked = mnuMainScaleToHeight.Checked =
btnZoomLock.Checked = mnuMainLockZoomRatio.Checked = false;
break;
case ZoomMode.ScaleToWidth:
btnScaletoWidth.Checked = mnuMainScaleToWidth.Checked = true;
btnAutoZoom.Checked = mnuMainAutoZoom.Checked =
btnScaleToFit.Checked = mnuMainScaleToFit.Checked =
btnScaletoHeight.Checked = mnuMainScaleToHeight.Checked =
btnZoomLock.Checked = mnuMainLockZoomRatio.Checked = false;