Getting the handle of window in C# - c#

I'm trying to do some P/Invoke stuff and need the handle of the current window.
I found Getting the handle of window in C#
But it appears that only works in WPF. Is there a winForms equivalent?

Control.Handle
An IntPtr that contains the window handle (HWND) of the control.

Try this in your form:
IntPtr myHandle = this.Handle;
It will return the handle of the form.

Related

Windows Forms Control.CreateControl() equivalent in WPF

normally I use UserControls from WinForms and can then have them created with the CreateControl() method and displayed in another application. But now I wanted to try this with WPF. But there I can't find a way to create the UserControl and then get the corresponding handle.
I tried it with
HwndSource hwndSource = PresentationSource.FromVisual(Control) as HwndSource;
if (hwndSource != null)
{
handle = hwndSource.Handle;
}
but i got no handle.
Then there is this possibility, which theoretically works, but only for Windows
IntPtr handle = new WindowInteropHelper(myWindow).EnsureHandle();
Normal Usage with WinForms Controls on a Sample:
MyCoolControl control = new MyCoolControl();
control.CreateControl();
SendHandleToSomethingElse(control.Handle.ToInt32());
and now i need this for WPF :D
Unlike a Windows Forms control, a WPF control doesn't have its own HWND handle.
In WPF only the top-level window has a handle and the controls are rendered as content inside this HWND.
Please refer to the docs for more information about how WPF uses HWNDs.

C# - How to make a X509Certificate2UI window always on top

Is it possible to make X509Certificate2UI window always on top, or any other way to select certificate with window on top? I'm using X509Certificate2UI.SelectFromCollection method
You can make the window stay on top of the parent window, if you pass a handle to the parent window to the function displaying the window, i.e. you need to use the following overload of the SelectFromCollection method where hwndParent is set to the handle of the opening window:
public static X509Certificate2Collection SelectFromCollection(
X509Certificate2Collection certificates,
string title,
string message,
X509SelectionFlag selectionFlag,
IntPtr hwndParent
)
In Windows Forms you can simply get the handle from the Handle property of the form:
IntPtr windowHandle = form.Handle;
If you are using WPF, you need to use the WindowInteropHelper class to retrieve the handle:
IntPtr windowHandle = new WindowInteropHelper(Application.Current.MainWindow).Handle;

Enumerate all controls inside window

I'm trying to enumerate all controls inside a window using user32.dll, but I don't know exactly what I need to do.
I'm using EnumWindows and EnumChildWindows, but it doesn't retrieve all controls inside a window.
I want to get the text of some labels in a Delphi application.
I tried using Spy++, but it doesn't list too.
IList<IntPtr> childern = new List<IntPtr>();
WNDENUMPROC enumChildProc = delegate(IntPtr hwnd, IntPtr param)
{
childern.Add(hwnd);
return true;
};
EnumChildWindows(Hwnd, enumChildProc, IntPtr.Zero);
return childern;
which Hwnd is the parent window handle
Delphi labels are non-windowed. That means that you cannot ever hope to obtain window handles for them since they are not windows.
If the Delphi VCL properly supported automation then you'd be able to use UI Automation to inspect the text of these controls. But the VCL only supports automation by dint of the underlying Win32 controls doing so. Since labels aren't windowed controls, again you are out of luck.
The only thing that can realistically hope to read this text is Delphi code inside the process. It does not sound as though that will be viable for you.

How to call native code which draw in WPF Image from C# code?

I have a native library (no sources available) wrapped into C# code.
The following C# declaration exists:
[DllImport(DRIVER_DLL_NAME,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "RenderBitmap")]
private static extern int RenderBitmap(int hWnd);
I need to call this function from my WPF C# project.
I have a working sample code for Windows forms:
System.Windows.Forms.PictureBox DisplayWindow;
...
RenderBitmap(DisplayWindow.Handle.ToInt32());
And I have not found how to do so with WPF System.Windows.Controls.Image instead of System.Windows.Forms.PictureBox - there is no Handle property or something similar.
Moreover I found in "WPF and Win32 Interoperation" the following statement:
"When you create a WPF Window, WPF creates a top-level HWND, and uses an HwndSource to put the Window and its WPF content inside the HWND. The rest of your WPF content in the application shares that singular HWND.".
It seems that HWND handle does not exist at all for Image.
How to call native code which draw in WPF Image from C# code?
WPF Controls does not have handles like in WinForms. Only main window handle is accessible:
For instance, in main window class (or use Application.Current.MainWindow):
var handle = (new WindowInteropHelper(this)).Handle;
var hwnd = HwndSource.FromHwnd(handle);
So looks like you've to consider another approach instead of native calls.
BTW, why you need such a low level drawing functionality? I just can assume that you simply want to reuse already implemented one for WinForms. Perhaps you can achieve the same using built in WPF features.

Set active window

I'm trying to make an app that gives a quake style drop-down HUD console. I can get it to show and hide the window, but I can't figure out how to set it as the active window after showing it. Im using Win API calls to show and hide the window. I've tried SetForegroundWindow(IntPtr hWnd) and SetFocus(IntPtr hWnd) to no avail. Anyone have any ideas?
http://pastebin.com/DgtJJGiv
public void ShowApp()
{
IntPtr h = FindWindow(null, "C:\\Windows\\system32\\cmd.exe");
ShowWindow(h, SW_SHOW);
//EnableWindow(h, true);
isHidden = false;
// set focus to console window
SetForegroundWindow(h);
System.Diagnostics.Debug.WriteLine(h);
}
I found an answer here:
How to show form in front in C#
The winAPI approaches were not working correctly for me but this did:
form.TopMost = true;
form.TopMost = false;
I originally was only setting TopMost to true but I ran into problems with dialog boxes displaying behind the form. It appears that setting TopMost to true pulls the form to the front and holds it there. Setting it to false doesn't push it back but does allow other forms to be shown in front. I was still having problems with focus so I ended up going with the following:
form.Activate();
You may use SetActiveWindow winAPI method. Hope this helps...
Try this (works for me):
public static void ShowApp()
{
IntPtr h = FindWindow(null, "C:\\Windows\\system32\\cmd.exe");
ShowWindow(h, ShowWindowCommands.Show);
SetForegroundWindow(h);
SetFocus(h);
System.Diagnostics.Debug.WriteLine(h);
}
Is there any reason why you can't implement your own console window? What I mean is a simple Form with a Textbox set to the correct style. You would probably have more control over how it works than trying to use the 'cmd' process.
Just a thought.

Categories