Click here to Skip to main content
15,887,436 members
Home / Discussions / C#
   

C#

 
GeneralRe: Lock the mouse to my application Pin
osto1-May-04 13:50
osto1-May-04 13:50 
GeneralRe: Lock the mouse to my application Pin
Heath Stewart2-May-04 6:11
protectorHeath Stewart2-May-04 6:11 
GeneralWindows Messag handling in C# Pin
Vini Deep1-May-04 0:21
Vini Deep1-May-04 0:21 
GeneralRe: Windows Messag handling in C# Pin
scadaguy1-May-04 8:33
scadaguy1-May-04 8:33 
GeneralWeb Service from Java Pin
Member 1697730-Apr-04 19:35
Member 1697730-Apr-04 19:35 
GeneralRe: Web Service from Java Pin
Mazdak30-Apr-04 19:57
Mazdak30-Apr-04 19:57 
GeneralRe: Web Service from Java Pin
Heath Stewart2-May-04 6:09
protectorHeath Stewart2-May-04 6:09 
GeneralCapturing mouse movement while transparent Pin
bennyrascal30-Apr-04 19:18
bennyrascal30-Apr-04 19:18 
G'day

Have a little program that captures mouse movements and displays the form and relative mouse location on labels on the form in real time. It also allows the form to be dragged while the mouse button is pressed. There is also a rich text box which has text in it. The form's transparency key and the rich text box colour are the same so that the rich text box is transparent. There are buttons which set the rich text box's enable property to either true or false.

When the disable button is pressed, the enable property is set to false. This has the effect of allowing the mouse movement to be captured over the rich text box area.
When the enable button is pressed, the enable property is set to true. Now, mouse movement is not captured in the rich text box area.

Basically, things are working as I expect them to. Want I would like to do is to be able to drag the form while the rich text box is enabled or transparent. I know this can be done as there is a program called "DSCLOCK" which does it. It might be using a normal text box or a label. It doesn't matter to me. Just want a way to allow the area to be moved and still be transparent. Thought about trying to capture the mouse movements at a parent level (Windows). Thought that would let me see all mouse movement and then I could sort out which bits I wanted.

Does anyone know how this can be done?

Sorry about the size of this but it will be easier to see what i'm doing if you cut and paste this into a new C# project.

benny

************************************************************

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RichTextBox
{
///
/// Summary description for Form1.
///

public class Form1 : System.Windows.Forms.Form
{
private int mousePressedX, mousePressedY;
private System.Windows.Forms.RichTextBox rTxt1;
private System.Windows.Forms.Label lblMouseStatus;
private System.Windows.Forms.Label lblMouseLocation;
private System.Windows.Forms.Button btnEnable;
private System.Windows.Forms.Button btnDisable;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label lblFormLocation;
private System.Windows.Forms.GroupBox groupBox2;
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.btnEnable = new System.Windows.Forms.Button();
this.rTxt1 = new System.Windows.Forms.RichTextBox();
this.btnDisable = new System.Windows.Forms.Button();
this.lblMouseStatus = new System.Windows.Forms.Label();
this.lblMouseLocation = new System.Windows.Forms.Label();
this.btnExit = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.lblFormLocation = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// btnEnable
//
this.btnEnable.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.btnEnable.Location = new System.Drawing.Point(224, 32);
this.btnEnable.Name = "btnEnable";
this.btnEnable.TabIndex = 0;
this.btnEnable.Text = "enable";
this.btnEnable.Click += new System.EventHandler(this.btnEnable_Click);
//
// rTxt1
//
this.rTxt1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(128)));
this.rTxt1.Font = new System.Drawing.Font("Comic Sans MS", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.rTxt1.Location = new System.Drawing.Point(24, 24);
this.rTxt1.Name = "rTxt1";
this.rTxt1.Size = new System.Drawing.Size(176, 40);
this.rTxt1.TabIndex = 1;
this.rTxt1.Text = "Rich Text Box";
//
// btnDisable
//
this.btnDisable.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.btnDisable.Location = new System.Drawing.Point(320, 32);
this.btnDisable.Name = "btnDisable";
this.btnDisable.TabIndex = 2;
this.btnDisable.Text = "disable";
this.btnDisable.Click += new System.EventHandler(this.btnDisable_Click);
//
// lblMouseStatus
//
this.lblMouseStatus.Font = new System.Drawing.Font("Comic Sans MS", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblMouseStatus.Location = new System.Drawing.Point(32, 40);
this.lblMouseStatus.Name = "lblMouseStatus";
this.lblMouseStatus.Size = new System.Drawing.Size(320, 23);
this.lblMouseStatus.TabIndex = 3;
this.lblMouseStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// lblMouseLocation
//
this.lblMouseLocation.Font = new System.Drawing.Font("Comic Sans MS", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblMouseLocation.Location = new System.Drawing.Point(32, 88);
this.lblMouseLocation.Name = "lblMouseLocation";
this.lblMouseLocation.Size = new System.Drawing.Size(320, 23);
this.lblMouseLocation.TabIndex = 4;
this.lblMouseLocation.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnExit
//
this.btnExit.BackColor = System.Drawing.Color.Red;
this.btnExit.Location = new System.Drawing.Point(368, 336);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(32, 23);
this.btnExit.TabIndex = 9;
this.btnExit.Text = "Exit";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.lblFormLocation);
this.groupBox1.Font = new System.Drawing.Font("Comic Sans MS", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.groupBox1.Location = new System.Drawing.Point(24, 88);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(384, 88);
this.groupBox1.TabIndex = 10;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Form location";
//
// lblFormLocation
//
this.lblFormLocation.Font = new System.Drawing.Font("Comic Sans MS", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblFormLocation.Location = new System.Drawing.Point(32, 40);
this.lblFormLocation.Name = "lblFormLocation";
this.lblFormLocation.Size = new System.Drawing.Size(320, 23);
this.lblFormLocation.TabIndex = 11;
this.lblFormLocation.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.lblMouseStatus);
this.groupBox2.Controls.Add(this.lblMouseLocation);
this.groupBox2.Font = new System.Drawing.Font("Comic Sans MS", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.groupBox2.Location = new System.Drawing.Point(24, 184);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(384, 136);
this.groupBox2.TabIndex = 11;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Mouse";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(192)));
this.ClientSize = new System.Drawing.Size(432, 376);
this.ControlBox = false;
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnDisable);
this.Controls.Add(this.rTxt1);
this.Controls.Add(this.btnEnable);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.TransparencyKey = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(128)));
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Form1 dc = new Form1();
dc.Load+= new EventHandler(dc.DigitalClock_Load);
Application.Run(new Form1());
}

private void DigitalClock_Load(object sender, EventArgs e)
{
}

private void btnEnable_Click(object sender, System.EventArgs e)
{
rTxt1.Enabled = true;
}

private void btnDisable_Click(object sender, System.EventArgs e)
{
rTxt1.Enabled = false;
} // end of button off click

private void Form1_MouseEnter(object sender, System.EventArgs e)
{
lblMouseStatus.Text = "inside";
lblFormLocation.Text = Location.X + " " + Location.Y;
} // end of digitalClock_MouseEnter

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
int mouseX = (e.X);
int mouseY = (e.Y);
int movementX, movementY;
int currentX = Location.X;
int currentY = Location.Y;
lblMouseLocation.Text = e.X + " " + e.Y;
movementX = mouseX - mousePressedX;
movementY = mouseY - mousePressedY;

int newX = (int)(currentX + movementX);
int newY = (int)(currentY + movementY);
if (newX < 0) newX = 0;
if (newY < 0) newY = 0;

if (lblMouseStatus.Text == "pressed")
{
// This is the drag condition
Location = new Point(newX, newY);
}
lblFormLocation.Text = Location.X + " " + Location.Y;
Invalidate();
} // end of digitalClock_MouseMove

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (lblMouseStatus.Text == "inside" | lblMouseStatus.Text == "up")
{
lblMouseStatus.Text = "pressed";
lblMouseLocation.Text = e.X + " " + e.Y;
mousePressedX = (e.X);
mousePressedY = (e.Y);
}
lblFormLocation.Text = Location.X + " " + Location.Y;
} // end of digitalClock_MouseDown

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
lblMouseStatus.Text = "up"; // always unpress
lblFormLocation.Text = Location.X + " " + Location.Y;
} // end of digitalClock_MouseUp

private void Form1_MouseLeave(object sender, System.EventArgs e)
{
lblMouseStatus.Text = "outside";
lblFormLocation.Text = Location.X + " " + Location.Y;
}

private void Form1_Load(object sender, System.EventArgs e)
{
MouseEnter += new EventHandler(Form1_MouseEnter);
MouseMove += new MouseEventHandler(Form1_MouseMove);
MouseDown += new MouseEventHandler(Form1_MouseDown);
MouseUp += new MouseEventHandler(Form1_MouseUp);
MouseLeave += new EventHandler(Form1_MouseLeave);
rTxt1.Enabled = false;
this.Enabled = true;
}

private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
} // end of digitalClock_MouseLeave
}
}
GeneralRe: Capturing mouse movement while transparent Pin
Heath Stewart2-May-04 6:05
protectorHeath Stewart2-May-04 6:05 
GeneralRe: Capturing mouse movement while transparent Pin
bennyrascal2-May-04 19:39
bennyrascal2-May-04 19:39 
GeneralDocking Dilemma Pin
gUrM33T30-Apr-04 15:48
gUrM33T30-Apr-04 15:48 
GeneralRe: Docking Dilemma Pin
Mazdak30-Apr-04 18:47
Mazdak30-Apr-04 18:47 
GeneralRe: Docking Dilemma Pin
gUrM33T30-Apr-04 19:46
gUrM33T30-Apr-04 19:46 
GeneralRe: Docking Dilemma Pin
Mazdak30-Apr-04 19:55
Mazdak30-Apr-04 19:55 
GeneralRe: Docking Dilemma Pin
Heath Stewart2-May-04 6:00
protectorHeath Stewart2-May-04 6:00 
GeneralRe: Docking Dilemma Pin
gUrM33T2-May-04 7:27
gUrM33T2-May-04 7:27 
GeneralRe: Docking Dilemma Pin
Heath Stewart2-May-04 7:47
protectorHeath Stewart2-May-04 7:47 
GeneralMultimedia Keys Pin
Member 87406830-Apr-04 14:31
Member 87406830-Apr-04 14:31 
GeneralRe: Multimedia Keys Pin
Mazdak30-Apr-04 19:02
Mazdak30-Apr-04 19:02 
GeneralRe: Multimedia Keys Pin
Heath Stewart2-May-04 5:57
protectorHeath Stewart2-May-04 5:57 
GeneralRe: Multimedia Keys Pin
Member 8740682-May-04 21:09
Member 8740682-May-04 21:09 
Questionremoting : object reference not set to an instance of an object ??? Pin
youssef30-Apr-04 13:45
youssef30-Apr-04 13:45 
AnswerRe: remoting : object reference not set to an instance of an object ??? Pin
Heath Stewart30-Apr-04 13:54
protectorHeath Stewart30-Apr-04 13:54 
GeneralRe: remoting : object reference not set to an instance of an object ??? Pin
youssef30-Apr-04 14:06
youssef30-Apr-04 14:06 
GeneralRe: remoting : object reference not set to an instance of an object ??? Pin
Heath Stewart30-Apr-04 14:13
protectorHeath Stewart30-Apr-04 14:13 

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.