Get the color of the mouse position in C #

1 minute read

environment

Windows10
.NET FrameWork 4.7.2

Project template

WPF application
(Although NotifyIcon looks like WindowsForm)

Reference site

Dynamically change the image in the task tray
http://nanoappli.com/blog/archives/1975
WPF? Get the color under the mouse cursor anywhere on the screen
https://gogowaten.hatenablog.com/entry/15890527

Github
https://github.com/riko0109/MouseOverColor

Overview

It was created with MVVM in mind as much as possible for programming beginners.
When started, it resides in the task tray icon and continues to acquire the color under the mouse cursor in #FFFFFFFF format.
The task tray icon changes color according to the color under the mouse cursor.
Double-clicking the task tray icon will bring up the main window, displaying the color under the current mouse cursor and the color of the last clicked part.

Where you had a hard time

-The part that receives the model property change notification in the view model and fires the view model change notification did not work well.

MouseOverColorViewModel.cs(Did not work)


private void PropertyUpdate(object sender, PropertyChangedEventArgs e)
        {
            RaisePropertyChanged(e.PropertyName);
        }

Since it did not work with e.PropertyName, I did it by specifying a character string.
In the first place, the e.PropertyName received from the model and the property name on the view model side may not match, so I wonder if conditional branching is better.
I can’t get a compile error, so if there is any other way …

MouseOverColorViewModel.cs


private void PropertyUpdate(object sender, PropertyChangedEventArgs e)
        {
            switch(e.PropertyName)
            {
                case "MouseOverColor":
                    RaisePropertyChanged(nameof(MouseOverColorText));
                    break;

                case "MouseOverFontColor":
                    RaisePropertyChanged(nameof(MouseOverFontColor));
                    break;

                case "LatestClickedPointColor":
                    RaisePropertyChanged(nameof(LatestClickedPointColorText));
                    break;

                case "LatestClickedPointFontColor":
                    RaisePropertyChanged(nameof(LatestClickedPointFontColor));
                    break;

                case "MousePosition":
                    RaisePropertyChanged(nameof(MousePosition));
                    break;
            }

Outlook

・ Implemented copy function on click board
・ When you press a specific shortcut key for the part to get the last clicked color
Make sure to memorize the color under the mouse cursor.

Tags:

Updated: