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;
Related
I want to set an application to be the child of my WPF application.
My application has 3 window and I would like all windows to be childs of the WPF.
I use this code enter code here
main:
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);
loaded:
var helper = new WindowInteropHelper(this);
SetParent(proc.MainWindowHandle, helper.Handle);
It works just for one of the windows.
What should I do to have all windows in child of WPF?
Each control in WinForm has a unique handle, but WPF elements have not, To solve your problem you can put one or more WindowsFormsHost in the window and use handle of them because each WindowsFormsHost also has a unique handle the same as WinForm controls.
Then use SetParent like this :
SetParent(proc.MainWindowHandle, YourWindowsFormsHost.Handle );
I'm trying to obtain process information for the current active application (or window), using .Net/C#.
Currently I'm using
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
To get the current active window.
I understand there's no native way to do this other than use this API function.
From that, I use:
[DllImport("user32")]
private static extern UInt32 GetWindowThreadProcessId(IntPtr hWnd, out Int32 lpdwProcessId);
To get the process name that belongs to that window and then I get further process information.
I also use
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
To get the current Window Text or caption.
Now, using the Process class, I can use
MainWindowTitle
to get the main Window title as well.
The thing is, MainWindowTitle and GetWindowText does not return the same information.
For example, let's say the main application opened is "Toad" with a connection and an editor open.
Then with GetWindowText I get:
"Toad for Oracle - myConnection - Somequery.sql".
and Process.MainWindowTitle returns
"myConnection".
So, the question is how do I get the exact same text as I get using GetWindowText, but using merely .Net classes?
Edit:
I found out that actually the reason is simply because both functions are not querying the same window handle.
The window handle returned in the GetForegroundWindow, is the number 198982.
And the MainWindowHandle property, which I suppose is the one used in the MainWindowTitle propery is the number 198954.
Using Spy++ I could find and confirm those windows handle captions are the one returned by their corresponding function.
So the "problem", if any, is that the Process class does not correctly identify the most foreground window as the Main Window.
GetForegroundWindow gives you the active window the user is working in and that might be a owned window or a modal dialog, not necessarily the applications main/root window.
MainWindow is a .NET concept, native win32 does not have such a thing and there can be 0, 1 or multiple "main windows" in an application.
Some Delphi/C++Builder applications have a HWND for the taskbar button and each form is a owned window belonging to this "invisible" window. Other UI frameworks may pull similar stunts that might confuse "main window" detection.
You can use UI Automation to inspect other applications if you don't want to use p-invoke. Start with the foreground window and walk up the tree of owned and child windows...
I want create using C# window with setted parent to my defined handle,
this is a other process window handle.
Anyone know how to do this?
Greetings,
If I understood your question correctly you should be able to achieve what you want by using something like this:
class Win32Window : IWin32Window
{
IntPtr handle;
public Win32Window(IntPtr handle) { this.handle = handle; }
public IntPtr Handle
{
get { return this.handle; }
}
}
static void Main()
{
IntPtr targetParent = // Get handle to the parent window
new MainForm().ShowDialog(new Win32Window(targetParent));
}
This will turn MainForm a child window of the specified window making it always appear above it. I use ShowDialog in the example, but this should also work for Show. This is specific for Windows Forms.
In WPF you can try the following:
var helper = new WindowInteropHelper(/* your Window instance */);
helper.Owner = // Set with handle for the parent
I quickly tried this after showing the WPF window and it seemed to work as expected, but WPF knowledge is not that great.
I believe the Handle will be read-only; therefore, the .Parent property is read-only. However, the .Owner property has a getter and a setter (ref. MSDN) ... however, you must have a reference to the Parent window.
Without more information, I will not be able to provide much more than that.
If your parent candidate is an unmanaged Window, check this link.
I have done a global mouse event in my windows application. When i click the center button of my mouse, i want to make a particular form topmost...
There are some applications which runs in the full screen mode, so i need to do this, in order to make my form visible to the users, because this is the only way to view it. Since Alt + Tab is disabled. This is a Kiosk application.
I tried using Topmost = true for that particular form and
I tried using below code...But no use. I am not getting my form in front.
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
private void BringToFront(string className,string CaptionName)
{
SetForegroundWindow(FindWindow(className,CaptionName));
}
The global hotkey which has to trigger this form to bring front is working perfectly.
How to make my form come front ??? Thanks.
I solved it, doing this:
this.TopMost = true;
this.TopMost = false;
Not my thing but have you tried messing with:
this.BringToFront();
this.Activate();
get the handle of the window and do this
SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE| SWP_SHOWWINDOW);
You need set the fullscreen form's topmost to false, then continue on.
You may not be calling your BringToFront method correctly. For the FindWindow API function, lpClassName would be your application's name (e.g. "MyApplication.exe"), while lpWindowName refers to the caption in the particular form's title bar (e.g. "Form1"). Usually with FindWindow you pass in one or the other, eg:
FindWindow("MyApplication.exe", null);
// or
FindWindow(null, "Form1");
I'm not sure what happens when you pass both.
You may also just need to do something simple to achieve this, like calling the particular form's Activate() method.
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.