[Help] Drawing pixels on screen?[Solved]

Posts 1–15 of 15 · Page 1 of 1
[Help] Drawing pixels on screen?[Solved]
How would you draw pixels on the screen? I know there is a SetPixel function but I don't know how to get it working..... I tried the following (NONE of it probably makes sense because I just typed anything):

[php]Public Class Form1

Private Declare Function SetPixel Lib "gdi32" Alias "SetPixel" (ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal color As Integer) As Integer
Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal wnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal wnd As IntPtr, ByVal dc As IntPtr) As IntPtr
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetPixel(GetDC(IntPtr.Zero), 500, 500, RGB(255, 0, 0))
ReleaseDC(IntPtr.Zero, GetDC(IntPtr.Zero))
End Sub
End Class
[/php]
Anyone? I edit my code here:
[php]Imports System****ntime.InteropServices
Public Class Form1
<DllImport("gdi32.dll")> Private Shared Function CreateDC(ByVal lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As String, ByVal lpInitData As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll")> Private Shared Function DeleteDC(ByVal hdc As IntPtr) As Boolean
End Function
<DllImport("gdi32.dll")> Private Shared Function SetPixel(ByVal hdc As IntPtr, ByVal nXPos As Integer, ByVal nYPos As Integer, ByVal colour As ColorRef) As Integer
End Function
Private ScreenDC As IntPtr = CreateDC("Display", Nothing, Nothing, IntPtr.Zero)
Private red As New ColorRef(255, 0, 0)
Private intX As Integer = Screen.PrimaryScreen.Bounds.Width / 2
Private intY As Integer = Screen.PrimaryScreen.Bounds.Height / 2
Structure ColorRef
Public red, green, blue As Byte
Private unused As Byte
Public Sub New(ByVal r As Byte, ByVal g As Byte, ByVal b As Byte)
red = r
green = g
blue = b
unused = 0
End Sub
End Structure

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 1
Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SetPixel(ScreenDC, intX, intY, red)
DeleteDC(ScreenDC)
End Sub
End Class[/php]
Won't work in game, by the way. Just renders over the top.

Also, DeleteDC should only be on your Form_Closing event, if you delete the device context, it removes all your alterations. Also, you can just do color.ToArgb.
Quote Originally Posted by Jason View Post
Won't work in game, by the way. Just renders over the top.

Also, DeleteDC should only be on your Form_Closing event, if you delete the device context, it removes all your alterations. Also, you can just do color.ToArgb.
Still doesn't work though :/
Quote Originally Posted by master131 View Post
Still doesn't work though :/
Did you remove DeleteDC from the timer?
Quote Originally Posted by Jason View Post


Did you remove DeleteDC from the timer?
I sure did..
Gimme one sec, I'll write a working one and you can go from there.

Edit, the problem might be your declarations, with manual dllimports it worked fine, but with declaring it didn't so just copy the declarations from my "DllImports" region and see if that fixes it first.

[php]
Imports System. Runtime.InteropServices
Public Class Form1
#Region "DllImports"

<DllImport("Gdi32.dll")> _
Public Shared Function GetPixel(ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
End Function

<DllImport("Gdi32.dll")> _
Public Shared Function SetPixel(ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal xColor As Integer) As Integer
End Function

<DllImport("Gdi32.dll")> _
Public Shared Function CreateDC(ByVal driverName As String, ByVal deviceName As String, ByVal output As String, ByVal lpInitData As IntPtr) As IntPtr
End Function

<DllImport("Gdi32.dll")> _
Public Shared Function DeleteDC(ByVal dc As IntPtr) As Boolean
End Function

#End Region

Private ScreenDC As IntPtr
Private midX As Integer = My.Computer.Screen.Bounds.Size.Width / 2
Private midY As Integer = My.Computer.Screen.Bounds.Size.Height / 2

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
DeleteDC(ScreenDC)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ScreenDC = CreateDC("Display", Nothing, Nothing, IntPtr.Zero)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For x As Integer = midX - 10 To midX + 10
SetPixel(ScreenDC, i, midY, Color.Red.ToArgb)
Next

For y As Integer = midY - 10 To midY + 10
SetPixel(ScreenDC, midX, x, Color.Red.ToArgb)
Next
End Sub

End Class
[/php]

nvm... didnt start...
the graphic command usage
[php]
graphic. *typ of Draw* ( *color*, *pos x*, *pos y*, *Length*, *Width* )
[/php]
Nothing still appears on my screen though, is it the fact that I need to run VB2010 as admin?
Or is it because I fail at VB? /
Quote Originally Posted by master131 View Post
Nothing still appears on my screen though, is it the fact that I need to run VB2010 as admin?
Or is it because I fail at VB? /
Hmm, did you use the exact code I used as a controlled test? (With a button on the form obviously.)
Quote Originally Posted by Jason View Post


Hmm, did you use the exact code I used as a controlled test? (With a button on the form obviously.)
I sure did! Strange how it didn't work though.

EDIT - Got it working, probably because ScreenDC wasn't declared at the start.
[php]Imports System****ntime.InteropServices
Public Class Form1
#Region "DllImports"

<DllImport("Gdi32.dll")> _
Public Shared Function GetPixel(ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
End Function

<DllImport("Gdi32.dll")> _
Public Shared Function SetPixel(ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal xColor As Integer) As Integer
End Function

<DllImport("Gdi32.dll")> _
Public Shared Function CreateDC(ByVal driverName As String, ByVal deviceName As String, ByVal output As String, ByVal lpInitData As IntPtr) As IntPtr
End Function

<DllImport("Gdi32.dll")> _
Public Shared Function DeleteDC(ByVal dc As IntPtr) As Boolean
End Function

#End Region
Private ScreenDC As IntPtr = CreateDC("Display", Nothing, Nothing, IntPtr.Zero)
Private midX As Integer = My.Computer.Screen.Bounds.Size.Width / 2
Private midY As Integer = My.Computer.Screen.Bounds.Size.Height / 2

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
DeleteDC(ScreenDC)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = midX - 10 To midX + 10
SetPixel(ScreenDC, i, midY, Color.Red.ToArgb)
Next

For x As Integer = midY - 10 To midY + 10
SetPixel(ScreenDC, midX, x, Color.Red.ToArgb)
Next
End Sub

End Class[/php]

The crosshair seems to be in Black and White though.
Quote Originally Posted by master131 View Post
I sure did! Strange how it didn't work though.
Last thing I can think of, try this:

[php]
Imports System****ntime.InteropServices
Public Class Form1
#Region "DllImports"

<DllImport("Gdi32.dll")> _
Public Shared Function GetPixel(ByVal hdc As IntPtr, ByVal x As Int32, ByVal y As Int32) As Int32
End Function

<DllImport("Gdi32.dll")> _
Public Shared Function SetPixel(ByVal hdc As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal xColor As Int32) As Int32
End Function

<DllImport("Gdi32.dll")> _
Public Shared Function CreateDC(ByVal driverName As String, ByVal deviceName As String, ByVal output As String, ByVal lpInitData As IntPtr) As IntPtr
End Function

<DllImport("Gdi32.dll")> _
Public Shared Function DeleteDC(ByVal dc As IntPtr) As Boolean
End Function

#End Region

Private ScreenDC As IntPtr
Private midX As Int32 = My.Computer.Screen.Bounds.Size.Width / 2
Private midY As Int32 = My.Computer.Screen.Bounds.Size.Height / 2

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
DeleteDC(ScreenDC)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ScreenDC = CreateDC("Display", Nothing, Nothing, IntPtr.Zero)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Int32 = midX - 10 To midX + 10
SetPixel(ScreenDC, i, midY, Color.Red.ToArgb)
Next

For x As Int32 = midY - 10 To midY + 10
SetPixel(ScreenDC, midX, x, Color.Red.ToArgb)
Next
End Sub
End Class
[/php]
Don't worry, got it working read above post.
Yeah I didn't play around with colors too much, couldn't be bothered fixing it up

Try this for a different color:

[php]
For i As Int32 = midX - 10 To midX + 10
SetPixel(ScreenDC, i, midY, &Hfe8a01)
Next

For x As Int32 = midY - 10 To midY + 10
SetPixel(ScreenDC, midX, x, &Hfe8a01)
Next
[/php]

Although doesn't seem like the HEX matches as it should.
Ah, never mind I fixed it myself. I used the ColorRef structure shown in the other snippet and it works now.
Posts 1–15 of 15 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us