Send window to back when activated - c#

I found the answer to this question in here.
that solution works great when you call the method just once (don't call it from the event window activated). I am creating a gadget and I want the window to always be in the back. if the user clicks on the gadget I don't want it to come to the front which is the default behavior of windows. so what I did in order to solve that problem was to create this event:
private void Window_Activated(object sender, EventArgs e)
{
SendWpfWindowBack(this);
}
so every time the window is activate it call the method founded on the first link that I provided. the problem is that the system tray gets sent to the back. let me show you what I mean.
that is my gadget on visual studio.
when I run it it get's sent to the back: (I open notepad and outlook to illustrate my point)
now when I click on it the event window_Activated fires and it gets send to the back:
But note how the system tray (windows start up menu window) get's sent to the back!!!!
it still is behind everything exept the most important window wich is the windows start up menu. How can I resolve this. why is it going in front of the system tray !?

After 2 hours of stubbornness I figured it out... Since the window was so big for some reason that happen (gadget was bigger than entire desktop). I made it smaller and it worked.

Related

Windows Phone 8 show MessageBox when app closing (deactivated)

I am trying to show user MessageBox just before app is closed (or deactivated). So I added this to both events:
if (AppSettings.ShouldShowAlertTransfer)
{
MessageBox.Show("...");
}
But the MessageBox is not shown. I am adding this because then I am adding background transfers and I must inform user that I created some background trasnfers. I know I can add this code to OnKeyBackPressed in every page. But it's working just for back key and not middle button. And it wouldn't be nice to have same code copied in every page.
So is it possible to show this MessageBox in App.xaml.cs? How can I do that? If it's not how can I show MessageBox when user pressed middle button?
I was intrigued by your question so I opened up a solution and attempted every single override available and every single page event. I also tried all of the methods in the App.xaml.cs.
I can unequivocally say, based on the MSDN documentation and my own personal testing that there is no way to detect the home button press or search button press. Furthermore there is no way to display a message box to temporarily stop one of these actions.
This testing was done on Windows Phone 8 with the WVGA 512MB Emulator in Visual Studio 2013 Ultimate.

UAC Window On Top

I seem to be running into a dead end here. I have a project that does the following:
just runs and stays in notification area
when user presses middle mouse button, it shows a form
user can then chose whatever from the form, and be happy.
This is working just fine, however what is NOT working is that when the form is shown, it does NOT activate.
Now, prior to me modifying the app.manifest to requireAdmin, it was working fine. Any time the form was shown, boom it was active.
But now that i'm running the form as Administrator, given that I need to be able to control one of our other applications with it (that is ran as admin as well), the form comes up just fine, but doesn't activate.
I have tried:
- TopMost = true inside of the Form_Load method
- Calling SetForegroundWindow with the forms handle on Form_Load
- Calling ShowDialog both on Form_Load and also tried it when the form was built.
- Calling Activate on Form_Load as well as when the form was built
Here's my layout:
Program runs, requires UAC permissions due to the manifest, user agrees, and it kicks off
Only a notification icon appears, shows a nice little bubble saying it's running
Program.cs monitors the keyboard/mouse hooks
When middle mouse button, or any of the keyboard hooks are triggered, it creates a new form object
sets the location of where the form should appear
and then finally shows the form.
Again, without UAC and requireAdmin inside of the app.manifest, this works fine. But once it is running as admin, nope, wont stay in front.
I know that MS has made the SetForegroundWindow requirements much stricter with later versions of windows (Vista, 7, 8), but i really need to be able to make this form show as the top most, active window (like a context menu).
How can i do that properly?
You could split your program into two, a non-admin requiring half that sits in the tray and watches for the middle button, and the half that requires admin permission that you launch when the mouse button is pressed. The non-elevated half could then call SetForegroundWindow or AllowSetForegroundWindow as needed.
If you want to avoid a UAC prompt every time you can cache a COM elevation object via the CoCreateInstanceAsAdmin method and use it repeatedly.

Winforms Notification Icon duplicates in the System Tray

I am working on a WinForms application and have to show the Application Icon(Notification Icon) in the System tray. Things working fine. But when I go to a sub page from the main page and come back and check the system tray It has 2 notification Items in the system tray. Again When I go to another page and see, another duplicate Icon appears in the system tray. Like that the number of notification icons gets increase when i go to another page and come back.
If someone can Guide me where I have gone wrong, really appreciate. Thanks in advance...
Since you're not giving any code, I'm gonna go on hunches and lucky guesses:
The logic you implemented to generate a notification icon gets executed on an event that occurs when you change page in your app. (Likely)
There is a chipmunk that lives inside your computer, and he felt like monday is messing-with-da-master day. (Not so likely)

Buffer button changing forms

this is my first post. I have a huge problem which make me headaches. I have an app uses WinForms, a TTS (Text-To-Speech) voice and custom-buttons with states.
In my 1st form -main- when I click a button, the app opens a 2nd form above the 1st. Ok.
When I close the 2nd form trough a button, I tell the TTS say something and the form closed itself, viewing again the 1st form. Ok.
The problem starts when I click two times in the button on the 2nd form: the TTS says something, the button closes and the 'second click' is still in the click buffer (or somewhere) and it makes click in the 1st form (which appears 4 seconds later when I hit the button for the first time).
I am using the voice in a Sync mode; if I use the voice in an Async mode, the application ends wit h a nice exception.
If I click three or four times in the 2nd form, the other clicks still remains in buffer and clicks in the 1st form all the times.
I tried to (1) delete the DoubleClick event, (2) delete the event associated to the button and (3) hide the button which is clicked automatically when I return from the 2nd form, (4) hide the 1st form before create the 2nd and restore when it finishes.
Suggestions?
Thanks!
PD: I'm sorry by my English :S
PD2: I've uploaded a very simple example of what happens.
EDIT 2
Having looked at the code I understand the issue you're having now. The reason button clicks are being stacked up is when you call Speak within TTS the application locks up while it waits for the function to finish. Any presses in that time are stacked up until the application is free again to process them, you then close the form instanly before the messages are handled and these are then dealt with in the first form.
I've come up with a few solutions which could work for you:
Use only the SpeakAsync command within your TTS class and introduce a Waiting system where you wait for the speech to finish before doing anything. This will free the application and won't cause the mouse click events to stack up.
After you trigger a Speak command you could access the Windows message list and clear all the mouse click events that occurred before the process finished. Unfortunately, I'm not sure how you'd implement this as I've not done this before. I think you need to overwrite the WndProc function but again I'm not sure. This might be also be a bit dangerous as you may end up clearing a perfectly valid or important system message by mistake. Sorry can't provide any more help on that one.
Implement a background worker in your second form which will process the Speak commands seperately on a background thread. This again will free the application so the mouse click events won't stack up. I've modified your sample project and zipped it up for you to take a look. If you want I can explain further but essentially it does the following:
Form 2 loads and creates a background worker.
Worker_DoWork and Worker_WorkComplete delegates are created and set in the background worker. These functions are called when the worker is started and after the worker has finished.
Form 2 triggers the background worker to start. The background worker then sits in an infinite loop waiting for commands to process.
When the "Hello" button is pressed this sets a SayHello boolean to true, the worker spots this, carrys out the appropriate speak function and then resets the boolean ready for the next press.
When the "Close" button is pressed a CancelASync request is called in the background worker.
CancelASync interupts the BackgroundWorker's main loop (CancellationPending becomes true). The appropriate speak command is sent and the cancel property of the DoWorkEventArgs is set to true before breaking out of the BackgroundWorker's main loop.
Breaking out of the main loop causes Worker_WorkComplete to be called where the form is then closed.
I hope you can follow the example (linked below) and I've explained it well enough here. I prefer this solution as its quite extendable, you can add more conditions within the main worker thread for example.
Like I said, if you have any questions please ask and I'll try help as much as possible.
Hope this helps.
Example Link: http://www.mediafire.com/?2mf1yahto50ljs6
Use a boolean flag to track whether the form is in a state that accepts the click.
IE - when you open the 2nd form, 'boolean canPlaySound = true;' When the button click event fires, only play the sound if canPlaySound is true (and set it to false before playing the sound).
The next click will be ignored because canPlaySound = false. You won't play the sound.

Disappearing System Tray icons

Im creating a system tray application in visual studio 2010, using C#.
When the application starts i create my thread and a system tray icon. THe icon shows, however whenever i mouse over the icon, it disappears ( the application is still running ), and even if i click the button to show all hidden icons, it doesnt display.
However, if i dont try to mouse over on it, then it stays their in the system tray.
Any Thoughts or experience?
Thanks in advance
Thanks for the answers guys.
Uhh, something i did to fix before so although for those who are perhaps curious.
I initially wasnt using a windows form, and this is when the problem occured. However when i set my app to be a windows form, and just hide the form, and not show it in the taskbar, it worked.
Paste this code into your form class:
protected override void OnFormClosing(FormClosingEventArgs e) {
notifyIcon1.Visible = false;
base.OnFormClosing(e);
}
This ensures the icon will disappear without lingering in the tray. Now set a breakpoint on that code and find out why your form is closing. Copy and paste the stack trace into your question if you cannot figure out why.
This means that tray icon has been removed. That usually happens after process terminates but the tray stays there - it is a windows bug.
So for some reason, your tray icon perhaps "crashes".
Without seeing your code, it would be impossible to comment any further.
If you are creating the icon object and letting it go out of scope without any reference to it, the next garbage collection will call it's destructor and this will happens.
When the Windows Explorer restarts ,windows will clear all the icons in the notification area and sends a broadcast message TaskbarCreated .One has to use the message to add the notification icon again .
You can use the following code to listen to the event :
UINT WM_TaskBarCreated = ::RegisterWindowMessage(L"TaskbarCreated");
and the use windowproc or MessageHandler to add the icon back in the notifiation area.

Categories