盈誌 發問時間: 電腦與網際網路程式設計 · 1 0 年前

VB 圖形移動問題

我在表單上做了四個Option,分別代表 左上、正下方、左方、右下方,還有兩個Command按鈕,題目要求勾選Option,按確定,圖形會依選擇的Option去做移動。

我只做了左上方的移動,其他就卡住了!!

麻煩大大給我點指示吧

以下是我在確定按鈕中的程式碼

Private Sub Command1_Click()

If Option3.Value = True Then

Image1.Left = Image1.Left - Speed

Else: Image1.Left = ScaleWidth: End If

If Image1.Top >= -Image1.Height Then

Image1.Top = Image1.Top - Speed

Else: Image1.Top = ScaleHeight: End If

1 個解答

評分
  • 世賢
    Lv 7
    1 0 年前
    最佳解答

    Option Explicit

    Dim Speed As Integer

    Private Sub Command1_Click()

    Dim opts As Integer

    If Option1.Value = True Then opts = 1

    If Option2.Value = True Then opts = 2

    If Option3.Value = True Then opts = 3

    If Option4.Value = True Then opts = 4

    Select Case opts

    Case 1 '左上

    Image1.Left = Image1.Left - Speed

    Image1.Top = Image1.Top - Speed

    Case 2 '正下方

    Image1.Top = Image1.Top + Speed

    Case 3 '左方

    Image1.Left = Image1.Left - Speed

    Case 4 '右下方

    Image1.Left = Image1.Left + Speed

    Image1.Top = Image1.Top + Speed

    End Select

    If Image1.Left < 0 Then Image1.Left = Me.ScaleWidth - Image1.Width

    If Image1.Top < 0 Then Image1.Top = Me.ScaleHeight - Image1.Height

    If Image1.Left > Me.ScaleWidth - Image1.Width Then Image1.Left = 0

    If Image1.Top > Me.ScaleHeight - Image1.Height Then Image1.Top = 0

    End Sub

    Private Sub Form_Load()

    Speed = Screen.TwipsPerPixelX * 15

    Option1.Caption = "左上"

    Option2.Caption = "正下方"

    Option3.Caption = "左方"

    Option4.Caption = "右下方"

    End Sub

還有問題?馬上發問,尋求解答。