0

I have created one WPF window and added some control to it. Now I will get the window handle and hide the window.

I'll create separate application (console) and use the same windowHandle to show the window using below api

[DllImport("User32")]

private static extern int ShowWindow(int hwnd, int nCmdShow);


but the issue is the window is coming with the black background. Not showing the control.

Can anyone help how to get it done. (Showing WPF window using window handle)

WPF window Code

var windowInteropHelper = new WindowInteropHelper(this);
int tempHandle = (int)windowInteropHelper.Handle;
string filePath = @"C:\windowHandleLog.txt";
if (File.Exists(filePath))
{
    File.Delete(filePath);
}
File.AppendAllText(filePath, tempHandle.ToString());
this.Hide();

Console Application Code:

string filePath = @"C:\windowHandleLog.txt";
string handle = File.ReadAllText(filePath);
int windowHandle = Convert.ToInt32(handle);
ShowWindow(windowHandle, SW_SHOW);

Wpf Window Before

Wpf Window After

  • can you put some code? – Vikash Kumar Aug 22 '18 at 10:58
  • WPF window code: var windowInteropHelper = new WindowInteropHelper(this); int tempHandle = (int)windowInteropHelper.Handle; string filePath = @"C:\windowHandleLog.txt"; if (File.Exists(filePath)) { File.Delete(filePath); } File.AppendAllText(filePath, tempHandle.ToString()); this.Hide(); – Ashish Agrawal Aug 22 '18 at 11:02
  • Console application code: string filePath = @"C:\windowHandleLog.txt"; string handle = File.ReadAllText(filePath); int windowHandle = Convert.ToInt32(handle); SendMessage((IntPtr)windowHandle, WmPaint, IntPtr.Zero, IntPtr.Zero); ShowWindow(windowHandle, SW_SHOW); – Ashish Agrawal Aug 22 '18 at 11:04
  • Agarwal you can use edit section ,,,,,,It will increase readability – Vikash Kumar Aug 22 '18 at 11:05
  • Hi Vikash updated the content. Hope it will give the clear picture what is the issue – Ashish Agrawal Aug 22 '18 at 11:14
  • is their any issue if you directly pass handle in showWindow – Vikash Kumar Aug 22 '18 at 11:19
  • Directly means: both are a different application – Ashish Agrawal Aug 22 '18 at 11:23
  • Instead of trying to force a different program's window to show externally, maybe used named pipes to send a message to that program and have it show the window itself. – Kevin Cook Aug 22 '18 at 11:33
  • The main thread of a WPF app requires Application.Run(), that seems to be missing here. Remove the this.Hide() call and make sure the app works correctly, rendering and responding to input as expected. Don't use a worker thread to create the window. – Hans Passant Aug 22 '18 at 11:41
  • I got the answer in Wpf application instead of using this.Hide(); I have to use ShowWindow(windowHandle, SW_SHOW); – Ashish Agrawal Aug 23 '18 at 13:03

0 Answers0