Click here to Skip to main content
15,895,142 members
Home / Discussions / C#
   

C#

 
AnswerRe: embed .net component to program Pin
EliottA8-Dec-08 11:28
EliottA8-Dec-08 11:28 
AnswerRe: embed .net component to program Pin
Giorgi Dalakishvili8-Dec-08 19:26
mentorGiorgi Dalakishvili8-Dec-08 19:26 
AnswerRe: embed .net component to program Pin
Lev Danielyan8-Dec-08 19:55
Lev Danielyan8-Dec-08 19:55 
QuestionHow to update my datagrid using text boxes? Pin
Vloops8-Dec-08 11:11
Vloops8-Dec-08 11:11 
AnswerRe: How to update my datagrid using text boxes? Pin
Alireza Loghmani8-Dec-08 22:40
Alireza Loghmani8-Dec-08 22:40 
QuestionThrottling Socket.Send Pin
davidhart8-Dec-08 10:13
davidhart8-Dec-08 10:13 
AnswerRe: Throttling Socket.Send Pin
Jimmanuel8-Dec-08 11:19
Jimmanuel8-Dec-08 11:19 
QuestionDrawing a window overlay Pin
xxxsamixxx8-Dec-08 10:08
xxxsamixxx8-Dec-08 10:08 
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.

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));
    }
  }
}

QuestionMeasuring reaction time of the user. Pin
Jakob Olsen8-Dec-08 9:49
Jakob Olsen8-Dec-08 9:49 
AnswerRe: Measuring reaction time of the user. Pin
User 66588-Dec-08 9:57
User 66588-Dec-08 9:57 
GeneralRe: Measuring reaction time of the user. Pin
Jakob Olsen8-Dec-08 10:20
Jakob Olsen8-Dec-08 10:20 
QuestionAdd image to spreadsheetml document Pin
GibbleCH8-Dec-08 9:48
GibbleCH8-Dec-08 9:48 
QuestionAcquiring a thread. Pin
Member 23244838-Dec-08 9:22
Member 23244838-Dec-08 9:22 
AnswerRe: Acquiring a thread. Pin
Christian Graus8-Dec-08 9:45
protectorChristian Graus8-Dec-08 9:45 
GeneralRe: Acquiring a thread. Pin
Member 23244838-Dec-08 10:20
Member 23244838-Dec-08 10:20 
GeneralRe: Acquiring a thread. Pin
N a v a n e e t h8-Dec-08 16:33
N a v a n e e t h8-Dec-08 16:33 
QuestionComunication between form an appl. and a service. Pin
daavena8-Dec-08 9:07
daavena8-Dec-08 9:07 
AnswerRe: Comunication between form an appl. and a service. Pin
Giorgi Dalakishvili8-Dec-08 9:16
mentorGiorgi Dalakishvili8-Dec-08 9:16 
GeneralRe: Comunication between form an appl. and a service. Pin
daavena8-Dec-08 9:28
daavena8-Dec-08 9:28 
GeneralRe: Comunication between form an appl. and a service. Pin
Giorgi Dalakishvili8-Dec-08 19:19
mentorGiorgi Dalakishvili8-Dec-08 19:19 
AnswerRe: Comunication between form an appl. and a service. Pin
PIEBALDconsult8-Dec-08 12:45
mvePIEBALDconsult8-Dec-08 12:45 
QuestionEmbedding Excel in Windows Forms Pin
San248-Dec-08 8:11
San248-Dec-08 8:11 
AnswerRe: Embedding Excel in Windows Forms Pin
Giorgi Dalakishvili8-Dec-08 8:27
mentorGiorgi Dalakishvili8-Dec-08 8:27 
GeneralRe: Embedding Excel in Windows Forms Pin
San248-Dec-08 9:10
San248-Dec-08 9:10 
GeneralRe: Embedding Excel in Windows Forms Pin
Giorgi Dalakishvili8-Dec-08 9:15
mentorGiorgi Dalakishvili8-Dec-08 9:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.