Monthly Archives: November 2008

Draw on your desktop

 

On a lonely sunday, was just trying to create another prank that would lead my friends to yell and cry at me…

So, started playing with the native window APIs

I got these info from MSDN Library under win32 API reference (available on msdn.com).

The method was supposed to return handle to window having title(string) passed to the function.

But, with some curiosity, I tried to get desktop, via this function, and It happened to work as follows

int hwn = FindWindow(null,"Desktop");

(later out, I found a direct function, for getting Desktop window -> int hwn = GetDesktopWindow()   )

once you have a handle to a window, you can create a Graphic context to that window by using a call to static function

Graphics G = Graphics.FromHwnd((IntPtr) hwn);

Here is the way to declare these functions inyour .NET code

Technorati Tags: ,,

you need to include namespace System.Runtime.InteropServices

[DllImport("user32")]
public static extern int FindWindow(string A, String B);

[DllImport("user32")]
public static extern int MoveWindow(int hwnd, int x, int y, int b, int h, int BRepaint);

[DllImport("user32")]
public static extern bool SetWindowText(int hWnd, string lpString);

[DllImport("user32")]
public static extern bool AnimateWindow(int hwn, int A, int B);

[DllImport("user32")]
public static extern int GetDesktopWindow();

User32.dll is the dll (dynamic link library) in which these functions are defined.

the above technique is called as Platform Invoke (PInvoke)

here is a snapshot:

Desktop_thumb2

To download the code, go to http://sanilsingh.brinkster.net/downloads/DesktopDraw.zip