|
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
}
}
|
|
|
|
|
Take a look at the Control.Capture property. You could capture the mouse and receive notification messages while another control - or even another application, so make sure you use Control.PointToClient to convert the coordinates relative to your control - has the mouse over it.
I don't know anything about DSCLOCK, but I'm guessing it uses system hooks, which is another way to do it, but should be a last resort - especially if this is just necessary in your application.
That said, you can use an IMessageFilter which acts as an application hook, passing all messages that get posted to your message pump (queue) through it giving you a chance to process. See the documentation for this interface, as well as the Application.AddMessageFilter method document in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath
Thanks again. Your comments are sufficient for me to sort this out now.
benny
|
|
|
|
|
Hi guys, I've two panels on my WinForm, one is docked to the left of the container and the other at the bottom. Assume that there are no more controls on the form. Now sometimes these panels are docked to the container (i.e. a WinForm) as
+-----------+
| |
| panelLeft +----------------------+
| | panelBottom |
+-----------+----------------------+
and sometimes as
+-----------+
| |
| panelLeft |
| |
+-----------+----------------------+
| panelBottom |
+----------------------------------+
I want to know why? Isn't there any way of making it sure as how these Panels will be docked in RELEASE version.
Plz do reply,
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
I think you have to go to your InitializeComponent() and at the end of it see which one added to Control propoerty of your form.
Mazy
"One who dives deep gets the pearls,the burning desire for realization brings the goal nearer." - Babuji
|
|
|
|
|
Both of those panel controls are being added to the Controls property of the form in InitializeComponent() . Whatz the point by the way?
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
Gurmeet S. Kochar wrote:
Both of those panel controls are being added to the Controls property of the form in InitializeComponent(). Whatz the point by the way?
I know both added. I mean which one first added and which one is second. This will show which one is at the top of other one.
Mazy
"One who dives deep gets the pearls,the burning desire for realization brings the goal nearer." - Babuji
|
|
|
|
|
Adding to what Mazdak said, if you see Controls.AddRange the controls are actually added in reverse order from what you see listed, like:
Controls.AddRange(new Control[]
{
myControl3,
myControl2,
myControl1
});
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
the controls are actually added in reverse order
So what you are saying is that myControl1 will get added first, then myControl2 and then myControl3 ? How this all reverse order adding affects docking?
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
Yes. The docked controls are docked in relation to each other in the order in which they were added.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Does anybody know how to accept commands given by the multimedia keys? (You know play/pause/next/back/stop/etc)
Would be really greatful if anybody could indicate how they work, or where I could get more info... have looked everywhere I can think of!
Many Thanks
|
|
|
|
|
|
Mazdak's link is a good place to look. These typically use the OEM keys, but you're going to have one problem: the keyboard driver (or some other program) uses a system hook to intercept these keys before you'd have a chance to process them, in most cases.
Something to keep in mind.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
I create a client remoting.
I set a method with the frontcontroller from a client.
When I executing the method this message appear :
"object reference not set to an instance of an object "
Best regards
youssef
|
|
|
|
|
That means you're trying to invoke a member on a null reference. This could be practically any problem, like not setting a variable to an instance of a class, the remote instance is not being created and proxied to your client, and the list goes on and on. The best way to find the problem is to debug your code and find where the NullReferenceException occurs.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I put some breakpoint.
But when I run in debug mode, an error occur the file.config doesn't exist.
Can you help me how can I do for execute the software in debug mode in remoting
Best regards
youssef
|
|
|
|
|
If you're file doesn't exist, then either you hap-hazardly hard-coded the file path (never a good idea) or it doesn't exist in the directory you think it should.
Typically, in .NET Remoting you just use the application's .config file (in the same directory as the application with the same name + .config appended, like an app called MyApp.exe would have a file called MyApp.exe.config in the same directory) and then you'd call RemotingConfiguration.Configure passing the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile so that you can keep everything in the same .config file. This is true for both the client and server. If you don't want to do it this way, then you need to code your app so that it can find the file no matter where it is, like using Application.StartupPath in the client application to use the directory where the .exe is located, and then use Path.Combine to combine that with file.config using the platform-dependent directory separator character.
To debug your application, just click the Debug->Start menu. There is more information in the Visual Studio help.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ok thanks a lot.
The error is a nullexecption in my client.
Now all are working.
Best Regards
youssef
|
|
|
|
|
How can I make it so that only one instance of my application is allowed to be run at a time. For example, Windows Media Player will only open once.
|
|
|
|
|
You can use a Mutex to limit your application to only one instances like so:
public class SingleInstanceApp
{
private const string UniqueId = "SingleInstanceApp_Mutex";
static void Main()
{
Mutex m = new Mutex(false, UniqueId);
if (m.WaitOne(0, true))
{
Console.WriteLine("I'm the first instance!");
Thread.Sleep(10000);
}
else
{
Console.WriteLine("I'm not the first instance. :(");
}
}
} If you want to, for example, open a document passed to the command line of the second (or later) instance, you'll need to use inter-process communications. In .NET, the preferred way is using .NET Remoting.
There is a good article here on CodeProject about this entitled Single Instance Application in VB.NET
[^]. It's in VB.NET but if you truly understand the .NET Framework, it should be no problem to translate. Supposedly, there are a few implementations based on this article in C# but the articles explain almost nothing. I guess you're supposed to just look at their source, which I didn't feel inclined to do.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the response Heath.
|
|
|
|
|
Heath Stewart wrote:
Console.WriteLine("I'm not the first instance. ");
You realize the unimpressed face won't show up in the console??
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hello,
I want the tab label to change appearance when the mouse passes over it.
I tried using OnMouseEnter of the tab control and I can get the appropriate tab label to change based on GetTabRect and the mouse position, but after the tab changes, since the mouse is still in the visible portion of the tab control, I cannot do the same for another tab unless I explicitly leave the tab control with the mouse and enter on top of the next tab.
I want to be able to run my mouse across all of the tab labels and have them, for example, turn red only when the mouse is over them.
Any suggestions?
Thanx,
-Flack
|
|
|
|
|
Tab controls support hot tracking (TCS_HOTTRACK style) by default, but that will turn a tab label to a blue color instead of a red.
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|