.net 如何将此Winform应用程序迁移到WPF?
我重新使用了10年前的代码来绘制卡片(使用cards.dll)。我会用另一个程序来操纵它们。我让它在WinForm中工作,但我真的很想找到一种在WPF中访问图形的方法 它的工作原理与我将粘贴的一样,但同样是winform: Form1.vb:.net 如何将此Winform应用程序迁移到WPF?,.net,wpf,vb.net,winforms,.net,Wpf,Vb.net,Winforms,我重新使用了10年前的代码来绘制卡片(使用cards.dll)。我会用另一个程序来操纵它们。我让它在WinForm中工作,但我真的很想找到一种在WPF中访问图形的方法 它的工作原理与我将粘贴的一样,但同样是winform: Form1.vb: Imports System.Runtime.InteropServices Public Class Form1 Private SQ As Card = New Card(Suit.Spade, Face.Queen) Private
Imports System.Runtime.InteropServices
Public Class Form1
Private SQ As Card = New Card(Suit.Spade, Face.Queen)
Private DK As Card = New Card(Suit.Diamond, Face.King)
Private C10 As Card = New Card(Suit.Club, Face.Ten)
Private H2 As Card = New Card(Suit.Heart, Face.Two)
Private SA As Card = New Card(Suit.Spade, Face.Ace)
#Region "Form Events"
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' MyBase.Load
Card.Init()
End Sub
Private Sub Main_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Card.Deinit()
End Sub
#End Region
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
SQ.PaintGraphicFace(e.Graphics, 10, 10)
DK.PaintGraphicFace(e.Graphics, 50, 10)
C10.PaintGraphicFace(e.Graphics, 110, 200)
C10.PaintGraphicBack(e.Graphics, 30, 300)
H2.PaintGraphicBack(e.Graphics, 250, 370)
SA.PaintGraphicFace(e.Graphics, 200, 50)
End Sub
End Class
Public Class Card
#Region "Construcor"
Public Sub New(ByVal cardSuit As Suit, ByVal cardFace As Face)
Init()
FCardSuit = cardSuit
FCardFace = cardFace
End Sub
#End Region
#Region "Private class vars"
Private FCardFace As Face
Private FCardSuit As Suit
#End Region
#Region "External methods and related fields"
Private Shared initialized As Boolean = False
Private Shared width As Integer = 75
Private Shared height As Integer = 100
'
Private Declare Function cdtInit Lib "cards.dll" (ByRef width As Integer, ByRef height As Integer) As Boolean
Private Declare Function cdtDrawExt Lib "cards.dll" (ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal card As Integer, ByVal suit As Integer, ByVal color As Integer) As Boolean
Private Declare Sub cdtTerm Lib "cards.dll" ()
#End Region
#Region "Properties"
Public Property CardSuit() As Suit
Get
Return FCardSuit
End Get
Set(ByVal Value As Suit)
FCardSuit = Value
End Set
End Property
#End Region
#Region "Open & Close"
Public Shared Sub Init()
If (initialized) Then Return
initialized = True
cdtInit(width, height)
End Sub
Public Shared Sub Deinit()
If (Not initialized) Then Return
initialized = False
cdtTerm()
End Sub
#End Region
#Region "Painting"
Public Sub PaintGraphicFace(ByVal g As Graphics, ByVal posX As Integer, ByVal posY As Integer)
PaintGraphicFace(g, posX, posY, width, height)
End Sub
Public Sub PaintGraphicFace(ByVal g As Graphics, ByVal posX As Integer, ByVal posY As Integer, ByVal sizeX As Integer, ByVal sizeY As Integer)
Dim hdc As IntPtr = g.GetHdc()
Try
Dim Card As Integer = CType(Me.FCardFace, Integer) * 4 + CType(Me.FCardSuit, Integer)
cdtDrawExt(hdc, posX, posY, sizeX, sizeY, Card, 0, 0)
Finally
g.ReleaseHdc(hdc)
End Try
End Sub
Public Sub PaintGraphicBack(ByVal g As Graphics, ByVal x As Integer, ByVal y As Integer)
PaintGraphicBack(g, x, y, width, height)
End Sub
Public Sub PaintGraphicBack(ByVal g As Graphics, ByVal x As Integer, _
ByVal y As Integer, ByVal dx As Integer, ByVal dy As Integer)
Dim hdc As IntPtr = g.GetHdc()
Try
cdtDrawExt(hdc, x, y, dx, dy, 59, 1, 0)
Finally
g.ReleaseHdc(hdc)
End Try
End Sub
#End Region
End Class
Public Enum Suit
Diamond = 1
Heart = 2
Spade = 3
Club = 4
End Enum
Public Enum Face
Ace = 0
Two = 1
Three = 2
Four = 3
Five = 4
Six = 5
Seven = 6
Eight = 7
Nine = 8
Ten = 9
Jack = 10
Queen = 11
King = 12
End Enum
要走到这一步需要做一点工作/挖掘
Now Card.vb:
Imports System.Runtime.InteropServices
Public Class Form1
Private SQ As Card = New Card(Suit.Spade, Face.Queen)
Private DK As Card = New Card(Suit.Diamond, Face.King)
Private C10 As Card = New Card(Suit.Club, Face.Ten)
Private H2 As Card = New Card(Suit.Heart, Face.Two)
Private SA As Card = New Card(Suit.Spade, Face.Ace)
#Region "Form Events"
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' MyBase.Load
Card.Init()
End Sub
Private Sub Main_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Card.Deinit()
End Sub
#End Region
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
SQ.PaintGraphicFace(e.Graphics, 10, 10)
DK.PaintGraphicFace(e.Graphics, 50, 10)
C10.PaintGraphicFace(e.Graphics, 110, 200)
C10.PaintGraphicBack(e.Graphics, 30, 300)
H2.PaintGraphicBack(e.Graphics, 250, 370)
SA.PaintGraphicFace(e.Graphics, 200, 50)
End Sub
End Class
Public Class Card
#Region "Construcor"
Public Sub New(ByVal cardSuit As Suit, ByVal cardFace As Face)
Init()
FCardSuit = cardSuit
FCardFace = cardFace
End Sub
#End Region
#Region "Private class vars"
Private FCardFace As Face
Private FCardSuit As Suit
#End Region
#Region "External methods and related fields"
Private Shared initialized As Boolean = False
Private Shared width As Integer = 75
Private Shared height As Integer = 100
'
Private Declare Function cdtInit Lib "cards.dll" (ByRef width As Integer, ByRef height As Integer) As Boolean
Private Declare Function cdtDrawExt Lib "cards.dll" (ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal card As Integer, ByVal suit As Integer, ByVal color As Integer) As Boolean
Private Declare Sub cdtTerm Lib "cards.dll" ()
#End Region
#Region "Properties"
Public Property CardSuit() As Suit
Get
Return FCardSuit
End Get
Set(ByVal Value As Suit)
FCardSuit = Value
End Set
End Property
#End Region
#Region "Open & Close"
Public Shared Sub Init()
If (initialized) Then Return
initialized = True
cdtInit(width, height)
End Sub
Public Shared Sub Deinit()
If (Not initialized) Then Return
initialized = False
cdtTerm()
End Sub
#End Region
#Region "Painting"
Public Sub PaintGraphicFace(ByVal g As Graphics, ByVal posX As Integer, ByVal posY As Integer)
PaintGraphicFace(g, posX, posY, width, height)
End Sub
Public Sub PaintGraphicFace(ByVal g As Graphics, ByVal posX As Integer, ByVal posY As Integer, ByVal sizeX As Integer, ByVal sizeY As Integer)
Dim hdc As IntPtr = g.GetHdc()
Try
Dim Card As Integer = CType(Me.FCardFace, Integer) * 4 + CType(Me.FCardSuit, Integer)
cdtDrawExt(hdc, posX, posY, sizeX, sizeY, Card, 0, 0)
Finally
g.ReleaseHdc(hdc)
End Try
End Sub
Public Sub PaintGraphicBack(ByVal g As Graphics, ByVal x As Integer, ByVal y As Integer)
PaintGraphicBack(g, x, y, width, height)
End Sub
Public Sub PaintGraphicBack(ByVal g As Graphics, ByVal x As Integer, _
ByVal y As Integer, ByVal dx As Integer, ByVal dy As Integer)
Dim hdc As IntPtr = g.GetHdc()
Try
cdtDrawExt(hdc, x, y, dx, dy, 59, 1, 0)
Finally
g.ReleaseHdc(hdc)
End Try
End Sub
#End Region
End Class
Public Enum Suit
Diamond = 1
Heart = 2
Spade = 3
Club = 4
End Enum
Public Enum Face
Ace = 0
Two = 1
Three = 2
Four = 3
Five = 4
Six = 5
Seven = 6
Eight = 7
Nine = 8
Ten = 9
Jack = 10
Queen = 11
King = 12
End Enum
对原始资料进行了一些调整,使其更有意义,并消除了多年来示例中的大量拼写错误。。。奇怪的
不管怎样,上面的内容足以让人立刻画卡片。我找不到任何这样有效的例子。唯一的陷阱是什么?将cards.dll与exe放在同一目录中,否则环境会在其他地方找到它
由于对hdc的调用,我找不到与WPF有任何相似之处。基于,您可以使用a来绘制(您需要在WPF应用程序中添加对System.Drawing
的引用),并使用该位图作为WPF控件的源代码
MainWindow.xaml
MainWindow.xaml.vb
导入系统图形
导入System.Windows.Interop
类主窗口
私人SQ As卡=新卡(套装、黑桃、脸、女王)
私人DK As卡=新卡(Suit.Diamond,Face.King)
私人C10 As卡=新卡(Suit.Club,Face.Ten)
私人H2 As卡=新卡(套装、心形、面部、二)
私人SA As卡=新卡(Suit.Spade,Face.Ace)
Private Sub Main Window_Loaded(发件人作为对象,e作为RoutedEventTargets)处理我。Loaded
使用tempBitmap=新位图(10001000)
使用g=Graphics.FromImage(tempBitmap)
平方漆面(g、10、10)
DK.漆面(g、50、10)
C10.漆面(g、110、200)
C10.油漆背面(g、30、300)
H2.油漆背面(g、250、370)
SA.漆面(g、200、50)
Dim hbmp=tempBitmap.GetHbitmap()
Dim options=BitmapSizeOptions.FromEmptyOptions()
Me.myImage.Source=Imaging.CreateBitmapSourceFromHBitmap(hbmp,
IntPtr.Zero、Int32Rect.Empty、选项)
终端使用
终端使用
Me.myImage.InvalidateMeasure()
Me.myImage.InvalidateVisual()
端接头
末级
您必须从WPF教程开始。好的,Mods。。。显然,我写得很好,得到了一个符合问题的答案,并做了标记。你有什么建议?谢谢,@Mark,这太棒了!现在我已经完成了所有的工作,我怀疑使用位图作为接口/输出是否是一个好主意。。。我倾向于WPF和个人图像。。。首先,命中检测会更容易,动画也比WinForm中的重画容易得多。。。尽管如此,我还是会为上面的测试创建一个Winform版本,并将其放在我的博客上供其他人使用!再次感谢!