Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace ThumbnailViewer
- {
- public partial class Form1 : Form
- {
- // DWM Structs.
- [StructLayout(LayoutKind.Sequential)]
- internal struct PSIZE
- {
- public int x;
- public int y;
- }
- [StructLayout(LayoutKind.Sequential)]
- internal struct Rect
- {
- internal Rect(int left, int top, int right, int bottom)
- {
- Left = left;
- Top = top;
- Right = right;
- Bottom = bottom;
- }
- public int Left;
- public int Top;
- public int Right;
- public int Bottom;
- }
- [StructLayout(LayoutKind.Sequential)]
- internal struct DWM_THUMBNAIL_PROPERTIES
- {
- public int dwFlags;
- public Rect rcDestination;
- public Rect rcSource;
- public byte opacity;
- public bool fVisible;
- public bool fSourceClientAreaOnly;
- }
- // DWM APIs.
- [DllImport("dwmapi.dll")]
- static extern int DwmRegisterThumbnail(IntPtr dest, IntPtr src, out IntPtr thumb);
- [DllImport("dwmapi.dll")]
- static extern int DwmUnregisterThumbnail(IntPtr thumb);
- [DllImport("dwmapi.dll")]
- static extern int DwmQueryThumbnailSourceSize(IntPtr thumb, out PSIZE size);
- [DllImport("dwmapi.dll")]
- static extern int DwmUpdateThumbnailProperties(IntPtr hThumb, ref DWM_THUMBNAIL_PROPERTIES props);
- static readonly int DWM_TNP_VISIBLE = 0x8;
- static readonly int DWM_TNP_OPACITY = 0x4;
- static readonly int DWM_TNP_RECTDESTINATION = 0x1;
- // Thumbnail Handle.
- private IntPtr thumb;
- // Running ProcessList.
- private Process[] processList;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- // Create processList to ComboBox.
- processList = Process.GetProcesses();
- foreach (Process process in processList)
- {
- comboBox1.Items.Add(process.ProcessName);
- }
- }
- private void Form1_FormClosed(object sender, FormClosedEventArgs e)
- {
- ReleaseThumb();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- // Release Exist Thumbnail.
- ReleaseThumb();
- // Regist New Thumbnail.
- foreach (Process process in processList)
- {
- String processName = process.ProcessName;
- int pid = process.Id;
- if (processName.Equals(this.comboBox1.SelectedItem))
- {
- IntPtr hWnd = process.MainWindowHandle;
- int i = DwmRegisterThumbnail(this.Handle, hWnd, out thumb);
- UpdateThumb();
- break;
- }
- }
- }
- private void UpdateThumb()
- {
- if (thumb != IntPtr.Zero)
- {
- PSIZE size;
- DwmQueryThumbnailSourceSize(thumb, out size);
- DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES();
- props.fVisible = true;
- props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY;
- props.opacity = 255;
- props.rcDestination = new Rect(pictureBox1.Left, pictureBox1.Top, pictureBox1.Right, pictureBox1.Bottom);
- DwmUpdateThumbnailProperties(thumb, ref props);
- }
- }
- private void ReleaseThumb()
- {
- if (thumb != IntPtr.Zero)
- {
- DwmUnregisterThumbnail(thumb);
- thumb = IntPtr.Zero;
- }
- }
- } // End of ThumbnailViewer Class.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement