简体   繁体   English

在VB.net 2010中将DrawImage设为按钮

[英]Making a DrawImage a button in VB.net 2010

I have these three images that I have drawn to my form. 我已经将这三张图像绘制到表单中。

    GraphicsBuffer.DrawImage(ButtonEasy, New Rectangle(25, 330, 100, 50), 0, 0, 100, 50, GraphicsUnit.Pixel, ImageAttributes)
    GraphicsBuffer.DrawImage(ButtonMedium, New Rectangle(150, 330, 100, 50), 0, 0, 100, 50, GraphicsUnit.Pixel, ImageAttributes)
    GraphicsBuffer.DrawImage(ButtonHard, New Rectangle(275, 330, 100, 50), 0, 0, 100, 50, GraphicsUnit.Pixel, ImageAttributes)

But I want to make a Boolean expression for when they are clicked so I can trigger the events to load the game mode selected. 但是我想为其单击时创建一个布尔表达式,以便触发事件以加载选定的游戏模式。

Do I do this through resource code or is there a simply way to do this. 我是通过资源代码执行此操作还是有一种简单的方法来执行此操作。 My idea seems like it would be bad and not syntaxically correct. 我的想法似乎很糟糕,而且语法上不正确。

Edit: I've gotten to this: 编辑:我已经做到这一点:

Private Sub ButtonEasy_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs) _
 Handles ButtonEasy.MouseClick

    Dim buttonEasyRect = New Rectangle(25, 330, 100, 50)
    If buttonEasyRect.Contains(e.Location) Then

    End If

End Sub

But not really sure where to go from this. 但不是很确定从何而来。 Apparently "ButtonEasy.Mouseclick" Handles throws an error "WithEvents variable undefined". 显然,“ ButtonEasy.Mouseclick”句柄会引发错误“ WithEvents变量未定义”。 Not sure where to go from here. 不知道从这里去哪里。

Thanks in advance! 提前致谢!

Edit2: After help from LarsTech I've gotten an Enum in and this: Edit2:在LarsTech的帮助下,我得到了一个枚举,其内容如下:

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
    Dim level As Difficulty = Difficulty.None
    If e.Button = MouseButtons.Left Then
    End If

    If New Rectangle(25, 330, 100, 50).Contains(e.Location) Then
        level = Difficulty.Easy
    ElseIf New Rectangle(150, 330, 100, 50).Contains(e.Location) Then       
        level = Difficulty.Medium
    ElseIf New Rectangle(275, 330, 100, 50).Contains(e.Location) Then
        level = Difficulty.Hard
    End If

    If level = Difficulty.Easy Then
        GameMode = 1
    ElseIf level = Difficulty.Medium Then
        GameMode = 2
    ElseIf level = Difficulty.Hard Then
        GameMode = 3
    End If

End Sub

How do I call this in my loop? 我该如何在循环中称呼它? Currently I have the loop wait for Asynchkeypress to set timescale to 300 which starts the game. 目前,我有循环等待Asynchkeypress将时间刻度设置为300,这将启动游戏。

Is there a reason you don't actually use Buttons to do this? 您有没有真正不使用Button来这样做的原因吗?

In any case, you probably should have a class for all that information, which image, which rectangle, etc. This button class would also hold the IsPushed logic. 在任何情况下,您都应该为所有这些信息,一个图像,哪个矩形等提供一个类。此按钮类还将包含IsPushed逻辑。

But for what you currently have, having an enum would probably help: 但是对于您当前拥有的资源,枚举可能会有所帮助:

Public Enum Difficulty
  None
  Easy
  Medium
  Hard
End Enum

Then in the MouseDown event: 然后在MouseDown事件中:

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
  Dim level As Difficulty = Difficulty.None

  If e.Button = MouseButtons.Left Then
    If New Rectangle(25, 330, 100, 50).Contains(e.Location) Then
      level = Difficulty.Easy
    ElseIf New Rectangle(150, 330, 100, 50).Contains(e.Location) Then
      level = Difficulty.Medium
    ElseIf New Rectangle(275, 330, 100, 50).Contains(e.Location) Then
      level = Difficulty.Hard
    End If
  End If

  If level <> Difficulty.None Then
    MessageBox.Show("You are playing " & level.ToString)
  End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM