1

i have a listview that is populated with the running application names:

listView1.Items.Add(proc.MainWindowTitle);

The code is in a foreach statment. I have tried to use this code to get the selected item (program name) and take a screenshot of the client window of that program:

 public string selectedProgram;

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetForegroundWindow();
    [DllImport("user32.dll")]
    public static extern bool GetWindowRect(IntPtr hwnd, out Rectangle lpRect);

    private void button2_Click(object sender, EventArgs e)
    {
        Process[] process = Process.GetProcesses();
        foreach (var p in process)
        {
            selectedProgram = listView1.SelectedItems.ToString();
        }

        Rectangle bonds = new Rectangle();
        GetWindowRect(Handle, out bonds);
        Bitmap bmp = new Bitmap(bonds.Width, bonds.Height);

        using (var gfx = Graphics.FromImage(bmp))
        {
            gfx.CopyFromScreen(bonds.Location, Point.Empty, bonds.Size);
            pictureBox1.Image = bmp;
            Form2 frm2 = new Form2(this);
            frm2.Show();
            frm2.pictureBox1.Image = pictureBox1.Image;
        }

what am i doing wrong?

Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116

1 Answers1

2

It would help if you describe what parts of it do not work.

I am not sure if this is your problem, but I see that you are simply overwriting one variable (selectedProgram) in your first for loop

Process[] process = Process.GetProcesses();
foreach (var p in process)
{
    selectedProgram = listView1.SelectedItems.ToString();
}

And then use the Member variable as the handle rather than the handle for that process' window GetWindowRect(Handle, out bonds);

You would need a call to get the window handles from the process. Also, is it not possible for a Process to have multiple window? Don't you need to loop through all the windows for the specified process using a combination of EnumWindows and GetWindowThreadProcessID() ( How to get main window handle from process id? )

Once you find that you are getting the correct window handles for the process (spy++ should help in that), you should be able to have a better picture of what is not working for you

Community
  • 1
  • 1
IDK
  • 178
  • 8