I'm trying to figure out how I'd be able to essentially trick a window into thinking it has focus. What I've found out through some searching, is that I'd need some sort of sandbox to accomplish this. An intermediate layer between the OS and the window.
I'd appreciate some assistance in pointing me in the right direction of how I'd be able to accomplish this through C# (with pinvoke, etc, if necessary). To essentially launch another executable from the C# program, and set it up in some fashion to be on a faux desktop, with constant window focus. But still be able to interact with the window as normal (like a window wrapper, if you will, that can lose focus all it wants, but the window contained within will never think it's out of focus).
I'm sure this is quite an undertaking (though, maybe it won't be, perhaps someone knows an elegant solution to this), but I know it's possible, as I've seen something similar done in a commercial product. (I'm trying to emulate one of the behaviors of this sandbox product).
First of all, try calling an activation event:
private void Form1_Activated(object sender, EventArgs e)
{
//Code
}
Then out of any functions, declare an eventarg:
private EventArgs ev;
Finally, when you want to fake a focus, call the activated function:
Form1_Activated(this, ev);
Hope that help!
You could try injecting a WM_ACTIVATE message into the target windows message queue. This may trick it into thinking it has focus, even when it does not. It really depends on how the application is checking for focus.
Related
I have a rather large C# Forms application. There's an MdiParent which controls lots of MdiChilds. I also created a 'loading screen' which jumps always on top when the user opens forms which require some loading time.
The thing is, if there are exceptions, I usually use something like Messagebox.Show(ErrorNumber) in the catch, so the user knows what happened. When my loading screen is on, and a message box shows, it's impossible to click it.
I'd like to tweak this. Is there a way to put a function in my MdiParent (or somewhere else, as long as it's just once) so it runs code whenever a MessageBox is shown? This way, I can easily call a function to close my loading screen. There are probably other solutions, but I'd like to learn more about window handles, hooks, etc.
You can subscribe to the Application.EnterThreadModal event.
This will be raised each time your application is about to enter the modal state (for example, because it is about to display a message box).
Note that there is also a corresponding Application.LeaveThreadModal event.
I think this is an awful idea. Imho, instead of practicing such voodoo, you should fix the real issue, which is the way you handle exceptions. Fixed code might look like this:
catch(Exception ex)
{
CloseLoadingScreen();
MessageBox.Show(ErrorNumber);
}
I mean, you are free to explore window hooks and stuff, its just that you've picked a bad occasion.
Edit: I think the proper way to refactor your code is to implement an entity, which sole purpose will be notifying user about important stuff. This entity will have knowledge about loading screen state and other relevant stuff, and depending on this context will do a set of actions (show message box, write to log, close loading screen, etc.). Then you can use this entity whenever you need to send a notification. This way you won't need to go through all the catch statements whenever you want to alter notification logic. It can be made a singleton for easier access.
I have a application in which i want to open a calculator on a button click and once the operation are performed on calculator and calculator is closed, I want that value back to my text box.
How can I achieve this functionality.
private void btnDollarTransferHelp_Click(object sender, EventArgs e)
{
System.Diagnostics.Process myProcess = System.Diagnostics.Process.Start("calc.exe");
}
I am using C# 4.0 and its a window based application.
Regards and Thanks
You may use the Microsoft Script Control to implement the calculator functionality in your C# app. The control has a simple Eval() method to evaluate expressions that may range from simple "2+2" to VBScript commands. More information here:
http://msdn.microsoft.com/en-us/library/aa227637(v=vs.60).aspx
It is indeed simpler to implement a calculator in your app: reading the content of another's process window is not trivial! And being notified when the calculator is "about to close" is even harder.
It involves, for sure, widows API (FindMessage/SendMessage) (see for example https://stackoverflow.com/a/360247/863564), probably P/Invoke, probably even hooking calc.exe (the technique used by Spy++ -- you need it to capture the WM_CLOSE message - see here what happens when you are closing a window)
(You need to capture WM_CLOSE, because it is the latest sensible moment to grab the result from the control on the window)
So, the alternative: a great one is scriptcs, but it may be well too much! A quick search revealed
some
great
projects
Either way, have fun!
you could use an existing library e.g. http://nclac.codeplex.com to provide calculation within your application.
I was wondering if there was any way through C# or C++ to send to fake or otherwise trick a program into thinking it has focus? I'm thinking that there's a message you can send to it using SendMessage/PostMessage that'll trick it into having focus.
There is no guaranteed way to trick it into thinking it has focus because there are multiple ways it can check if it has focus. For example, it could be checking for WM_SETFOCUS and then checking it is out of focus when it receives WM_KILLFOCUS. So you could trick it in this case by sending intercepting all WM_KILLFOCUS messages with a window hook.
However, it might also be checking for if it has focus by calling GetFocus. So to trick it in that scenario, you would need to detour GetFocus and fake the return value. You can try both of these methods and they might work and they might not, but I wouldn't expect them to work reliably.
I hooked WM_NCACTIVATE to let the game know that it is still in focus. It worked fine on two games I worked.
I just wanted to know, if there is someway to make a program (or part of a program) intangable with c#. I want to make it so that people can see the program is there, but if they were to click, it would click whatever is underneath it. I would also like to know if you can do that backwords. Is there someway to make an item that is invisable, clickable?
Thank you for your help!
To your vague question, I offer a vague response:
Sounds like your option one is possible. You would need to send the click event (message) that you receive to the appropriate window (the one underneath yours). I suspect that you would have to DllImport some stuff to do this from c#.
Your option two, while more difficult, is probably also possible because you can inject code into other running executables. This will be a privileged operation, and you will likely again have to use stuff from non .NET dlls to do it from c#. See Three Ways to Inject Your Code into Another Process on CodeProject.
If you want to display something to a user without it getting in the way of whatever it was they were doing at the time you could pop up your messages in bubble from the task bar perhaps?
The answer to this question covers this. Or if you're lazy here's the code project link.
Ok so it sometimes might be necessary to show something on screen and not let it be clickable (like On-Screen-Display for video playback to show volume increase, etc..)
Here's an example of how to this in C# - from codeproject: http://www.codeproject.com/KB/cs/OSDwindow.aspx
This uses the Win32 API ShowWindow(hWnd, SW_SHOWNOACTIVATE) to present the window without losing focus (can't be selected).
And here's the MSDN page for this call ShowWindow
To display a window that is invisible but clickable you can use a window with no border (FormBorderStyle=None) and set transparency to 1%.
Hope that helps!
I'm programming an application consisting of three usercontrols in an main window.
In one of the usercontrols, there's a slider that needs to be controllable by keyboard input. The left arrow should decrease value, right button increase and so on. I have this work, but only when the slider has focus. If some other control has focus, I cant make it work at all.
Is it possible to define "global" hotkeys? IE keys that trigger the same event or function, no matter where the focus is? Hope I've made myself clear enough...
I have never tried this but If you have a command registered at the main window level with keys associated to it that might work. Keep in mind I have never done this but it is some thing you can try. If you are new to commands here is a blog post about it.
I have never rolled this my self but when using the built in past command I actually had to put code in to prevent it from happening in some cases.
I know this probably isn't much help but I hope it is enough to get you started.