为什么在将句柄窗口设置为焦点时仅在第一次工作?

时间:2018-09-21 11:11:39

标签: c# winforms

这是将窗口最小化时如何获得它们的方法:

    private static bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam)
    {
        bool specialCapturing = false;

        if (hWnd == IntPtr.Zero) return false;

        if (!IsWindowVisible(hWnd)) return true;

        if (!countMinimizedWindows)
        {
            if (IsIconic(hWnd)) return true;
        }
        else if (IsIconic(hWnd) && useSpecialCapturing) specialCapturing = true;

        if (GetWindowText(hWnd) == PROGRAMMANAGER) return true;

        if (GetWindowText(hWnd).Contains("Test"))
            windowSnaps.Add(new WindowSnap(hWnd, specialCapturing));

        return true;
    }

    /// <summary>
    /// Get the collection of WindowSnap instances fro all available windows
    /// </summary>
    /// <param name="minimized">Capture a window even it's Minimized</param>
    /// <param name="specialCapturring">use special capturing method to capture minmized windows</param>
    /// <returns>return collections of WindowSnap instances</returns>
    public static WindowSnapCollection GetAllWindows(bool minimized, bool specialCapturring)
    {
        windowSnaps = new WindowSnapCollection();
        countMinimizedWindows = minimized;//set minimized flag capture
        useSpecialCapturing = specialCapturring;//set specialcapturing flag
        EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
        EnumWindows(callback, IntPtr.Zero);

        return new WindowSnapCollection(windowSnaps.ToArray(), true);
    }

最后,我在windowSnap中有两个示例,两个窗口的应用程序名称分别为Test。

然后在Form1中,将所选窗口带入listBoxSnap并将其聚焦:

在Form1中:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace MinimizeCapture
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonSnap_Click(object sender, EventArgs e)
        {
            this.listBoxSnap.Items.Clear();
            this.pictureBoxSnap.Image = null;
            this.listBoxSnap.Items.AddRange
                (
            WindowSnap.GetAllWindows(this.checkBoxMinimized.Checked, this.checkBoxSpecialMode.Checked).ToArray()
                );
        }

        private void listBoxSnap_SelectedIndexChanged(object sender, EventArgs e)
        {
            WindowSnap snap = this.listBoxSnap.SelectedItem as WindowSnap;
            this.pictureBoxSnap.Image = snap.Image;
            SetForegroundWindow(snap.Handle);

        }
   }
}

问题是,当我第一次在listBoxSnap中选择窗口手柄时,它工作正常,它将选中的窗口手柄置于最前面并对其进行聚焦。

但是当我尝试在listBox中选择另一个窗口句柄时,它什么也没做,就是没有将它放到最前面,也没有将其再次聚焦。

我正在使用:

SetForegroundWindow(snap.Handle);

因此,确实可以将两个窗口置于最前面,并在选择它们时将焦点集中在它们上,但第一次仅一次。如果我想再次执行此操作,则需要关闭csharp程序并再次运行。

0 个答案:

没有答案