网上的确有,不过不知道为什么我那里运行不了。这里提供的代码是paint在panel上的。.net真好啊,不用设备上下文,gethdc默认还是隐藏的……我找来找去都找不到这个方法……
几点注意:
1.记得要把dll初始化,function cdtInit。两个参数,在网上找吧。
2.要redraw panel,用:Panel1.Invalidate()。
Public Class Form1
Inherits System.Windows.Forms.Form
Dim pcards(3) As card
Dim p As card
Dim i As Integer
Declare Function cdtInit Lib "cards.dll" (ByRef width As Integer, _
ByRef height As Integer) As Boolean
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 Long) As Boolean
Declare Sub cdtTerm Lib "cards.dll" ()

Public Structure card
Public face As Integer
Public suit As Integer
Public count As Integer
Public faceup As Boolean
End Structure


Windows 窗体设计器生成的代码


Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim ct As Int16 = 0
For ct = 1 To 3
drawcard(pcards(ct), e.Graphics, 10 + ct * 20, 30, 75, 100)
Next
'For Each p In pcards
'drawcard(p, e.Graphics, 10 + ct * 10, 30, 75, 100)
'ct += 1
'Next

End Sub
Private Sub drawcard(ByVal t As card, 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
Dim Card As Integer = t.suit * 4 + t.count
cdtDrawExt(hdc, x, y, dx, dy, Card, 0, 0)
Finally
' If Intellisense doesn't show this method
' unhide advanced members in Tools|Options
g.ReleaseHdc(hdc)
End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cdtInit(75, 100)

For i = 1 To 3
pcards(i) = New card()
With pcards(i)
.suit = i
.count = i + 1
End With
Next
Panel1.Invalidate()
End Sub
End Class

几点注意:
1.记得要把dll初始化,function cdtInit。两个参数,在网上找吧。
2.要redraw panel,用:Panel1.Invalidate()。
Public Class Form1
Inherits System.Windows.Forms.Form
Dim pcards(3) As card
Dim p As card
Dim i As Integer
Declare Function cdtInit Lib "cards.dll" (ByRef width As Integer, _
ByRef height As Integer) As Boolean
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 Long) As Boolean
Declare Sub cdtTerm Lib "cards.dll" ()
Public Structure card
Public face As Integer
Public suit As Integer
Public count As Integer
Public faceup As Boolean
End Structure

Windows 窗体设计器生成的代码

Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim ct As Int16 = 0
For ct = 1 To 3
drawcard(pcards(ct), e.Graphics, 10 + ct * 20, 30, 75, 100)
Next
'For Each p In pcards
'drawcard(p, e.Graphics, 10 + ct * 10, 30, 75, 100)
'ct += 1
'Next
End Sub
Private Sub drawcard(ByVal t As card, 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
Dim Card As Integer = t.suit * 4 + t.count
cdtDrawExt(hdc, x, y, dx, dy, Card, 0, 0)
Finally
' If Intellisense doesn't show this method
' unhide advanced members in Tools|Options
g.ReleaseHdc(hdc)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cdtInit(75, 100)
For i = 1 To 3
pcards(i) = New card()
With pcards(i)
.suit = i
.count = i + 1
End With
Next
Panel1.Invalidate()
End Sub
End Class


浙公网安备 33010602011771号