?
Solved

Visual Basic Screen Resolution Changes

Posted on 2011-04-21
9
Medium Priority
?
483 Views
Last Modified: 2012-05-11
I have got everything to work just perfect on my monitor, but when I tested it on another monitor the coordinates are off. I did some searching and see that there is some math involved. I don't know what part of the code you need to see, if I need to post all of it just let me know.
0
Comment
Question by:Jack_Jones
  • 7
9 Comments
 
LVL 1

Author Comment

by:Jack_Jones
ID: 35440129
The coordinates for the pixel detection, mouse coords seem to be ok. I am not sure how to word this perfectly.
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 35440149
I take it back on a test computer, the mouse and pixels are off.
0
 
LVL 14

Expert Comment

by:athomsfere
ID: 35440157
Can you explain what you are trying to do, whats not working, and then post the code for that part of the application?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Jack_Jones
ID: 35440167
If it helps, my screen resolution is 1680 x 1050.

Other system needed to work on is 1056 x 825
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 35440176
Here is the code, my computer uses 1680 x 1050 that I used to develop this program on. The host computers use 1056 x 825. Rather than change my display, is it possible to just have the program do the changes?

Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Drawing
Imports System
Imports System.Threading
Imports System.Diagnostics
Public Class Form1

    Private slowmode As Boolean = False
    Dim MyProcess As Process
    'Load Ccleaner
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        MoveMouseTo(Panel1.PointToScreen(New Point(895, 288)))
        Delay(1)
        Clicked.LeftDown()
        Delay(0.5)
        Clicked.LeftUp()
        Delay(1)

        Do While True
            Dim HandleColor As String = "#31AB4E"

            Dim pt As New Point(Panel1.PointToScreen(New Point(136, 174)))
            Dim clr As Color = System.Drawing.ColorTranslator.FromHtml(HandleColor)

            Delay(1)
            If IsColorAt(pt, clr) = True Then
                Label3.Text = "Scan Complete"
            Else
                Label3.Text = "System Scanning"
            End If
        Loop

    End Sub
    'Grab the window to panel1
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim FHandle As IntPtr
        FHandle = FindWindow("msseces_class", Nothing)
        SetParent(FHandle, Panel1.Handle)

    End Sub

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=False)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, _
                                          ByVal lParam As IntPtr) As IntPtr
    End Function

    <System.Runtime.InteropServices.DllImport("USER32.DLL", CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    End Function

    Private Sub MoveMouseTo(ByVal targetPT As Point)
        Dim startPT As Point = Cursor.Position
        Dim distance As Single = PointToPointDist(startPT.X, startPT.Y, targetPT.X, targetPT.Y)
        Dim duration As Single = distance / 400 * 1000
        Dim startDT As DateTime = DateTime.Now
        Dim targetDT As DateTime = DateTime.Now.AddMilliseconds(duration)
        Dim percent As Single
        While DateTime.Now <= targetDT
            percent = DateTime.Now.Subtract(startDT).TotalMilliseconds / duration
            Cursor.Position = New Point(startPT.X + (targetPT.X - startPT.X) * percent, startPT.Y + (targetPT.Y - startPT.Y) * percent)
            System.Threading.Thread.Sleep(50)
            Application.DoEvents()
        End While
        Cursor.Position = targetPT
    End Sub

    Private Function PointToPointDist(ByVal Ax As Single, ByVal Ay As Single, ByVal Bx As Single, ByVal By As Single) As Single
        ' PointToPointDist = SquareRoot((Bx - Ax)^2 + (By - Ay)^2)
        Return Math.Sqrt((Bx - Ax) * (Bx - Ax) + (By - Ay) * (By - Ay))
    End Function

    Private Sub Delay(ByVal DelayInSeconds As Integer)
        Dim targetDT As DateTime = DateTime.Now.Add(TimeSpan.FromSeconds(DelayInSeconds))
        While targetDT > DateTime.Now
            System.Threading.Thread.Sleep(50) ' <-- very SMALL delay
            Application.DoEvents() ' <-- keep UI responsive
        End While
    End Sub

    Private Function IsColorAt(ByVal pt As Point, ByVal clr As Color) As Boolean
        Dim bmp As New Bitmap(1, 1)
        Using G As Graphics = Graphics.FromImage(bmp)
            G.CopyFromScreen(pt, New Point(0, 0), bmp.Size)
        End Using
        Return bmp.GetPixel(0, 0).Equals(clr)
    End Function

    Public Class Clicked
        Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
        Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down 
        Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up 
        Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down 
        Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up 

        Public Shared Sub LeftDown()
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        End Sub

        Public Shared Sub LeftUp()
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        End Sub

        Public Shared Sub RightDown()
            mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        End Sub

        Public Shared Sub RightUp()
            mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
        End Sub
    End Class

End Class

Open in new window

0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 35444694
Where you at Idle_Mind... I have been searching still trying to find a good resolution to this but no luck so far.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 35444807
Lol...not sure how to solve the resolution issue.

Personally, though, I wouldn't automate a process like this using coords and mouse clicks.  =\

It's a lot more difficult to code, but I would use windows handles and SendMessage() to automate the process.  You'd use things like FindWindow()/FindWindowEx() and/or EnumWindows()/EnumChildWindows() instead.

Would love to provide some examples but I've got to ferry kids off to Soccer and Tae Kwon Do!  =)
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 35444908
Sounds good, I will be here when you return ;). I have my daughters 7'th bday today, so we going over wife's parents for cake / icecream ;).  I will be back later as well.
0
 
LVL 1

Author Closing Comment

by:Jack_Jones
ID: 35453394
Idle, thanks again! Once I get more time I will look further into it. I did some pokeing around and found that this will work just need to play with it. If I find any problems I will post another questions. Thanks Man

You'd use things like FindWindow()/FindWindowEx() and/or EnumWindows()/EnumChildWindows() instead.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question