Skip to content

Commit

Permalink
get process windows
Browse files Browse the repository at this point in the history
  • Loading branch information
huetterm committed Mar 30, 2019
1 parent 595ef72 commit 6f34d78
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DesktopRestorer/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Click="ButtonBase_OnClick">click</Button>
<TextBlock Name="TB" TextWrapping="Wrap"></TextBlock>
<Button Click="ButtonBase_OnClick" HorizontalAlignment="Right">click</Button>

</Grid>
</Window>
34 changes: 34 additions & 0 deletions DesktopRestorer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,44 @@ enum ShowWindowCommands
static extern bool SetWindowPlacement(
IntPtr hWnd,ref WINDOWPLACEMENT lpwndpl);

delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);

[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn,
IntPtr lParam);

static List<IntPtr> EnumerateProcessWindowHandles(int processId)
{
var handles = new List<IntPtr>();

foreach (ProcessThread thread in Process.GetProcessById(processId).Threads)
EnumThreadWindows(thread.Id,
(hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);

return handles;
}

private const uint WM_GETTEXT = 0x000D;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam,
StringBuilder lParam);


public MainWindow()
{
InitializeComponent();
var processes = Process.GetProcesses();

TB.Text = string.Join("\n", processes.Select(process => process.ProcessName));

foreach (var handle in EnumerateProcessWindowHandles(
Process.GetProcessesByName("explorer").First().Id))
{
StringBuilder message = new StringBuilder(1000);
SendMessage(handle, WM_GETTEXT, message.Capacity, message);
Console.WriteLine(message);
}
}

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit 6f34d78

Please sign in to comment.