How to Create an On-Screen Dimension Calculator in Visual Basic
Submitted by Yorkiebar on Sunday, October 13, 2013 - 09:28.
Language
Introduction:
Welcome to my tutorial on how to create a on-screen dimensions calculator.
Steps of Creation:
Step 1:
First we want to create a form with a button to begin the process. We also want to make two labels, one to say "Console:" and label2 to contain the current console status, set the default to "Idle...". We then want to Import System.Threading because we will be using a one second delay multiple times. If we used a one second delay within the main program thread the entire program UI would freeze.
Step 2:
Now that we have imported we are ready to create a new thread, on button1 click lets create a new thread and give it the starting address of a new custom function called getx...
We also want to disable checking for illegal crossthread calls on form load otherwise we would get an error when trying to update our console line in the UI from a new thread.
Step 3:
Now we want to tell the user to position their mouse in the correct location and give them a few seconds to perform the step. We then take note of the x and y co-ordinates of the mouse. (twice).
Step 4:
Once we have the top (left and right) and bottom (left and right) positions of the image, we want to create a new couple of integers which has the biggest co-ords minus the lowest co-ords (get the size of the image). We then output the result in a message. Done!
Project Complete!
Below is the full source code and download to the project files.
- Imports System.Threading
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim trd As thread = New thread(AddressOf getx)
- trd.isbackground = True
- trd.start()
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- CheckForIllegalCrossThreadCalls = False
- End Sub
- Private Function getx()
- Dim i As Integer = 3
- Do Until i <= 0
- Label2.Text = "Position mouse over the top, left corner of the image... {" & i & " Seconds}"
- thread.sleep(1000)
- i -= 1
- Loop
- Dim x1 As Integer = MousePosition.X
- Dim y1 As Integer = MousePosition.Y
- i = 3
- Do Until i <= 0
- Label2.Text = "Position mouse over the bottom, right corner of the image... {" & i & " Seconds}"
- thread.sleep(1000)
- i -= 1
- Loop
- End Function
- Dim x2 As Integer = MousePosition.X
- Dim y2 As Integer = MousePosition.Y
- Dim x3 As Integer = x2 - x1
- Dim y3 As Integer = y2 - y1
- MsgBox("On-screen dimensions of the image are: " & x3 & "x" & y3)
- Imports System.Threading
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim trd As thread = New thread(AddressOf getx)
- trd.isbackground = True
- trd.start()
- End Sub
- Private Function getx()
- Dim i As Integer = 3
- Do Until i <= 0
- Label2.Text = "Position mouse over the top, left corner of the image... {" & i & " Seconds}"
- thread.sleep(1000)
- i -= 1
- Loop
- Dim x1 As Integer = MousePosition.X
- Dim y1 As Integer = MousePosition.Y
- i = 3
- Do Until i <= 0
- Label2.Text = "Position mouse over the bottom, right corner of the image... {" & i & " Seconds}"
- thread.sleep(1000)
- i -= 1
- Loop
- Dim x2 As Integer = MousePosition.X
- Dim y2 As Integer = MousePosition.Y
- Dim x3 As Integer = x2 - x1
- Dim y3 As Integer = y2 - y1
- MsgBox("On-screen dimensions of the image are: " & x3 & "x" & y3)
- End Function
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- CheckForIllegalCrossThreadCalls = False
- End Sub
- End Class
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 65 views