Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5.  
  6. namespace ThumbnailViewer
  7. {
  8. public partial class Form1 : Form
  9. {
  10. // DWM Structs.
  11. [StructLayout(LayoutKind.Sequential)]
  12. internal struct PSIZE
  13. {
  14. public int x;
  15. public int y;
  16. }
  17.  
  18. [StructLayout(LayoutKind.Sequential)]
  19. internal struct Rect
  20. {
  21. internal Rect(int left, int top, int right, int bottom)
  22. {
  23. Left = left;
  24. Top = top;
  25. Right = right;
  26. Bottom = bottom;
  27. }
  28. public int Left;
  29. public int Top;
  30. public int Right;
  31. public int Bottom;
  32. }
  33.  
  34. [StructLayout(LayoutKind.Sequential)]
  35. internal struct DWM_THUMBNAIL_PROPERTIES
  36. {
  37. public int dwFlags;
  38. public Rect rcDestination;
  39. public Rect rcSource;
  40. public byte opacity;
  41. public bool fVisible;
  42. public bool fSourceClientAreaOnly;
  43. }
  44.  
  45. // DWM APIs.
  46. [DllImport("dwmapi.dll")]
  47. static extern int DwmRegisterThumbnail(IntPtr dest, IntPtr src, out IntPtr thumb);
  48.  
  49. [DllImport("dwmapi.dll")]
  50. static extern int DwmUnregisterThumbnail(IntPtr thumb);
  51.  
  52. [DllImport("dwmapi.dll")]
  53. static extern int DwmQueryThumbnailSourceSize(IntPtr thumb, out PSIZE size);
  54.  
  55. [DllImport("dwmapi.dll")]
  56. static extern int DwmUpdateThumbnailProperties(IntPtr hThumb, ref DWM_THUMBNAIL_PROPERTIES props);
  57.  
  58. static readonly int DWM_TNP_VISIBLE = 0x8;
  59. static readonly int DWM_TNP_OPACITY = 0x4;
  60. static readonly int DWM_TNP_RECTDESTINATION = 0x1;
  61.  
  62. // Thumbnail Handle.
  63. private IntPtr thumb;
  64.  
  65. // Running ProcessList.
  66. private Process[] processList;
  67.  
  68. public Form1()
  69. {
  70. InitializeComponent();
  71. }
  72.  
  73. private void Form1_Load(object sender, EventArgs e)
  74. {
  75. // Create processList to ComboBox.
  76. processList = Process.GetProcesses();
  77. foreach (Process process in processList)
  78. {
  79. comboBox1.Items.Add(process.ProcessName);
  80. }
  81. }
  82.  
  83. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  84. {
  85. ReleaseThumb();
  86. }
  87.  
  88. private void button1_Click(object sender, EventArgs e)
  89. {
  90. // Release Exist Thumbnail.
  91. ReleaseThumb();
  92.  
  93. // Regist New Thumbnail.
  94. foreach (Process process in processList)
  95. {
  96. String processName = process.ProcessName;
  97. int pid = process.Id;
  98.  
  99. if (processName.Equals(this.comboBox1.SelectedItem))
  100. {
  101. IntPtr hWnd = process.MainWindowHandle;
  102. int i = DwmRegisterThumbnail(this.Handle, hWnd, out thumb);
  103. UpdateThumb();
  104. break;
  105. }
  106. }
  107. }
  108.  
  109. private void UpdateThumb()
  110. {
  111. if (thumb != IntPtr.Zero)
  112. {
  113. PSIZE size;
  114. DwmQueryThumbnailSourceSize(thumb, out size);
  115.  
  116. DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES();
  117.  
  118. props.fVisible = true;
  119. props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY;
  120. props.opacity = 255;
  121. props.rcDestination = new Rect(pictureBox1.Left, pictureBox1.Top, pictureBox1.Right, pictureBox1.Bottom);
  122.  
  123. DwmUpdateThumbnailProperties(thumb, ref props);
  124. }
  125. }
  126.  
  127. private void ReleaseThumb()
  128. {
  129. if (thumb != IntPtr.Zero)
  130. {
  131. DwmUnregisterThumbnail(thumb);
  132. thumb = IntPtr.Zero;
  133. }
  134. }
  135.  
  136.  
  137. } // End of ThumbnailViewer Class.
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement