I created a usercontrol in c#, Leave and LostFocus are not fired if I switch to another program. How can I detect if the app loses focus?
Try your mainform's Deactivate event
If you switch to a differnt app, your custom control DIDN'T lose focus. At least not within the app it is in.
If you come back to your app, such as hitting it on the taskbar (and not by clicking on another control on the same app), your control will still have "focus".
This is where Form.Deactivate comes in handy.
Related
I need to disable a button from a third-party winforms application.
I got the buttons handle using Windows API and WinSpy++, and i have no problem disabling it.
That button is in a DialogBox control. My application is also a winforms app.
How can i be notified when the dialog box appear, so i can disable that button?
Is there an event that can fire when it appears or any workaround?
Thanks guys!
I'm working with a WPF application.This application runs on a pc with a touch screen and when I push one button for more than 3 seconds MyButton_PreviewMouseUp event occurs and after that occurs a MyButtonSimple_PreviewMouseRightButtonDown event. I need to disable something for disabling the function of windows that do this.
You are not describing the default behaviour of a Button. In a normal WPF application, the PreviewMouseUp event occurs only when a pressed mouse button is let go. You can easily prove this yourself by adding a Button to a new WPF application and attaching a handler to the PreviewMouseUp event. You will see that it is only raised when you lift your finger off a pressed mouse button.
So this means that it is your code that is causing that behaviour and as you haven't shown us the relevant section (or indeed any code), we cannot help you with this further. I suggest searching your application for PreviewMouseUp to see if this event is being manually raised anywhere.
i have written a small form application, which contains textbox only. I have enabled shortcut key by using low level keyboard hook to give focus to application when needed.
All is working fine, I press the short key i.e. (Left Control Key)+(Left Control Key) for the first time the application get proper control (focus).
But when I deactivate and redo the shortcut key, the icon of the application on the taskbar starts blinking and the form doesn't get focus, the title bar is greyed out.
More Information :
on deactivation the form's opacity is reduced but it remains on top, so the whole time the form is displayed on the screen
This form is activated from another class, within the application.
On Activation event opacity of the form is increased so it now very well visible
I am giving focus to the application by using form.Activate() I have also tried from.Visible but with no luck.
The activation works for the first time only, post that the icon in taskbar blinks.
Does any have any idea why is this happening?
This answer applies to you as well:
https://stackoverflow.com/a/3789985/64121 . You need to make use of the AttachThreadInput API function in order to steal focus away from another app.
I've written a c# application which automates an instance of IE. I'd like to know when internet explorer gains focus and when it looses focus.
From the SHDocVw.InternetExplorer object I can get it's HWND. From there how can I create a message hook to receive WM_KILLFOCUS and WM_FOCUS events (assuming those are the correct events to be listening for :))?
Thanks everyone!!
UPDATE: I found a way I could use to accomplish the above goal without using hooks (which I haven't quite figured out how to do in c# yet), using the .NET framework in this question.
The problem with this code
AutomationFocusChangedEventHandler focusHandler
= new AutomationFocusChangedEventHandler(OnFocusChanged);
Automation.AddAutomationFocusChangedEventHandler(focusHandler);
is that it is easy for a window to be the foreground window and that event won't fire when that window is switched to because it's waiting for a specific UI Element to be in focus. (To test this you can use a function that uses that code and prints a message everytime a new window is in focus, such as the MSDN sample TrackFocus, and then click on a webbrowser. When most webpages or a blank page is being displayed in the browser the event wont fire until the address bar or some other element is selected.) It could probably work if there was a way to modify so that it could throw the event if either no UI Element is in focus or every time an element lost focus (instead being thrown when it gains focused). Anyone have any ideas on how I could fix the above code to solve my problem?
Update 2: I just came across this (article claims you can only hook to mouse and keyboard from c#) as well which may mean I can't use hooks at all for what I'd like to do.
Detailed instructions on setting up a hook from C# are here: http://support.microsoft.com/kb/318804/en-us?fr=1
I seem to have a problem I am trying to detect when my form loses focus but it has a COM interop based user control on it to display a web cam feed. This is causing the loses and gained focus events to not always fire. Is there a way to detect that the application has lost focus, perhaps through a windows api call.
This SO thread might help you.