QuestionDrawing problems!

Posts 1–5 of 5 · Page 1 of 1
Drawing problems!
Hello,
I was wondering if anyone has a solution to keep something drawn on my screen, I have kind of fixed it by just putting a Timer in my code and getting it to draw the image over and over but this causes lag and is a bit of a problem!
If i don't do that to keep the image on the screen, the image will show but as soon as something refreshes behind it the drawn image will be gone!
Is there anyway to keep the image drawn on the screen by not using a timer just to repeat the drawing process ?

Here is my whole code if this helps:
Code:
Public Class Form1
    Declare Function GetDC Lib "user32.dll" (ByVal hWnd As IntPtr) As System.IntPtr
    Declare Function ReleaseDC Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal hdc As IntPtr) As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim width As String = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width.ToString
        Dim height As String = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height.ToString
        Label1.Text = width
        Label2.Text = height
        Dim width2 As Integer = CInt(Label1.Text.ToString)
        Dim height2 As Integer = CInt(Label2.Text.ToString)
        Label1.Text = CStr(width2 / 2)
        Label2.Text = CStr(height2 / 2)
        Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim width3 As Integer = Label1.Text
        Dim height3 As Integer = Label2.Text
            Dim hdc As IntPtr = GetDC(IntPtr.Zero)
            Dim gr As Graphics = Graphics.FromHdc(hdc)
        Dim Point As New Point(width3, height3)
        If RadioButton1.Checked = True Then
            gr.DrawImage(My.Resources.cross1, Point)
        ElseIf RadioButton2.Checked = True Then
            gr.DrawImage(My.Resources.cross2, Point)
        ElseIf RadioButton3.Checked = True Then
            gr.DrawImage(My.Resources.cross3, Point)
        End If
    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        If RadioButton1.Checked = True Then
            PictureBox1.Image = My.Resources.cross1
        End If
        If RadioButton2.Checked = True Then
            PictureBox1.Image = My.Resources.cross2
        ElseIf radiobutton3.checked = True Then
            PictureBox1.Image = My.Resources.cross3
        End If
    End Sub
End Class
Thanks in advance guys
Adding your code to the paint event should do the job.

I also wouldnt use that second timer either. You can handle all that just using Radiobutton checked changed.
Something like this should work
Code:
    Private Sub _CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, radioButton3.CheckedChanged
        PictureBox1.Image = If(RadioButton1.Checked, My.Resources.cross1, If(RadioButton2.Checked, My.Resources.cross2, My.Resources.cross3))
    End Sub
Thanks man, using paint event now and its working better anyway but still not perfect, and thanks for that code
np dude, i didnt try fixing the main function. I'll see if i can help you further so you can get the first part working the way you want.
I cant really tell whats wrong just by looking at it, im not a vb coder. So i'll create the code on my end to see what the problem is.

EDIT:
Looks like flicker. Not really sure how to fix that but i have another option.
Instead of drawing your image on screen, draw it on another form.

Example
Create another form in your project. I called mine Picures
Set the FormBorderStyle to None

Something like this can be used to change the settings
Code:
    Public Sub CreateImage(ByVal _Form As Form, ByVal _Size As Size, ByVal _Location As Point, ByVal _Image As Image)
        If Not _Form.Visible Then
            _Form.Show()
        End If
        _Form.Location = _Location
        _Form.Size = _Size
        _Form.BackgroundImage = _Image
        _Form.BackgroundImageLayout = ImageLayout.Stretch
    End Sub
ByVal _Form As Form = Second Form
ByVal _Size As Size = size you would like the image
ByVal _Location As Point = location on screen
ByVal _Image As Image = Image you want

On the same RadioButton Checked Changed
Code:
    Private Sub _CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged
        Dim _Size As Size = Screen.PrimaryScreen.Bounds.Size
        Dim _Image As Image = If(RadioButton1.Checked, My.Resources.cross1, If(RadioButton2.Checked, My.Resources.cross2, My.Resources.cross3))
        CreateImage(Pictures, New Size(150, 150), New Point(_Size.Width / 2, _Size.Height / 2), _Image)
    End Sub
I only have the size 150x150 and some random location. But everytime i change radio button, the image changes without flicker.
Maybe have a mess around with it, might help ya.
Hmm seems another good choice ill have to try this Thanks alot for your help
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us