Process.GetCurrentProcess().MainWindowHandle returns zero - c#

I'm trying to get the window handle (HWND) of the main form of my C# application (the application has only 1 form).
Some solutions on the internet show that I can use:
Process.GetCurrentProcess().MainWindowHandle
to get the window handle of current process of my application. But this value is always zero, anything wrong?

MSDN says:
The main window is the window opened by the process that currently has the focus (the TopLevel form). You must use the Refresh method to refresh the Process object to get the current main window handle if it has changed.
and
A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window, the MainWindowHandle value is zero. The value is also zero for processes that have been hidden, that is, processes that are not visible in the taskbar. This can be the case for processes that appear as icons in the notification area, at the far right of the taskbar.
See http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle(v=vs.110).aspx

In properties [F4] window 'Show in Taskbar' should be True

try using
System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle

Related

How to pass a c# window(form) handle to a VB6 project or form?

We have an application where we launch a VB modal dialog from a c# form on a button click. Even though the VB dialog is a modal window we are able to go back to C# dialog on click and then the application hangs.
Looks like the VB6 form does not have a parent and hence the issue. So we decided to pass the form handle from c# to VB and make our c# dialog as a parent to the VB dialog. Any help or a sample code would be really appreciated.
Once we click search on our c# dialog, we call this:
gPMSearch.Search();
The Search class is in VB:
Private Sub Class_Initialize()
'Load the search form
Set Form = New frmMain
Load Form
End Sub
The form is loaded as below:
Me.Show vbModal
Ownership of a window is set at window creation. Help says it can't be changed.
Owned Windows
An overlapped or pop-up window can be owned by another overlapped or pop-up window. Being owned places several constraints on a window.
An owned window is always above its owner in the z-order.
The system automatically destroys an owned window when its owner is destroyed.
An owned window is hidden when its owner is minimized.
Only an overlapped or pop-up window can be an owner window; a child window cannot be an owner window. An application creates an owned window by specifying the owner's window handle as the hwndParent parameter of CreateWindowEx when it creates a window with the WS_OVERLAPPED or WS_POPUP style. The hwndParent parameter must identify an overlapped or pop-up window. If hwndParent identifies a child window, the system assigns ownership to the top-level parent window of the child window. After creating an owned window, an application cannot transfer ownership of the window to another window.
Dialog boxes and message boxes are owned windows by default. An application specifies the owner window when calling a function that creates a dialog box or message box.
An application can use the GetWindow function with the GW_OWNER flag to retrieve a handle to a window's owner.
Are these in process or out of process VB6. Can't you call the VB6 form (rather than a sub) from C#
Anyway you'll need to write stupid code that simulates the behaviour you want. Monitoring their Z order and changing it as required.
So I'd put a timer in C# every 50ms. If test if C# is the active window, if so makes the VB6 active.
The point is to call ShowDialog, not just Show.
'Assuming that `MyModalDialogForm` is a c# object
Dim modalDialog = New MyModalDialogForm()
modalDialog.ShowDialog(Me)
Passing Me as the owner also ensures that the modal dialog stays on top of the other form.
If you need the result:
Dim modalDialog = New MyModalDialogForm()
If modalDialog.ShowDialog(Me) = DialogResult.Ok Then
...
End If
However; if the dialog is opened by gPMSearch.Search(); you cannot influence the behavior of the form in VB6. You will have modify the code of gPMSearch.Search() in C#.

Mono Gtk Window require focus for input

I haven't been able to find an example containing this functionality, and either i missed it in the documentation or it's not there.
I have a fullscreen GUI program, and when a user is required to type in a number, a calculator window popup has to appear in the center of the screen. The user types a number and clicks on enter, or hits cancel to continue past the window.
The problem is clicking on the fullscreen window behind the calculator brings that window to the front and hides the calculator without the intended entry being completed, which could get annoying for the user.
I guess the functionality I'm trying to create is what happens in most text editors/IDE's when you press the Open File button. Let me know if you want to see code, it's just two separate Window classes at the moment.
The Present() function of the Window object brings that particular window to the front. So adding a FocusOutEvent listener on the window you wish to keep in front like:
windowObj.FocusOutEvent += (obj, args) => windowObj.Present();
will work.
The alternative to this (and the better way) is to set the Modal property of the window to true.
If you've stumbled across this and you were looking for a popup window that suspends whatever called it to wait for input, see this question:
gtk# thread for window
You basically use the Dialog class instead of Window and add your elements to the ActionArea of the Dialog.
Hope this helps.

How do you use a child window in Silverlight such that it is not done asynchronous?

How do you use a child window in Silverlight such that it is not done asynchronous? The way you use a child window in Silverlight (so far as I know) is to use the show method:
ChildWindow update_dlg = new Update();
update_dlg.Show();
But you cannot get the input from the dialog because the call is asynchronous and keeps on running while the child window us up.
You can't, you need to listen for the child window closing then grab the dialog result.
From MSDN page:
To get the DialogResult value from a child window, handle the Closed event in the code-behind page of the calling window. In the Closed event handler, cast the sender parameter to a ChildWindow or a derived class to access the DialogResult property.
I like to pass an Action to the child window, if the user cancels the window nothing is called, if the user accepts the window the action is executed with the parameters from the window. This way the code to run when the user accepts is still written in the calling window.
There are three ways of doing this as I see it.
1) launch a seperate thread in the main app that waits until some third party class member is switched from true to false to indicate that the child window has been closed. Then in the child window, when a shutdown or OK or Cancel is done, this third party class member is switched too false etc. (this is the way i did this)
2) have a call back function in the main program that is called by the child window.
3) kind of cheat and don't have a child window at all. Instead, in the XAML code of the main window, have a rectangle that looks like a pop up or child window that is hidden (visual set to collapse) that comes up (but really becomes visual) and all other fields in the main program are set to inactive until the right butons are puched.**

Get all window handles and respective label handles given process name

is it possible to get the handle of a specific component in an unknown number of window opened by the same program?
The program foo.exe contains a button that, when clicked, opens a form containing a Label and an Image ( with no upper bound on the number of forms opened). Is possible to get all the handle of the Label component of every window openend, given the process name "foo" ?
with EnumWindows or FindWindow you can find the application window depending if you already know the title or the process name or so on, you can even use FindWindowLike if you really do not much of it.
Once in one way or another you know the windows handle of your window, with GetDlgItem you can get info about your child controls.

Visual C# Window Behaver

I have a form which instances a new form that I've already created using
statuswindow statwin = new statuswindow();
statwin.ShowDialog();
return statwin;
This code is in a function. The problem is when I run the function in other code the next line of code doesn't run until the newly instanced window is closed by the user, I'm guessing this is the side-effect of using ShowDialog(), is there a way to have it use the same behaver (being topmost, not being to select the other main window) while still letting the main form's code run?
Update: Although Show(); will work, I need to be able to make the window like a dialog by having it always on top of the other window and it always being the active selected window.
The ShowDialog method of showing a window shows it as a dialog, which is a blocking mechanism within C#/.net. If you want to simply display the window and not cause the running code to block until it is closed, you can use the window.Show() function.
In your code:
startwin.Show();
This will cause the startwin form to become visable to the user, and will fire the startwin.VisibleChanged event as well as the startwin.Load event to fire.
To make the new window always on top, you could set the Topmost window flag in the form properties.
To make the window run in a separate thread, first spawn the thread, then create the window from that thread.
There are also "application modal" and "system modal" Win32 window flags, but I don't know if those are exposed to WinForms or not -- go have a look on the form properties!

Categories

Resources