|
Bir uygulamamızda o anda aktif olan pencereyi almak istiyorsak “user32.dll” içerisindeki GetForeGroundWindow metodunu kullanırız. Bu metot bize aktif olan pencerenin handle değerini döndürür. Biz de bu handle değeriyle o forma erişebiliriz ve o forma istediğimiz işlemi uygulayabiliriz.
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
Ben uygulamamda eğer o pencere bir uygulamanın MainWindow’u ise o uygulamayı alıyorum. Bunu da formun handle değerini o uygulamanın MainWindow’unun handle değeriyle karşılaştırarak yapıyorum.
public Process GetForeGroundWindowProcess()
{
IntPtr handle = Native.GetForegroundWindow();
Process[] processes = Process.GetProcesses();
for (int i = 0; i < processes.Length; i++)
{
if (processes[i].MainWindowHandle == handle)
return processes[i];
}
return null;
}
Uygulamamda aynı zamanda aktif pencereyi aldığımız anda ekranın görüntüsünü de alıyorum. Aldığım bu görüntüyü Temp klasörüne kopyalıyor, form kapanana kadar orada saklıyorum.
public static Image GetScreenImage()
{
IntPtr handle = Native.GetDesktopWindow();
IntPtr hdcSrc = Native.GetWindowDC(handle);
Native.RECT windowRect = new Native.RECT();
Native.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
IntPtr hdcDest = Native.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = Native.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = Native.SelectObject(hdcDest, hBitmap);
Native.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Native.SRCCOPY);
Native.SelectObject(hdcDest, hOld);
Native.DeleteDC(hdcDest);
Native.ReleaseDC(handle, hdcSrc);
Image img = Image.FromHbitmap(hBitmap);
Native.DeleteObject(hBitmap);
return img;
}
public static string SaveImage()
{
Image image = GetScreenImage();
string path = Path.GetTempPath() + Path.GetFileNameWithoutExtension(Path.GetTempFileName());
image.Save(path + ".gif", ImageFormat.Gif);
return path + ".gif";
}
Daha sonra bu resimleri form kapanırken siliyorum.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
for (int i = 0; i < lwProcesses.Items.Count; i++)
{
try
{
File.Delete(lwProcesses.Items[i].SubItems[2].Text);
}
catch
{
}
}
}
Ekran görüntüsünü de alıp listeme uygulamanın adı, uygulamanın ana penceresinin ismi Title ı ile birlikte ekliyorum. Bu işlem timer her tick olduğunda yapılıyor(Uygulamamda 5 saniye).
private void timer1_Tick(object sender, EventArgs e)
{
Process process = GetForeGroundWindowProcess();
string s = ScreenCapture.SaveImage();
if (process == null)
{
lwProcesses.Items.Add(new ListViewItem(new string[] { "Null", "Null",s }));
}
else
{
lwProcesses.Items.Add(new ListViewItem(new string[] { process.ProcessName,process.MainWindowTitle,s }));
}
}
Daha sonra listemde “Click For Image” kısmına tıklandığında yeni bir form açılıp, listedeki o item ile ilgili resim yeni bir formda açılıyor. Yeni açılan formun aktif olmaması için SWP_NOACTIVATE ile gösteriyoruz.
Formun gösterilme kodları…
public void Show(string imagePath, Point p)
{
Native.ShowWindow(this.Handle, Native.SWP_NOACTIVATE);
Set(imagePath, p);
}
public void Set(string imgPath, Point pt)
{
pbImage.ImageLocation = imgPath;
Location = pt;
}
ListView’un sonuncu subitem’ına tıklandığında o formu açıyorum.
private void lwProcesses_MouseDown(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
ListViewHitTestInfo info = lwProcesses.HitTest(p);
if (info.SubItem != null && info.Item.SubItems.IndexOf(info.SubItem) == 2)
{
if (imageForm != null)
{
imageForm.Set(info.SubItem.Text, lwProcesses.PointToScreen(p));
}
else
{
imageForm = new frmImage();
imageForm.Show(info.SubItem.Text, lwProcesses.PointToScreen(p));
AddOwnedForm(imageForm);
}
}
}

Kaynak kodlarını indirmek için tıklayın...
Bol neşeli günler…
|