hey there, i've been messing around with applying graphics to windows of other applications, using their window handles.. (if that's even possible..).
here's my code so far:

        public static extern IntPtr GetForegroundWindow();
        public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);  
        public Graphics g;
        public IntPtr window = IntPtr.Zero; //the main handle holder
        public IntPtr[] hwnd = new IntPtr[100]; //a temporary handle holder.. gave it a 100 cuz u can't know how many handles u get..
        public static StringBuilder str = new StringBuilder("",100); //what da heck.. give it a 100 ;)
        public Form1()

        private void button2_Click(object sender, EventArgs e)
            if (window != IntPtr.Zero)
                g = Graphics.FromHwnd(window);
                g.DrawRectangle(new Pen(Color.Red, 1.8f), ClientRectangle.Width / 2, this.ClientRectangle.Height / 2, 10, 10);
            else //a small debug..
                window = this.Handle;
                Graphics g = Graphics.FromHwnd(window);
                g.DrawLine(new Pen(Color.Red, 1), 0,this.Height,this.Width,0);
                g.DrawLine(new Pen(Color.Red, 1), 0, 0, this.Height, this.Width);                

        private void button1_Click(object sender, EventArgs e)

        private IntPtr FindHwnd()
            Array.Clear(hwnd,0, hwnd.Length);
            window = GetWindow(GetForegroundWindow(), 0);            
            while (window != IntPtr.Zero)
               // str.Insert(0, 0);
                GetWindowText(window, str, 100);
                //str.Insert(100, 0);  i canceled these 2 cuz it gave me an out of memory exception..
                if (str.Length != 0)
                hwnd[listBox1.Items.Count] = window;
                window = GetWindow(window, 2);                  
            return window;

        private void listBox1_DoubleClick(object sender, EventArgs e)
            window = hwnd[listBox1.SelectedIndex];

to sum it up: u click on 1 button, and the listbox fills up with the window handles FindHwnd picked up.. the only problem i have is that i can't draw on them... can't tell why.. oddly enough, i did manage to draw at the button2_Click when i gave "window" the value of [this.handle] (the debug part in the drawing section).. so something must have gone wrong when i picked up or passed on the handles... if any1 can shed some light on this, i will appreciate it allot, thanks!
oh btw.. this is the first program i tried to write that actually does something so... excuse any improper use of w.e i put there... kina hard to figure it all out from the msdn.. no realy, try it ;)
oh yeah, and this is my first post on a forum ever so.. sorry if i did something wrong here...

Edited by suberi: n/a

7 Years
Discussion Span
Last Post by suberi

well ddanbe helps you in this question......:)

ddanbe is a user here right? can u please show me where did he answer this?

Edited by suberi: n/a


ok i solved this guys... the problem was the line

if (str.length != 0)

this line ignored the window handle and gave me another handle (probebly the title bar..), i played with the code abit and now it works!
thanks anyways~

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.