CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2008
    Posts
    1

    Drawing a window overlay

    I'm trying to write a program, that displays some auxiliary information on top of a 3rd party
    application. For now, I'm just trying to draw a transparent window, that lets keypresses and mouse events through to the 3rd party application, and draws a red border around its client area.

    ..but instead of doing that, what happens is that only small part of the upper left corner of my auxiliary screen gets drawn, while rest of the area stays black. The reason for this seems to be that the ClipRectangle in the Paint doesn't cover the whole area of my auxiliary window.

    I've tried to invalidate the whole area by explicitly giving the window size, etc, but nothing seems to help. Please, can anyone see what is wrong and help me fix this? Thanks!

    I've attached the code. Just replace the hwnd in the Main with a hwnd of an existing window to try it out.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
     
     
    namespace Overlay {
      public class OverlayForm : Form {
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
          public int left;
          public int top;
          public int right;
          public int bottom;
        }
     
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(HandleRef hwnd, out RECT lpRect);
     
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetClientRect(HandleRef hwnd, out RECT lpRect);
     
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(HandleRef childWwnd, HandleRef newParentHwnd);
     
        private IntPtr parentHwnd;
     
        public OverlayForm(IntPtr parentHwnd) {
          this.parentHwnd = parentHwnd;
          this.Paint += new PaintEventHandler(OverlayForm_Paint);
     
          this.SuspendLayout();
          this.BackColor = System.Drawing.Color.Lime;
          this.FormBorderStyle = FormBorderStyle.None;
          this.TransparencyKey = Color.Lime;
          this.StartPosition = FormStartPosition.Manual;
          SetParent(new HandleRef(null, this.Handle), new HandleRef(null, parentHwnd));
          adjustSize();
          this.ResumeLayout(false);
          this.PerformLayout();
     
          Thread adjustThread = new Thread(adjustSizeContinuous);
          adjustThread.Start();
        }
     
        private void adjustSizeContinuous() {
          while(true) {
            this.SuspendLayout();
            adjustSize();
            this.ResumeLayout(false);
            this.PerformLayout();
     
            Thread.Sleep(500);
          }
        }
     
        private void adjustSize() {
          RECT windowRect = new RECT();
          RECT clientRect = new RECT();
          GetWindowRect(new HandleRef(null, parentHwnd), out windowRect);
          GetClientRect(new HandleRef(null, parentHwnd), out clientRect);
     
          this.Location = new Point(windowRect.left, windowRect.top);
          this.Size = new Size(clientRect.right, clientRect.bottom);
          Refresh();
        }
     
        private void OverlayForm_Paint(object sender, PaintEventArgs e) {
          Console.WriteLine("  cr = " + e.ClipRectangle);
     
          Graphics g = e.Graphics;
          g.Clear(Color.Lime);
          Pen pen = new Pen(Color.Red, 10);
          g.DrawRectangle(pen, 0, 0, ClientSize.Width, ClientSize.Height);
          g.Dispose();
        }
     
        public static void Main() {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          IntPtr hwnd = new IntPtr(0x00D3024A);
          Application.Run(new OverlayForm(hwnd));
        }
      }
    }
    Last edited by xasmx; December 8th, 2008 at 06:28 PM.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Drawing a window overlay

    Could you wrap the coide in code tags? My eyes hurt...

    [ code] code goes here [/ code] (no spaces though).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured