?? You need the current resolution in pixels? (SystemInformation.Primary
Or you want to capture the screen?
Main Topics
Browse All TopicsI need to convert my screen resolution to pixels in vb.net.... there must be an easy line or 2 of code to do this.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
i was looking for a way to make the current resolution into pixels so that i could use coordinates for a mouse event..... But when you change screen resolution it changes where buttons, and other click-able stuff is positioned... this is what i came up with after some googling
my current screen resolution 1440x900
the coordinates of my mouse event (30000,20000)
So if another monitor is 1280x1024 you would plug those into corresponding iWidth and iHeight
basically its a percentage problem
dest_x = (30000 * (iWidth / 1440))
dest_y = (20000 * (iHeight / 900))
I still have to test this
"the coordinates of my mouse event (30000,20000)"
So you're saying you have some kind of container that is 30,000 pixels wide by 20,000 pixels high?!
What kind of control is it?...the coords in .Net, even for the Move() event is still in Pixels.
If you have a different coordinate system of some kind then cK- gave you one way to get the resolution.
You could also do:
Dim rc As Rectangle = Screen.PrimaryScreen.Bound
MessageBox.Show(rc.Width & "x" & rc.Height)
hopefully this will help clear up the confusion as to how I get huge numbers.
'the below declaration is on one line... what would we do without word wrap?....
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Public Const MOUSEEVENTF_MOVE As Integer = &H1
Public Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Public Const MOUSEEVENTF_LEFTUP As Integer = &H4
Public Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000
'set the coordinates of the mouse
Dim dest_x
Dim dest_y
dest_x = (30000 * (iWidth / 1440))
dest_y = (16500 * (iHeight / 900))
'this actually moves the mouse to specified coordinates
mouse_event( _
MOUSEEVENTF_ABSOLUTE + _
MOUSEEVENTF_MOVE + _
MOUSEEVENTF_LEFTDOWN + _
MOUSEEVENTF_LEFTUP, _
dest_x, dest_y, 0, 0)
mouse_event() also expects SCREEN COORDS IN PIXELS too...
So you want to click at a specific position on the screen? (based on "percentage")
I would use Cursor.Position() to move the mouse and then click without coords like this:
Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
Private Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
Private Const MOUSEEVENTF_RIGHTUP As Integer = &H10
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, _
ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, _
ByVal dwExtraInfo As Integer)
Private Sub ClickAt(ByVal x As Integer, ByVal y As Integer, ByVal LeftClick As Boolean, ByVal RestorePosition As Boolean)
Dim pt As Point = Cursor.Position
Cursor.Position = New Point(x, y)
If LeftClick Then
mouse_event(MOUSEEVENTF_LE
mouse_event(MOUSEEVENTF_LE
Else
mouse_event(MOUSEEVENTF_RI
mouse_event(MOUSEEVENTF_RI
End If
If RestorePosition Then
Cursor.Position = pt
End If
End Sub
If you wanted 3/4ths of the way to the right of the screen and 1/2 way down:
Dim percentX As Double = 0.75
Dim percentY As Double = 0.5
Dim rc As Rectangle = Screen.PrimaryScreen.Bound
ClickAt(rc.Width * percentX, rc.Height * percentY, True, False)
Are you clicking on an external button or something? If you want it fullproof then get a HANDLE to the external window and get its screen position with GetWindowRect().
You can get handles in various ways inclucing EnunWindows() and/or FindWindow()/FindWindowEx(
If you explain the "big picture" we can make better suggestions.... =)
Originally I needed an IE window to open maximized and simulate a user clicking a button. (I could not get VB to see the web button through coding)
All that works fine on my screen 1440x900
Then I realized that I needed this to run on different computers with different resolutions ex. 1280x768
I noticed that the mouse will click the exact same spot no matter what resolution the screen was in which due to different resolutions the locations of the web page buttons move.
Now i'm trying to compensate so that my button click is accurate. (when i change resolutions it's only off like .5 inches lol so close yet so far)
I tried opening the window as a certain size ex. 800x600 and having it set in a specific location every time... using setwindowpos function... I could not get that to work... or a movewindow function... also failed..... trying to figure out the ratios seems like the best option because the down and dirty of it is that it's all math based......I was looking for a fool proof solution nothing else i tried has worked...
You can automate some things in IE...just depends on how the button was created. For standard html stuff:
http://www.experts-exchang
Some sites allow data to be posted directly (as part of the URL) with httpwebrequest.
If it's flash based then you'd probably have to go your current route.
It looks like all monitors will work because it is based on the resolution (99% sure)...but if anyone tries to use the code they will need to first set their own coordinates and input their own resolution before distributing.... it's still not fully tested for every single monitor and resolution but for my purpose its good to go
Business Accounts
Answer for Membership
by: sevensnake77Posted on 2009-11-03 at 14:14:49ID: 25734416
'The API's:
&H1)
Private Declare Function EnumDisplaySettings Lib "user32.dll" Alias "EnumDisplaySettingsA" _
(ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
Private Declare Function ChangeDisplaySettings Lib "user32.dll" Alias "ChangeDisplaySettingsA" _
(lpDevMode As Any, ByVal dwFlags As Long) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'Types:
Private Type DEVMODE
dmDeviceName As String * 32
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * 32
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
'Now the function itself:
Public Function changeResolution (ByVal width As Integer, ByVal height As Integer)
Dim dev As DEVMODE
EnumDisplaySettings 0, 0, dev
dev.dmFields = &H80000 Or &H100000
dev.dmPelsWidth = width
dev.dmPelsHeight = height
ChangeDisplaySettings dev, &H4
Call ChangeDisplaySettings(dev,
Call SendMessage(&HFFFF&, &H7E&, 42, ByVal 0&)
End Function
'Use: changeResolution (width, height)
'Example: changeResolution (1024,768)