[Winform] supports color collection.

Source: Internet
Author: User

Today, someone in a group asked how the color collection function () in the drawing software was implemented. I only provide a simple idea.

There are two ideas in total.

Idea 1: Use GDI to capture desktop images and display them in a borderless form. You can get the corresponding color value through the mouse event to get the click position.

Train of Thought 2: Do not use a false window. Register an underlying hook to monitor the mouse. Then obtain the color of the corresponding position. (May be regarded as a virus)

 

The first idea is simpler to implement. After the second idea is implemented, the app can interact with the app during color collection.

 

Here we will post the code for the first idea.

using System.Drawing;using System.Windows.Forms;namespace ColorPickerDemo{    public class PickColorForm : Form    {        public PickColorForm()        {            Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);            using (Graphics g = Graphics.FromImage(bmp))            {                g.CopyFromScreen(0, 0, 0, 0, bmp.Size);            }            this.FormBorderStyle = FormBorderStyle.None;            this.SetStyle(ControlStyles.DoubleBuffer, true);            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);            this.DesktopLocation = new Point(0, 0);            this.Size = bmp.Size;            this.Cursor = Cursors.Cross;            this.MouseUp += (ss, ee) =>            {                if (ee.Button == MouseButtons.Left)                {                    this.Color = bmp.GetPixel(ee.X, ee.Y);                    this.DialogResult = DialogResult.OK;                }                else                {                    Color = null;                    this.DialogResult = DialogResult.Cancel;                }                this.Close();            };            this.KeyDown += (ss, ee) =>            {                if (ee.KeyCode == Keys.Escape)                    this.Close();            };            this.BackgroundImage = bmp;        }        public Color? Color { get; private set; }    }}

Call method:

        private void Form1_MouseClick(object sender, MouseEventArgs e)        {            PickColorForm f = new PickColorForm();            if (f.ShowDialog() == DialogResult.OK)            {                this.BackColor = f.Color.Value;            }        }

PS: I wrote this article only when I was bored tonight, so the content of this article is very simple. If you do not understand it, leave a message.

When will this blog post be updated if I have implemented idea 2.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.