Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:57:35+00:00 2026-06-14T11:57:35+00:00

The WPF program below puts up a window which looks like this: Mouse-movement outside

  • 0

The WPF program below puts up a window which looks like this:

enter image description here

Mouse-movement outside the black square causes the window title to be updated with the mouse’s position. The updating stops when the mouse enters the square.

I’d like for MouseMove to continue to trigger even when the mouse is over the square. Is there a way to do this?

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Wpf_Particle_Demo
{
    class DrawingVisualElement : FrameworkElement
    {
        public DrawingVisual visual;

        public DrawingVisualElement() { visual = new DrawingVisual(); }

        protected override int VisualChildrenCount { get { return 1; } }

        protected override Visual GetVisualChild(int index) { return visual; }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var canvas = new Canvas();

            Content = canvas;

            var element = new DrawingVisualElement();

            canvas.Children.Add(element);

            CompositionTarget.Rendering += (s, e) =>
                {
                    using (var dc = element.visual.RenderOpen())
                        dc.DrawRectangle(Brushes.Black, null, new Rect(0, 0, 50, 50));
                };

            MouseMove += (s, e) => Title = e.GetPosition(canvas).ToString();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:57:36+00:00Added an answer on June 14, 2026 at 11:57 am

    You will need to Capture the mouse this will allow your Canvas to continue to respond to the MouseMove Event, Try something like this it will update your coordinates as long as the Mouse is Pressed

    public partial class MainWindow : Window
    {
    
        public MainWindow()
        {
            InitializeComponent();
    
    
            var canvas = new Canvas();
            Content = canvas;
    
            var element = new DrawingVisualElement();
    
            canvas.Children.Add(element);
            CompositionTarget.Rendering += (s, e) =>
            {
                using (var dc = element.visual.RenderOpen())
                    dc.DrawRectangle(Brushes.Black, null, new Rect(0, 0, 50, 50));
            };
    
            Mouse.Capture(canvas);
            MouseDown += (s, e) => Mouse.Capture((UIElement)s);
            MouseMove += (s, e) => Title = e.GetPosition(canvas).ToString();
            MouseUp += (s, e) => Mouse.Capture(null);
    
        }
    

    Second Method

    public MainWindow()
    {
        InitializeComponent();
        var canvas = new Canvas();
        Content = canvas;
    
        DrawingVisualElement element = new DrawingVisualElement();
        Grid myElement = new Grid();
        canvas.Children.Add(myElement);
    
        CompositionTarget.Rendering += (s, e) =>
        {
            using (var dc = element.visual.RenderOpen())
            {
                dc.DrawRectangle(Brushes.Black, null, new Rect(100, 0, 50, 50));
            }
    
            DrawingImage myImage = new DrawingImage(element.visual.Drawing);
            myElement.Height = myImage.Height;
            myElement.Width = myImage.Width;
            myElement.Background = new ImageBrush(myImage);
        };
    
    
        MouseMove += (s, e) => Title = e.GetPosition(canvas).ToString();
    }
    

    Using a Hook be sure to put a using System.Windows.Interop;

    public partial class MainWindow : Window
    {
    
    
        public MainWindow()
        {
            InitializeComponent();
            var canvas = new Canvas();
            Content = canvas;
    
            var element = new DrawingVisualElement();
            canvas.Children.Add(element);
    
            CompositionTarget.Rendering += (s, e) =>
            {
                using (var dc = element.visual.RenderOpen())
                {
                    dc.DrawRectangle(Brushes.Black, null, new Rect(0, 0, 50, 50));
                }
            };
            this.SourceInitialized += new EventHandler(OnSourceInitialized);
        }
    
        void OnSourceInitialized(object sender, EventArgs e)
        {
            HwndSource source = (HwndSource)PresentationSource.FromVisual(this);
            source.AddHook(new HwndSourceHook(HandleMessages));
    
        }
        IntPtr HandleMessages(IntPtr hwnd, int msg,IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == 0x200)
                Title = Mouse.GetPosition(this).ToString(); // because I did not want to split the lParam into High/Low values for Position information
            return IntPtr.Zero;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a WPF program in C# in which I have a ListView
I keep getting this error when making the program as given below: using System;
I have a WPF program with two listviews which display information from two different
I created this simple textpad program in WPF/VB.NET 2008 that automatically saves the content
I'm writting a WPF program.And now i have some problems. There is a Window
Take note of the code for this small WPF C# program made with Microsoft
Following scenario: In an WPF application the program calls Log.Write to enqueue messages which
I have a WPF program that I'm developing in which I want to catch
I'm making a WPF program which is able to color the rows in a
I am working on a calendar program, which consists mainly of a WPF DataGrid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.