6 Replies - 1126 Views - Last Post: 28 February 2011 - 12:35 AM Rate Topic: -----

#1 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Explanation of code

Posted 15 February 2011 - 01:31 AM

Hey can someone help explain what this code does , to help me comment it and also its not working so if i understand it a bit better i might be able to fix it
its supposed to have a background with lines in each corner changing angle
it moves at the when it opens
please help i dont understnad most of the code

Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D


Public Sub Draw(ByVal g As Graphics)

        g.SmoothingMode = SmoothingMode.AntiAlias

        Dim rect As Rectangle = Me.ClientRectangle
        Dim cx As Integer = rect.Width
        Dim cy As Integer = rect.Height
        Dim scale As Single = CSng(cy) / CSng(cx)
        Dim _brush As LinearGradientBrush = New LinearGradientBrush(Me.ClientRectangle, Color.Empty, Color.Empty, 45)
        Dim blend As ColorBlend = New ColorBlend()
        blend.Colors = New Color() {Color.Red, Color.Green, Color.Blue}
        blend.Positions = New Single() {0.0, 0.5, 1.0}
        _brush.InterpolationColors = blend
        Dim _pen As Pen = New Pen(_brush)
        Dim x As Integer
        For x = 0 To cx Step 7
            g.DrawLine(_pen, 0, x * scale, cx - x, 0)
            g.DrawLine(_pen, 0, (cx - x) * scale, cx - x, cx * scale)
            g.DrawLine(_pen, cx - x, 0 * scale, cx, (cx - x) * scale)
            g.DrawLine(_pen, cx - x, cx * scale, cx, x * scale)
        Next
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim s As String = ""
        g.DrawString(s, Me.Font, _brush, rect.X, rect.Y, format)
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Explanation of code

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Explanation of code

Posted 16 February 2011 - 09:07 AM

Hi,

When do you call this code: Draw?

i would recommend putting into the form's draw event...presumably my form is Called Form1 this is how it can work...
Notice what I added to the For Loop: Threading.Thread.Sleep(100), this will slow the loop a bit down so you can have a feel of what is happening

Basically it draws 4 lines in each loop by doing this (by example the first line): g.DrawLine(_pen, 0, x * scale, cx - x, 0)

now DrawLine accepts the following parameters

Pen: this is the color of your line and thickness etc. this is created from a brush:

Dim _brush As LinearGradientBrush = New LinearGradientBrush(Me.ClientRectangle, Color.Empty, Color.Empty, 45)



as you can see it is a LinearGradientBrush meaning the colors will change in a Linear way from Red,Green and Blue (as per the blend):

   Dim blend As ColorBlend = New ColorBlend()
   blend.Colors = New Color() {Color.Red, Color.Green, Color.Blue}



the next 4 parameters (x1,y1,x2,y2) are the starting point of the line and the end point of the line on a x and y axis. now the system knows where to paint the LinearGradientBrush(ed) Pen on the coordinates given. Since there are 4 lines, each line will be drawn after the other.

xc and xy are the width and height respectively of whatever you want to draw on. (Remember i said draw on the form, but you can use anything that has a "Paint" method or a ".CrateGraphics" method. Just be carefull some of the "g.Graphics" will need to be "Disposed" off.

to play arround, you could comment out all except one of the "g.DrawLine(...)" statements to find out wich line is wich. also because of the "threading.thread.sleep()" method, you will have to wait until the image is drawn finished before you can close the application or do other things. if you remove the "threading.thread.sleep()" method the image/paining should render quite fast.

What you are using to paint is called GDI this is not directx that is used in game development.. that is something else...

Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D

Public Class Form1

    Public Sub Draw(ByVal g As Graphics)

        g.SmoothingMode = SmoothingMode.AntiAlias

        Dim rect As Rectangle = Me.ClientRectangle
        Dim cx As Integer = rect.Width
        Dim cy As Integer = rect.Height
        Dim scale As Single = CSng(cy) / CSng(cx)
        Dim _brush As LinearGradientBrush = New LinearGradientBrush(Me.ClientRectangle, Color.Empty, Color.Empty, 45)
        Dim blend As ColorBlend = New ColorBlend()
        blend.Colors = New Color() {Color.Red, Color.Green, Color.Blue}
        blend.Positions = New Single() {0.0, 0.5, 1.0}
        _brush.InterpolationColors = blend
        Dim _pen As Pen = New Pen(_brush)
        Dim x As Integer
        For x = 0 To cx Step 7
            g.DrawLine(_pen, 0, x * scale, cx - x, 0)
            g.DrawLine(_pen, 0, (cx - x) * scale, cx - x, cx * scale)
            g.DrawLine(_pen, cx - x, 0 * scale, cx, (cx - x) * scale)
            g.DrawLine(_pen, cx - x, cx * scale, cx, x * scale)
            Threading.Thread.Sleep(100)
        Next

        'old stuff
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim s As String = ""
        g.DrawString(s, Me.Font, _brush, rect.X, rect.Y, format)

        'new stuff
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim s As String = "Hello World"
        g.DrawString(s, Me.Font, _brush, rect.X, rect.Y, format)
        g.DrawString(s, Me.Font, _brush, cx / 2, cy / 2, format)
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Draw(e.Graphics)
    End Sub
End Class



now the last bit of code can write some text but i changed it as it did not seem to be rightly positioned. I changed s (wich was like this s = "") to s = "Hello World" and in stead of "g.DrawString(s, Me.Font, _brush, rect.X, rect.Y, format)", wich draws a string at a position 0,0 to this "g.DrawString(s, Me.Font, _brush, cx / 2, cy / 2, format)" wich will draw the string in the center of the screen (cx / 2 and cy / 2).

Hope this helps.. It might look very confusing but draw a rectangle on a peice of paper and check the variables values that are passed to g.DrawString and you will quickly find out that it's just a little bit of maths and the line is always in a nice position

;)
Was This Post Helpful? 2
  • +
  • -

#3 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Re: Explanation of code

Posted 25 February 2011 - 08:38 AM

thanks alot , :)
Was This Post Helpful? 0
  • +
  • -

#4 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Re: Explanation of code

Posted 26 February 2011 - 11:21 AM

haha im still trying to understand it now
im not good at this , but youve explained it well!

thanks alot
Was This Post Helpful? 0
  • +
  • -

#5 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Re: Explanation of code

Posted 26 February 2011 - 11:32 AM

when i run it im getting a blank screen thats not responding

if i comment out Threading.Thread.Sleep(100)
then it works but the background is there instantly not as an animation :(
Was This Post Helpful? 0
  • +
  • -

#6 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Re: Explanation of code

Posted 26 February 2011 - 12:03 PM

sorry , your code does work
i tried it on a diferent form and it works great :)
there must be something blocking it on this form , im just going to paste the code from the form load
can you spot anything that could be stopping it ? , or is a property ive set wrong maybe?

its a login page btw , and that code shouldnt effect anything really , its just loading a text file in to a text box , but dont know what is wrong :(

[code]
'change password charicter to *
txt_Password.PasswordChar = "*"

'Set FILE_NAME as a string, which is the file location
Dim FILE_NAME As String = "E:\Media Player 4\log in\Textfiles\Username.txt"
' set file read as new, check the connection
Dim objReader As New System.IO.StreamReader(FILE_NAME)
'make the txextbox as info in the file
txt_UserName.Text = objReader.ReadToEnd
'close the conection
objReader.Close()

cb_RememberLogin.Location = New Point(650, 440)

[\code]

oh and also how do i set the text of "hello world" to black so i cant see it when it loads

sorry , im not too good with this stuf
thanks
Was This Post Helpful? 0
  • +
  • -

#7 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Explanation of code

Posted 28 February 2011 - 12:35 AM

Sorry for late reply. So you have more controls on your form? you will maybe have to use "Applcation.DoEvents()" inbetween your code. use it wisely and not too much. it's resource intensive. what happens is that maybe other code is running whilst you are drawing. Forcing Events might help you in rendering the form nicely. If it still looks like it's lagging or staggering, then you will most probably use threads. That one is not so easey to understand. But I use it successfully (took quite a while to figure out)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1