Вы находитесь на странице: 1из 30

Kasus 1

You can create an audio player that enables you to play midi and wave files in your hard drives
or DVDROM. It allows you to change drives and search for the particular sound file that you
wish to play.
In this program, you have to add a Combo box, a List Box, a Text Box, a Drive List Box and
three command buttons. Change the label of one of the buttons to "PLAY", another command
button to "STOP", and the last command button to "EXIT". Besides, you need to add the
MMControl and make it invisible.

The code
Sub Combo1_Change( )

Private Sub Stop_Click( )

if ListIndex=0 Then
File1.Pattern=("*.wav")
ElseIf ListIndex=1 Then
File1.Pattern=("*.mid")
Else
File1.Pattern=("*.*)
End If
End Sub

If MMControl1.Mode=524 Then Exit Sub


If MMControl1.Mode<>525 Then
MMControl1.Wait=True
MMControl1.Command="Close"

If Right(File1.Path,1) <>"\" Then


filenam=File1.Path +"\"+File1.Filename

End If

File1.Path=Dir1.Path
If Combo1.ListIndex=0 Then
File1.Pattern=("*.wav")
ElseIf Combo1.ListIndex=1 Then
File1.Pattern=("*.mid")
Else
File1.Pattern=("*.*)
End If

Text1.Text=filenam

Private Sub Drive1.Change( )

End Sub

Dir1.Path=Drive1.Drive
End Sub

Else
filenam=File1.Path+File1.Filename

End Sub
Private Sub Dir1_Change( )

Private Sub Play_Click( )


Private Sub Form_Load( )
If Combo1.ListIndex=0 Then
MMControl1.DeviceType="WaveAudio"
ElseIf Combo1.ListIndex=1 Then
MMControll.DeviceType="Sequencer"
End If
MMControl1.FileName=Text1.Text
MMControl1.Command="Open"
MMControl1.Command="Play"
End Sub

Top=(Screen.Height-Height)\2
Left=(Screen.Width-Width)\2
Combo1.Text="*.wav"
Combo1.AddItem "*.wav"
Combo1.AddItem "*.mid"
Combo1.AddItem "All files"
End Sub

KASUS 2
This is a multimedia player that can play all kinds of media files such as wav, midi, mp3, mpeg
video, avi video and so on. When you launch the program, you can select files of different types
from different drives. After you have selected a particular file, you can play it using the
customized button or you can use the buttons of the multimedia control
In this program, you need to insert the Microsoft Multimedia Control, a combo box, a
dirListBox, a DriveListBox, a fileListbox, a picture box(to play the media files) , a text box and
four command buttons which you label as "Play", "Open","Stop" and "Exit".
The combo box is to allow the user to select different media files from his or her hard drive or
from other sources. The code is:
Private Sub Combo1_Change()
If ListIndex = 0 Then
File1.Pattern = ("*.wav")
ElseIf ListIndex = 1 Then
File1.Pattern = ("*.mid")
ElseIf ListIndex = 2 Then
File1.Pattern = ("*.avi")
ElseIf ListIndex = 3 Then
File1.Pattern = ("*.mpeg;*.mpg;*.mp3")
Else
Fiel1.Pattern = ("*.*")
End If
End Sub

The procedure to allow the user to search for a particular media file in a certain folder is as
follow:
Private Sub File1_Click()
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.wav")
ElseIf Combo1.ListIndex = 1 Then
File1.Pattern = ("*.mid")
ElseIf Combo1.ListIndex = 2 Then
File1.Pattern = ("*.avi")
ElseIf Combo1.ListIndex = 3 Then
File1.Pattern = ("*.mpeg;*.mpg;*.mp3")
Else

File1.Pattern = ("*.*")
End If
If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
Text1.Text = filenam
End Sub

The procedure to open the selected media file is as follow:

Private Sub Open_Click()


If Combo1.ListIndex = 0 Then
MMControl1.DeviceType = "WaveAudio"
End If
If Combo1.ListIndex = 1 Then
MMControl1.DeviceType = "Sequencer"
End If
If Combo1.ListIndex = 2 Then
MMControl1.DeviceType = "AVIVideo"
End If
If Combo1.ListIndex = 3 Then
MMControl1.DeviceType = ""
End If

The Interface

The code
Private Sub Form_Load()
Left = (Screen.Width - Width) \ 2
Top = (Screen.Height - Height) \ 2
'To offer the user selection of media files

Combo1.Text = "*.wav"
Combo1.AddItem "*.wav"
Combo1.AddItem "*.mid"
Combo1.AddItem "*.avi"
Combo1.AddItem "*.mpeg;*.mpg;*.mp3"
Combo1.AddItem "All files"
End Sub
Private Sub Combo1_Change()
'To allow the user to select a particular media file type
If ListIndex = 0 Then
File1.Pattern = ("*.wav")
ElseIf ListIndex = 1 Then
File1.Pattern = ("*.mid")
ElseIf ListIndex = 2 Then
File1.Pattern = ("*.avi")
ElseIf ListIndex = 3 Then
File1.Pattern = ("*.mpeg;*.mpg;*.mp3")
Else
Fiel1.Pattern = ("*.*")
End If
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.wav")
ElseIf Combo1.ListIndex = 1 Then
File1.Pattern = ("*.mid")
ElseIf Combo1.ListIndex = 2 Then
File1.Pattern = ("*.avi")
ElseIf Combo1.ListIndex = 3 Then
File1.Pattern = ("*.mpeg;*.mpg;*.mp3")
Else
File1.Pattern = ("*.*")
End If
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_Click()
'Allows the user to select a particular media file

If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.wav")
ElseIf Combo1.ListIndex = 1 Then
File1.Pattern = ("*.mid")
ElseIf Combo1.ListIndex = 2 Then
File1.Pattern = ("*.avi")
ElseIf Combo1.ListIndex = 3 Then
File1.Pattern = ("*.mpeg;*.mpg;*.mp3")
Else
File1.Pattern = ("*.*")
End If
If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
Text1.Text = filenam
End Sub
Private Sub Open_Click()
If Combo1.ListIndex = 0 Then
MMControl1.DeviceType = "WaveAudio"
End If
If Combo1.ListIndex = 1 Then
MMControl1.DeviceType = "Sequencer"
End If
If Combo1.ListIndex = 2 Then
MMControl1.DeviceType = "AVIVideo"
End If
If Combo1.ListIndex = 3 Then
MMControl1.DeviceType = ""
End If
MMControl1.FileName = Text1.Text
MMControl1.Command = "Open"
End Sub
Private Sub play_Click()
Timer1.Enabled = True
MMControl1.Command = "Play"

MMControl1.hWndDisplay = Picture1.hWnd
End Sub
Private Sub Stop_Click()
If MMControl1.Mode = 524 Then Exit Sub
If MMControl1.Mode <> 525 Then
MMControl1.Wait = True
MMControl1.Command = "Stop"
End If
MMControl1.Wait = True
MMControl1.Command = "Close"
End Sub
Kasus 3
We can create a simple virtual drawing program using Visual Basic 6 as shown in the
figure below. We called it drawing pad.
In this program, the user needs to fill in all the coordinates and selects a color before
proceeding to draw the required shape. If the user forgets to fill in the coordinates or
selects a color, he or she will be prompted to do so.
To create the drawing pad, you need to insert a common dialog control, a picture box,
four text boxes , six command buttons and the necessary labels. The function of the
common dialog control is to assist the users to choose colors. The text boxes are for the
user to enter the coordinates and the picture box is to display the pictures drawn.
The syntax to draw a straight line is Line, and the syntax is as follows:
Picture1.Line (x1, y1)-(x2, y2), color
where picture1 is the picture box, (x1,y1) is the coordinates of the starting point, (x2,
y2) is the ending point and color understandably is the color of the line.
The syntax to draw a non-solid rectangle is
Picture1.Line (x1, y1)-(x2, y2), color, B
The syntax to draw a solid rectangle is
Picture1.Line (x1, y1)-(x2, y2), color, BF
The syntax to draw a circle is

Picture1.Circle (x3, y3), r, color


Where (x3, y3) is the center of the circle and r is the radius.
if you wish to draw a solid circle and fill it with the selected color, then add two more
lines to the above syntax:
Picture1.FillStyle = vbSolid
Picture1.FillColor = color
The syntax to clear the picture is
Picture1.Cls

The Interface

The code
Private Sub cmd_Rectangle_Click()
x1 = Text1.Text
y1 = Text2.Text
x2 = Text3.Text
y2 = Text4.Text
Picture1.Line (x1, y1)-(x2, y2), color, B
End Sub

Private Sub cmd_Color_Click()


CommonDialog1.Flags = &H1&
CommonDialog1.ShowColor
color = CommonDialog1.color
End Sub
Private Sub cmd_Circle_Click()
On Error GoTo addvalues
x3 = Text5.Text
y3 = Text6.Text
r = Text7.Text
Picture1.FillStyle = vbSolid
Picture1.FillColor = color
Picture1.Circle (x3, y3), r, color
Exit Sub
addvalues:
MsgBox ("Please fill in the coordinates ,the radius and the color")
End Sub
Private Sub Command5_Click()
Picture1.Cls
End Sub
Private Sub cmd_SolidRect_Click()
x1 = Text1.Text
y1 = Text2.Text
x2 = Text3.Text
y2 = Text4.Text
Picture1.Line (x1, y1)-(x2, y2), color, BF
End Sub

Kasus 4

This is a program that enables the user to search for an image file in his or her computer and
view it in a picture box. There are two ways to implement the program. The first is to build the
program from the ground up and the second makes use of the common dialog box.
The first way is a little more complicated but you can learn a great deal of programming
techniques. To create this program, you need to insert a a drive list box(DriveListBox) , a
directory list box (DirListBox), a file list box(FileListBox) and a combo box . The drive list box
is for the user to select a drive, the directory list box is for the user to choose a folder and the file
list box is display the files in the selected folder. Besides that, the combo box allows the user to
select all graphics files or all files. You also need to insert a picture box to display the image.

The Interface

The code
Private Sub Combo1_Change()
'To list all graphics files or all files
If ListIndex = 0 Then
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")

Else
Fiel1.Pattern = ("*.*")
End If
End Sub
Private Sub Dir1_Change()
'To choose drive
File1.Path = Dir1.Path
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub

Private Sub File1_Click()


'To select a file
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
Else
File1.Pattern = ("*.*")
End If
If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
End Sub
Private Sub show_Click()
'To show the selected graphics file
If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
Picture1.Picture = LoadPicture(filenam)
End Sub

The second way is much easier to program, you need to insert an image control, a common
dialog box and an icon that resembles on opened file. You need to set the stretchable property of
the image control to true. The procedure to open the common dialog box to browse the image
files as well as to load the selected picture into the image control is
CommonDialog1.Filter = "Bitmaps(*.BMP)|*.BMP|Metafiles(*.WMF)|*.WMF|Jpeg Files(*.jpg)|
*.jpg|GIF Files(*.gif)|*.gif|Icon Files(*.ico)|*.ico|All Files(*.*)|*.*"
CommonDialog1.ShowOpen
Picture1.Picture = LoadPicture(CommonDialog1.FileName)

The filter property of the common dialog box uses the format as shown below
Bitmaps(*.BMP)|*.BMP
to specify the file type, and uses the pipe line | to separate different file types.
Visual Basic supports most of the picture formats namely bmp, wmf, jpg, gif, ico(icon) and
cur(cursor) files.
The command CommonDialog1.ShowOpen is to open the common dialog box and the
command.
Picture1.Picture = LoadPicture (CommonDialog1.FileName)
is to load the selected picture file into the picture box.
The whole program is shown below and the output is shown in the figure below:
Private Sub Image1_Click()
CommonDialog1.Filter = "Bitmaps(*.BMP)|*.BMP|Metafiles(*.WMF)|*.WMF|Jpeg Files(*.jpg)|
*.jpg|GIF Files(*.gif)|*.gif|Icon Files(*.ico)|*.ico|All Files(*.*)|*.*"
CommonDialog1.ShowOpen
image2.Picture = LoadPicture (CommonDialog1.FileName)
End Sub
When the user clicks the opened file icon, the following dialog will appear. The user then can
select the file he or she wish to view.

The Interface

Kasus 5

Why build apps for SharePoint 2013?

BI: Business Intelligence in Share ...

Presented 3 talks at C# Corner Ann ...

Blog

Using TreeView to Display Records from MDB File in


VB.NET 2010
By Mostafa Kaisoun on Jan 26, 2015
In this blog you will learn how to use TreeView to Display Records from MDB File in
VB.NET 2010.

1109

facebook

twitter

linkedIn

google Plus

o
o
o
o
o
o
o
o

expand

This is a trial to read records from MDB database file using TreeView control. My project is a
phone index to display some fields from Customers table in Northwind database file:
CompanyName, ContactName, Address, City, Country and Phone.
My project has one form (frmPhone), this form has following controls:

TreeView: tvData

Four TextBoxes

txtCompany for Company Name, txtAddress for Address, txtCity for City and , txtCountry
for Country.
Two Buttons: For load data and exit application.
Code
There is five procedures:
1. Private Sub LoadCustomerData()
2. ' for open the database file

3.

MyDataFile = Application.StartupPath & "\DataFile\Northwind.mdb"

4.

Dim MyCon As String = "provider=microsoft.jet.oledb.4.0;Password=""""


;data source=" & MyDataFile

5.

datCon = New OleDbConnection()

6.

datCon.ConnectionString = MyCon

7.

Dim cmdSelect As OleDbCommand = New OleDbCommand()

8.

Dim datAdp As OleDbDataAdapter = New OleDbDataAdapter()

9.

datSet = New DataSet()

10.

Try

11.

MyTable = "Customers"

12.

' Select all fields

13.

Dim strSql As String = "SELECT * FROM " + MyTable + " ORDER BY Co


ntactName"

14.

cmdSelect.CommandText = strSql

15.

cmdSelect.CommandType = CommandType.Text

16.

datCon.Open() ' open connection

17.

cmdSelect.Connection = datCon

18.

datAdp.SelectCommand = cmdSelect

19.

datAdp.Fill(datSet, MyTable) ' fill dataset

20.

datCon.Close()

21.

' if no records then exit:

22.

Dim RecCount As Integer = Me.BindingContext(datSet, MyTable).Coun


t

23.

If RecCount = 0 Then

24.

MessageBox.Show("No records in Customer table file!")

25.

Exit Sub

26.
27.
28.

End If
Catch ex As Exception
MessageBox.Show(ex.ToString())

29.

End Try

30.

SetRootNode()

31.

End Sub

32.
33.

Private Sub SetRootNode()

34.

' set root node of TreeView:

35.

tvData.Nodes.Add("Phone Index")

36.

tvData.Nodes(0).Tag = "RootDB"

37.

tvData.Nodes(0).ImageIndex = 0

38.

tvData.Nodes(0).SelectedImageIndex = 0

39.

' Set Contact Name

40.

SetContactName()

41.

'Set Customer Phone

42.

SetCustPhone()

43.

End Sub

44.
45.Private Sub SetContactName()
46.'set ContactName field as node
47.

itmNumber = datSet.Tables(MyTable).Rows.Count

48.

For i As Integer = 0 To itmNumber - 1

49.tvData.Nodes(0).Nodes.Add(datSet.Tables(MyTable).Rows(i).ItemArray(2).ToSt
ring())

50.

tvData.Nodes(0).Nodes(i).Tag = "Name"

51.

tvData.Nodes(0).Nodes(i).ImageIndex = 2

52.

tvData.Nodes(0).Nodes(i).SelectedImageIndex = 2

53.
54.

Next
End Sub

55.
56.Private Sub SetCustPhone()
57.'set Phone field as node
58.

For i As Integer = 0 To itmNumber - 1

59.tvData.Nodes(0).Nodes(i).Nodes.Add(datSet.Tables(MyTable).Rows(i).ItemArra
y(9).ToString())
60.

tvData.Nodes(0).Nodes(i).Nodes(0).Tag = "Phone"

61.

tvData.Nodes(0).Nodes(i).Nodes(0).ImageIndex = 4

62.

tvData.Nodes(0).Nodes(i).Nodes(0).SelectedImageIndex = 4

63.
64.

Next
End Sub

65.
66.

Private Sub DisplayRecord(ByVal EmployeeName As String)

67.

' Show some data about customer:

68.

Try

69.

' using DataView to find record:

70.

Dim dv As DataView = New DataView(datSet.Tables(MyTable))

71.

dv.Sort = "ContactName"

72.

Dim i As Integer = dv.Find(EmployeeName)

73.

Me.BindingContext(datSet, MyTable).Position = i

74.

txtCompany.Text = datSet.Tables(MyTable).Rows(i).ItemArray(1).ToStri
ng()

75.

txtAddress.Text = datSet.Tables(MyTable).Rows(i).ItemArray(4).ToStrin
g()

76.

txtCity.Text = datSet.Tables(MyTable).Rows(i).ItemArray(5).ToString()

77.

txtCountry.Text = datSet.Tables(MyTable).Rows(i).ItemArray(8).ToStrin
g()

78.

Catch ex As Exception

79.
80.
81.

MessageBox.Show(ex.ToString())
End Try
End Sub

Kasus 6

Convert Date From Solar (Gregorian) to Lunar (Hijri) Using


VB.NET
Introduction
The Lunar (Hijri) calendar is very important for Muslims as is the Solar (Gregorian) calendar is
important because the Lunar calendar was related to some elements of worship, so I looked at
many sites on the internet to understand how to calculate the age of the moon in any given day, I
found many sites offering various ways and I took what I found to provide results closer to the
truth.
I've noticed that most sites agree on the expense of the Julian date but don't agree on how to
calculate the age of the moon and found the difference between these sites to be up to one day
and when the moon's age is 30 days, the result is zero in some sites.

In this program I calculate the approximate age of the moon in days and did not give attention to
the parts of the day of the hours and minutes.
For the program to be more useful, I added a PictureBox control to display the lighted part of the
moon and darkness part of the moon commensurate with the age of the moon.
There is a small probability of a one-day error for any calculation to convert a date.

Note: You can read Months by its name or by its number.


Code
Best Calculation to Get Number Approximation

1. Private Function getInt(ByRef fNumber As Double) As Double


2.
3.
4.
5.
6.

If fNumber < -0.0000001 Then


getInt = Math.Ceiling(fNumber - 0.0000001)
Else
getInt = Math.Floor(fNumber + 0.0000001)
End If

7. End Function

Convert Solar (Gregorian) Date to Lunar (Hijri) Date


1. Private Sub SolarToLunar()
2.

' convert Solar year from 622 to 2500

3.

Dim jd As Double

4.

Dim j, L, n As Double

5.

Dim d, m, y As Integer

6.

Dim theDay As Integer

7.

' Solar day

8.

d = Val(SolarDay.Text)

9.

'get the number of Solar month

10.

m = SolarMonth.SelectedIndex + 1

11.

' Solar year

12.

y = Val(SolarYear.Text)

13.

If (y > 1582) Or ((y = 1582) And (m > 10)) Or ((y = 1582) And (m = 10) An
d (d > 14)) Then

14.

jd = getInt((1461 * (y + 4800 + getInt((m - 14) / 12))) / 4) + getInt((3


67 * (m - 2 - 12 * (getInt((m - 14) / 12)))) / 12) - getInt((3 * (getInt((y + 4900
+ getInt((m - 14) / 12)) / 100))) / 4) + d - 32075

15.

Else

16.

jd = 367 * y - getInt((7 * (y + 5001 + getInt((m - 9) / 7))) / 4) + getInt(


(275 * m) / 9) + d + 1729777

17.

End If

18.

' Solar year >= 622

19.

If jd < 1948440 Then

20.

DateMinError()

21.

Exit Sub

22.

End If

23.

' Solar year <= 2500

24.

If jd > 2621734 Then

25.

DateMaxError()

26.

Exit Sub

27.

End If

28.

'day of the week

29.

theDay = jd Mod 7

30.

lblDay.Text = WeekDays(theDay)

31.

L = jd - 1948440 + 10632

32.

n = getInt((L - 1) / 10631)

33.

L = L - 10631 * n + 354

34.

j = (getInt((10985 - L) / 5316)) * (getInt((50 * L) / 17719)) + (getInt(L / 5


670)) * (getInt((43 * L) / 15238))

35.

L = L - (getInt((30 - j) / 15)) * (getInt((17719 * j) / 50)) - (getInt(j / 16)) * (


getInt((15238 * j) / 43)) + 29

36.

m = Int(getInt((24 * L) / 709))

37.

d = Int(L - getInt((709 * m) / 24))

38.

y = Int(30 * n + j - 30)

39.

' display Lunar date

40.

LunarDay.Text = Str(d)

41.

LunarMonth.Text = LunarMonths(m - 1)

42.

LunarYear.Text = Str(y)

43.

ShowMoonPhase()

44.
45.
46.
47.
48.

If d = 1 Then
lblAge.Text = Str(d) & " day"
Else
lblAge.Text = Str(d) & " days"
End If

49.End Sub

Convert Lunar (Hijri) Date to Solar (Gregorian) Date


1. Private Sub LunarToSolar()
2.

' convert Lunar year from 1 to 1900

3.

Dim jd As Double

4.

Dim i, j, k, L, n As Double

5.

Dim d, m, y As Integer

6.

Dim theDay As Integer

7.

' Lunar day

8.

d = Val(LunarDay.Text)

9.

If d = 1 Then

10.
11.
12.

lblAge.Text = Str(d) & " day"


Else
lblAge.Text = Str(d) & " days"

13.

End If

14.

'get the number of Lunar month

15.

m = LunarMonth.SelectedIndex + 1

16.

' Lunar year

17.

y = Val(LunarYear.Text)

18.

jd = getInt((11 * y + 3) / 30) + 354 * y + 30 * m - getInt((m - 1) / 2) + d +


1948440 - 385

19.

'day of the week

20.

theDay = jd Mod 7

21.

lblDay.Text = WeekDays(theDay)

22.

If jd > 2299160 Then

23.

L = jd + 68569

24.

n = getInt((4 * L) / 146097)

25.

L = L - getInt((146097 * n + 3) / 4)

26.

i = getInt((4000 * (L + 1)) / 1461001)

27.

L = L - getInt((1461 * i) / 4) + 31

28.

j = getInt((80 * L) / 2447)

29.

d = Int(L - getInt((2447 * j) / 80))

30.

L = getInt(j / 11)

31.

m = Int(j + 2 - 12 * L)

32.

y = Int(100 * (n - 49) + i + L)

33.

Else

34.

j = jd + 1402

35.

k = getInt((j - 1) / 1461)

36.

L = j - 1461 * k

37.

n = getInt((L - 1) / 365) - getInt(L / 1461)

38.

i = L - 365 * n + 30

39.

j = getInt((80 * i) / 2447)

40.

d = Int(i - getInt((2447 * j) / 80))

41.

i = getInt(j / 11)

42.

m = Int(j + 2 - 12 * i)

43.

y = Int(4 * k + n + i - 4716)

44.

End If

45.

' display Solar date

46.

SolarDay.Text = Str(d)

47.

SolarMonth.Text = SolarMonths(m - 1)

48.

SolarYear.Text = Str(y)

49.

ShowMoonPhase()

50.End Sub

Draw the Moon at Selected Date


1. Private Sub ShowMoonPhase()
2.

Dim ag As Integer = Val(LunarDay.Text)

3.

Dim Phase As Double = ag / 29.530588853

4.

Dim Xpos, Ypos, Rpos As Integer

5.

Dim Xpos1, Xpos2 As Integer

6.

Me.ClearDraw() 'clear PicMoon PictureBox

7.

' Width of 'ImageToDraw' Object = Width of 'PicMoon' control

8.

Dim PageWidth As Integer = Me.MoonShape.Width

9.

' Height of 'ImageToDraw' Object = Height of 'PicMoon' control

10.

Dim PageHeight As Integer = Me.MoonShape.Height

11.

' Initiate 'ImageToDraw' Object with size = size of control 'PicMoon' control

12.

Dim ImageToDraw As Bitmap = New Bitmap(PageWidth, PageHeight)

13.

'Create graphics object for alteration.

14.

Dim newGraphics As Graphics = Graphics.FromImage(ImageToDraw)

15.

Dim PenW As Pen = New Pen(Color.White) ' For lighted part of the moon

16.

Dim PenB As Pen = New Pen(Color.Black) ' For darkness part of the moon

17.

For Ypos = 0 To 45

18.

Xpos = Int(Math.Sqrt(45 * 45 - Ypos * Ypos))

19.

' Draw darkness part of the moon

20.

Dim pB1 As Point = New Point(90 - Xpos, Ypos + 90)

21.

Dim pB2 As Point = New Point(Xpos + 90, Ypos + 90)

22.

Dim pB3 As Point = New Point(90 - Xpos, 90 - Ypos)

23.

Dim pB4 As Point = New Point(Xpos + 90, 90 - Ypos)

24.

newGraphics.DrawLine(PenW, pB1, pB2)

25.

newGraphics.DrawLine(PenW, pB3, pB4)

26.

' Determine the edges of the lighted part of the moon

27.

Rpos = 2 * Xpos

28.

If (Phase < 0.5) Then

29.

Xpos1 = -Xpos

30.

Xpos2 = Int(Rpos - 2 * Phase * Rpos - Xpos)

31.

Else

32.

Xpos1 = Xpos

33.

Xpos2 = Int(Xpos - 2 * Phase * Rpos + Rpos)

34.

End If

35.

' Draw the lighted part of the moon

36.

Dim pW1 As Point = New Point(Xpos1 + 90, 90 - Ypos)

37.

Dim pW2 As Point = New Point(Xpos2 + 90, 90 - Ypos)

38.

Dim pW3 As Point = New Point(Xpos1 + 90, Ypos + 90)

39.

Dim pW4 As Point = New Point(Xpos2 + 90, Ypos + 90)

40.

newGraphics.DrawLine(PenB, pW1, pW2)

41.

newGraphics.DrawLine(PenB, pW3, pW4)

42.

Next

43.

' Display the bitmap in the picture box.

44.

Me.MoonShape.Image = ImageToDraw

45.

' Release graphics object

46.

PenW.Dispose()

47.

PenB.Dispose()

48.

newGraphics.Dispose()

49.

ImageToDraw = Nothing

50.End Sub

Вам также может понравиться