How to Create a Colour Chooser/Grabber in Visual Basic
Submitted by GeePee on Wednesday, April 22, 2015 - 22:45.
Introduction:
Welcome to a tutorial on how to create a colour chooser in Visual Basic.
Steps of Creation:
Step 1:
First we want to create a form. Mine has three textboxes (r,g,b) to show to red, green and blue colours of the current colour and a Panel to contain the colour image.
Step 2:
Now we want to begin the timer on form load so it is instant. You can make a start/stop button if you want.
Step 3:
Now we want to make the timer tick function. We simply create a new bitmap and get the colours from the mouse position on screen. We then send that to the panel1 and set the textboxes.
Project Complete!
That's it! Below is the full source code and download to the project files.
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Timer1.Enabled = True
- Timer1.Interval = 100
- Timer1.Start()
- End Sub
- Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
- Dim BMP As New Drawing.Bitmap(1, 1)
- Dim GFX As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP)
- GFX.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), New Drawing.Point(0, 0), BMP.Size)
- Dim Pixel As Drawing.Color = BMP.GetPixel(0, 0)
- Panel1.BackColor = Pixel
- r.Text = Pixel.R
- g.Text = Pixel.G
- b.Text = Pixel.B
- End Sub
- Public Class CC
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Timer1.Enabled = True
- Timer1.Interval = 100
- Timer1.Start()
- End Sub
- Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
- Dim BMP As New Drawing.Bitmap(1, 1)
- Dim GFX As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP)
- GFX.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), New Drawing.Point(0, 0), BMP.Size)
- Dim Pixel As Drawing.Color = BMP.GetPixel(0, 0)
- Panel1.BackColor = Pixel
- r.Text = Pixel.R
- g.Text = Pixel.G
- b.Text = Pixel.B
- End Sub
- End Class
Add new comment
- 9 views