Weekly Code-a-thon

Ok so the other day Steven tagged me on a coding challenge , challenge being to write up some code that people could use / Find interesting / just about anything that had to do with coding :) . Alas i do not have the time to do this daily so i decided to do it on a weekly basis.

On a second note i did not have the time to think up super cool as him and decided to just present a project that i am currently working on.

Project Name : The Boss key / Hide Me

Overview :

So as far as i know every one reading this blog is very hard working ,but unless you are a machine constantly working can just tire out the mind extremely fast. Now how you break this constant stream of work is up to you, it could be free cell or some other random time waster.

The problem is always this , no matter how long or how hard you worked the 5 seconds that you stopped working is the time that your manager / Boss happens to pass by, no leaving free cell open is not the best thing to show anyone in management. While you could always alt-tab out or just close the application , chances are people are going to notice every time you scramble to close a application when ever some one walks past. Also if you do decide to push the application to the background chances are they will see the application in the task bar and you might as well just tell them that you had it open and did not want to show them.

What ’Hide Me’ does is create a motion sensitive area on your desktop , that when triggered will hide any application that you have open. It removes it from the desktop ,taskbar as well as the alt-tab menu. The only way to find a application once it has been hidden is to use task manager to kill the application.

The Code :

The core of the application involves making calls to windows built in methods via p/Invokes. As you will see after a bit of research its all really simple and can make for some rather interesting applications as seen here.

Components Used For Gui :

Just remember this is what i used , not the recommended or best way to do things.

FormDesign

Notification Icon On Form
FormNotificationIcon

Ok , so the picture above shows the basic layout of the form. It consists of 2 buttons to put the form into hide mode or take it off from hide mode , a text box stores the name of the application to hide, and a panel to hide the controls depending on the current mode. Also i added a notification icon component to the form so that i can minimize the form and still recover it.

I am not going to post all the code here ,only snippets that i need to discuss with regards to the flow of the application. The basic flow is described below.

1. When the activate button is clicked the following code is executed.

AppActive = true
panel1.Visible = false
I begin by setting the application to active and hiding the activation panel. The rest of the interaction with the form is done via the mouse over of the form.
2. When the mouse enter is executed the following code is executed.

if (!AppActive) return
if (CurrentStateOpen)
{
HideWindowFromUser();
CurrentStateOpen = false
}
else
{
ShowWindow();
CurrentStateOpen = true
}
I begin by checking if the AppActive has been set to active , if not it exits the method. Next the code checks if the current forms state is opened or minimized. If the form is open then it hides it and sets the state to closed,if the form is closed then it opens it and sets the state to open.
3. The Hide and Show methods are basically the core of the application as they send the calls to windows.

privatevoid ShowWindow()
{
IntPtr ip = Win32.FindWindowByCaption(0, txtAppTitle.Text);
Win32.ShowWindow(ip, (intWindowShowStyle.ShowDefault);
}
The 2 calls to the windows API are wrapped by a custom class, the first line searches for window with the text the user enters into the text box and returns a pointer to the window. The next line sends a message to the window using pointer from the previous window. The type of the message is returned from the enum and is always the same. For a list of all the enum values you can google it or just look at the enum.

privatevoid HideWindowFromUser()
{
IntPtr ip = Win32.FindWindowByCaption(0, txtAppTitle.Text);
Win32.ShowWindow(ip, (intWindowShowStyle.Hide);
this.Hide();
}
The hide window basically does the same thing as above except that it uses a different message value in this case Hide.
4. The last detail for the application is the wrapper class.
///<summary>
/// Executes the find window method , this is basically used to find a windows based on the windows caption
///</summary>
///</summary>
///<param name=”ZeroOnly”></param>
///<param name=”lpWindowName”></param>
///<returns></returns>
[DllImport"user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
publicstaticextern System.IntPtr FindWindowByCaption(int ZeroOnly, string lpWindowName);
///<summary>
/// Executes the show window method , this is used to hide and show the window from the front end
///</summary>
///<param name=”hWnd”></param>
///<param name=”nCmdShow”></param>
///<returns></returns>
[DllImport"user32.dll")]
publicstaticexternbool ShowWindow(IntPtr hWnd, int nCmdShow);

These 2 Pinvoke methods make calls to windows api that are stored in the user32 dll. The wrapper class just makes it a bit easier to access the code and does not do much else.
So i am not sure how much detail i was supposed to go into here but if its not enough please let me know and i will post more info. BTW you can grab the source code and the application from my website ,www.codebreaker.co.za
As always remember suggestions and criticism is welcome.
~stalkerh

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Comments are closed.