0

我需要构建一个 15 幅图片拼图,但我不知道如何将图像添加到按钮中,以使图像填满按钮的所有空间。或者我应该做不同的事情?这是我需要构建的链接: sahek.co.il/content/full_screen.php?url= http://www.sahek.co.il/ ...目前我所拥有的只是一个带有数字的游戏:

Class MainWindow 

Private Sub btn1_Click(sender As Object, e As RoutedEventArgs) Handles btn1.Click
    '2,4
    checkBtn(btn1, btn4)
    checkBtn(btn1, btn2)
    chechSolved()
End Sub

Private Sub btn2_Click(sender As Object, e As RoutedEventArgs) Handles btn2.Click
    '1,3,5
    checkBtn(btn2, btn1)
    checkBtn(btn2, btn3)
    checkBtn(btn2, btn5)
    chechSolved()
End Sub

Private Sub btn3_Click(sender As Object, e As RoutedEventArgs) Handles btn3.Click
    '2,6
    checkBtn(btn3, btn2)
    checkBtn(btn3, btn6)
    chechSolved()
End Sub

Private Sub btn4_Click(sender As Object, e As RoutedEventArgs) Handles btn4.Click
    '1,5,7
    checkBtn(btn4, btn1)
    checkBtn(btn4, btn5)
    checkBtn(btn4, btn7)
    chechSolved()
End Sub

Private Sub btn5_Click(sender As Object, e As RoutedEventArgs) Handles btn5.Click
    '2,4,6,8
    checkBtn(btn5, btn2)
    checkBtn(btn5, btn4)
    checkBtn(btn5, btn6)
    checkBtn(btn5, btn8)
    chechSolved()
End Sub

Private Sub btn6_Click(sender As Object, e As RoutedEventArgs) Handles btn6.Click
    '3,5,9
    checkBtn(btn6, btn3)
    checkBtn(btn6, btn5)
    checkBtn(btn6, btn9)
    chechSolved()
End Sub

Private Sub btn7_Click(sender As Object, e As RoutedEventArgs) Handles btn7.Click
    '4,8
    checkBtn(btn7, btn4)
    checkBtn(btn7, btn8)
    chechSolved()
End Sub

Private Sub btn8_Click(sender As Object, e As RoutedEventArgs) Handles btn8.Click
    '5,7,9
    checkBtn(btn8, btn5)
    checkBtn(btn8, btn7)
    checkBtn(btn8, btn9)

    chechSolved()
End Sub

Private Sub btn9_Click(sender As Object, e As RoutedEventArgs) Handles btn9.Click
    '6,8
    checkBtn(btn9, btn6)
    checkBtn(btn9, btn8)

    chechSolved()
End Sub

Sub checkBtn(ByRef butt1 As Button, ByRef butt2 As Button)
    If butt2.Content = "" Then
        butt2.Content = butt1.Content
        butt1.Content = ""
    End If
End Sub

Sub chechSolved()

    If btn1.Content = "1" And btn2.Content = "2" And btn3.Content = "3" And btn4.Content = "4" And btn5.Content = "5" And btn6.Content = "6" And btn7.Content = "7" And btn8.Content = "8" And btn9.Content = "" Then
        MsgBox("הצלחת")
    End If
End Sub



Sub shuffle()
    Dim a(8) As String
    Dim i, j, RN As Integer
    Dim flag As Boolean

    flag = False
    i = 1
    a(j) = 1

    Do While i <= 8
        Randomize()
        RN = CInt(Int((8 * Rnd()) + 1))

        For j = 1 To i
            If (a(j) = RN) Then
                flag = True
                Exit For
            End If
        Next

        If flag = True Then
            flag = False
        Else
            a(i) = RN
            i = i + 1
        End If

    Loop

    btn1.Content = a(1)
    btn2.Content = a(2)
    btn3.Content = a(3)
    btn4.Content = a(4)
    btn5.Content = a(5)
    btn6.Content = a(6)
    btn7.Content = a(7)
    btn8.Content = a(8)
    btn9.Content = ""
End Sub

Private Sub btnSH_Click(sender As Object, e As RoutedEventArgs) Handles btnSH.Click
    shuffle()
End Sub
End Class
4

2 回答 2

0

XAML中,您可以尝试,

<Button Name="button1" Width="50" Height="30" Click="OnImageButtonClick">
<Image Source="Images/YourImageName.jpg"></Image></Button>

或者
从后面的代码中尝试,

 var brush = new ImageBrush();
 brush.ImageSource = new BitmapImage(new Uri("Images/Image1.jpg",UriKind.Relative));
 button1.Background = brush;

或者

BitmapImage btm = new BitmapImage(new Uri("/LoadImages;component/Images/test.png", UriKind.Relative));
Image img = new Image();
img.Source = btm;
img.Stretch = Stretch.Fill;
button2.Content = img;

请记住根据需要将图像构建操作属性设置为内容资源。但是如果您从资源图像设置按钮背景,则使用上面的代码,如..

  Uri resourceUri = new Uri("Images/Image1.jpg", UriKind.Relative);
  StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

  BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
  var brush = new ImageBrush();
  brush.ImageSource = temp;

  button1.Background = brush;

最后,为您提供另一个很好的替代解决方案..

于 2013-07-02T19:10:13.103 回答
0

在您的 XAML 代码中尝试此操作:

您也可以尝试 Stretch="None"、"Fill"、"Uniform" 或 "UniformToFil"。 以下是有关成像概述的更多信息

<Button Height="100" Width="25" >
  <Button.Background>
    <ImageBrush ImageSource="your image source path" Stretch="Fill" TileMode="None" />
  </Button.Background>
</Button>
于 2013-07-02T17:47:50.010 回答