Click here to Skip to main content
15,892,298 members
Home / Discussions / C#
   

C#

 
GeneralRe: newbie in c# Pin
J4amieC16-Dec-05 0:25
J4amieC16-Dec-05 0:25 
GeneralRe: newbie in c# Pin
yaminilathatv16-Dec-05 21:55
yaminilathatv16-Dec-05 21:55 
QuestionSchema Pin
Mahi.Ragava15-Dec-05 18:00
Mahi.Ragava15-Dec-05 18:00 
QuestionCheckbox Arrays... Pin
flicktom15-Dec-05 17:48
flicktom15-Dec-05 17:48 
AnswerRe: Checkbox Arrays... Pin
flicktom15-Dec-05 18:26
flicktom15-Dec-05 18:26 
AnswerRe: Checkbox Arrays... Pin
J. Dunlap15-Dec-05 18:26
J. Dunlap15-Dec-05 18:26 
QuestionHit testing problem Pin
t800t815-Dec-05 15:44
t800t815-Dec-05 15:44 
AnswerRe: Hit testing problem Pin
Heath Stewart15-Dec-05 21:35
protectorHeath Stewart15-Dec-05 21:35 
Windows - including controls (which are client windows) - are only sent messages such as mouse move messages when the mouse is over them or the mouse is captured (I'll get to that in a second). Forms that parent controls such as your label can receive mouse events for their children, which actually happens anyway since the parent control will dispatch messages to their children. This is why the label only knows about mouse movement within its client region.

In order to be notified when the mouse moves outside of the control you need to capture the mouse. See http://msdn2.microsoft.com/en-us/library/ms171545(en-US,VS.80).aspx[^] for general information, making note of the following:
Only the foreground window can capture the mouse. When a background window attempts to capture the mouse, the window receives messages only for mouse events that occur when the mouse pointer is within the visible portion of the window. Also, even if the foreground window has captured the mouse, the user can still click another window, bringing it to the foreground. When the mouse is captured, shortcut keys do not work.
Try the following sample:
    public class Form1 : Form
    {
       private System.Windows.Forms.Label label1;
       private System.Windows.Forms.CheckBox checkBox1;
 
       public Form1()
        {
            InitializeComponent();
        }
 
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label1.Location = new System.Drawing.Point(33, 98);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(221, 33);
            this.label1.TabIndex = 0;
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.label1.MouseLeave += new System.EventHandler(this.label1_MouseLeave);
            this.label1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.label1_MouseMove);
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(36, 135);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(97, 17);
            this.checkBox1.TabIndex = 1;
            this.checkBox1.Text = "Capture mouse";
            this.checkBox1.UseVisualStyleBackColor = true;
            this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            label1.Text = e.Location.ToString();
        }
 
        private void label1_MouseLeave(object sender, EventArgs e)
        {
            if (!label1.Capture)
            {
                label1.Text = string.Empty;
            }
        }
 
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            label1.Capture = checkBox1.Checked;
        }
 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
 


This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft

[My Articles] [My Blog]
GeneralRe: Hit testing problem Pin
t800t815-Dec-05 22:01
t800t815-Dec-05 22:01 
Question[URGENT] REGEX question Pin
Sam 200615-Dec-05 15:20
Sam 200615-Dec-05 15:20 
AnswerRe: [URGENT] REGEX question Pin
Christian Graus15-Dec-05 16:12
protectorChristian Graus15-Dec-05 16:12 
Questionopen an executable file on remote machine using WMI Pin
Erick Shuai15-Dec-05 15:12
Erick Shuai15-Dec-05 15:12 
AnswerRe: open an executable file on remote machine using WMI Pin
Dave Kreskowiak15-Dec-05 16:22
mveDave Kreskowiak15-Dec-05 16:22 
QuestionExists an interface for Parse(string value) method and ToString(string format)? Pin
machocr15-Dec-05 14:21
machocr15-Dec-05 14:21 
AnswerRe: Exists an interface for Parse(string value) method and ToString(string format)? Pin
[Marc]15-Dec-05 14:46
[Marc]15-Dec-05 14:46 
GeneralRe: Exists an interface for Parse(string value) method and ToString(string format)? Pin
machocr15-Dec-05 17:31
machocr15-Dec-05 17:31 
GeneralRe: Exists an interface for Parse(string value) method and ToString(string format)? Pin
S. Senthil Kumar15-Dec-05 20:45
S. Senthil Kumar15-Dec-05 20:45 
GeneralRe: Exists an interface for Parse(string value) method and ToString(string format)? Pin
machocr16-Dec-05 10:47
machocr16-Dec-05 10:47 
QuestionATTACHING DATABASE FILE Pin
Bagie15-Dec-05 14:08
Bagie15-Dec-05 14:08 
AnswerRe: ATTACHING DATABASE FILE Pin
Curtis Schlak.15-Dec-05 15:21
Curtis Schlak.15-Dec-05 15:21 
GeneralRe: ATTACHING DATABASE FILE Pin
Bagie16-Dec-05 12:52
Bagie16-Dec-05 12:52 
GeneralRe: ATTACHING DATABASE FILE Pin
Curtis Schlak.17-Dec-05 4:34
Curtis Schlak.17-Dec-05 4:34 
GeneralRe: ATTACHING DATABASE FILE Pin
Bagie17-Dec-05 11:12
Bagie17-Dec-05 11:12 
GeneralRe: ATTACHING DATABASE FILE Pin
Curtis Schlak.19-Dec-05 18:06
Curtis Schlak.19-Dec-05 18:06 
QuestionFrom Bagie [ Important ] Pin
Bagie15-Dec-05 13:30
Bagie15-Dec-05 13:30 

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.