Получение hWnd из textbox и делаем активное окно - C#
Формулировка задачи:
Собственно нужно получить из textbox hWnd и по нему сделать окно активным. Вот что у меня пока что получилось, завис на последнем клике получения из тексбокса) Заранее благодарен за помощь.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
btnSearch_Click();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2() ;
f.Show();
}
private void btnSearch_Click()
{
EnumWindows((hWnd, lParam) =>
{
if (IsWindowVisible(hWnd) && GetWindowTextLength(hWnd) != 0)
{
listBox1.Items.Add(GetWindowText(hWnd));
}
return true;
}, IntPtr.Zero);
}
string GetWindowText(IntPtr hWnd)
{
int len = GetWindowTextLength(hWnd) + 1;
StringBuilder sb = new StringBuilder(len);
len = GetWindowText(hWnd, sb, len);
return sb.ToString(0, len);
}
private void listBox1_click(object sender, EventArgs e)
{
}
}
}Решение задачи: «Получение hWnd из textbox и делаем активное окно»
textual
Листинг программы
public partial class Form1 : Form
{
class WindowWrapper
{
[DllImport( "user32.dll", SetLastError = true )]
static extern int GetWindowText ( IntPtr hWnd, StringBuilder lpString, int nMaxCount );
[DllImport( "user32.dll", SetLastError = true )]
static extern int GetWindowTextLength ( IntPtr hWnd );
[DllImport( "user32.dll", SetLastError = true )]
[return: MarshalAs( UnmanagedType.Bool )]
static extern bool IsWindowVisible ( IntPtr hWnd );
public IntPtr Handle { get; private set; }
public string Title { get { return GetWindowText( Handle ); } }
public bool IsVisible { get { return IsWindowVisible( Handle ); } }
public WindowWrapper ( IntPtr hWnd )
{
Handle = hWnd;
}
string GetWindowText ( IntPtr hWnd )
{
int len = GetWindowTextLength( hWnd ) + 1;
StringBuilder sb = new StringBuilder( len );
len = GetWindowText( hWnd, sb, len );
return sb.ToString( 0, len );
}
public override string ToString ( )
{
var title = Title;
return string.IsNullOrEmpty( title ) ? "HWND: 0x" + Handle.ToString( "X" ) : title;
}
}
delegate bool EnumWindowsProc ( IntPtr hWnd, IntPtr lParam );
[DllImport( "user32.dll", SetLastError = true )]
[return: MarshalAs( UnmanagedType.Bool )]
static extern bool EnumWindows ( EnumWindowsProc lpEnumFunc, IntPtr lParam );
public Form1 ( )
{
InitializeComponent();
RefreshWindowList();
}
private void btnSearch_Click ( object sender, EventArgs e )
{
RefreshWindowList();
}
private void RefreshWindowList ( )
{
listBox1.Items.Clear();
EnumWindows( ( hWnd, lParam ) => {
var wnd = new WindowWrapper( hWnd );
if ( wnd.IsVisible && !string.IsNullOrEmpty( wnd.Title ) )
{
listBox1.Items.Add( wnd );
}
return true;
}, IntPtr.Zero );
}
private void listBox1_SelectedIndexChanged ( object sender, EventArgs e )
{
MessageBox.Show( (listBox1.SelectedItem as WindowWrapper).Handle.ToString( "X" ) );
}
}