PDA

View Full Version : معادل hdc در VB.NET



مطهر
پنج شنبه 19 آذر 1383, 10:41 صبح
با سلام
معادل hdc در VB.NET چیست
Form1.hdc
همچنین معادل Hwnd
با سپاس

Farhad.B.S
پنج شنبه 19 آذر 1383, 14:29 عصر
معادل Hwnd :

FormName.Handle
برای بدست آوردن hdc ابتدا باید یک سطح گرافیکی با استفاده از متد CreateGraphics ایجاد کنید :

FormName.CreateGraphics().GetHdc()

علیرضا مداح
پنج شنبه 19 آذر 1383, 15:01 عصر
سلام .


معادل hdc در VB.NET چیست

مثالی از MSDN :


<System.Runtime.InteropServices.DllImportAttribute ("gdi32.dll")> _
Private Shared Function Rectangle(hdc As IntPtr, _
ulCornerX As Integer, ulCornerY As Integer, lrCornerX As Integer, _
lrCornerY As Integer) As Boolean
End Function
Public Sub GetHdcForGDI(e As PaintEventArgs)
' Create pen.
Dim redPen As New Pen(Color.Red, 1)
' Draw rectangle with GDI+.
e.Graphics.DrawRectangle(redPen, 10, 10, 100, 50)
' Get handle to device context.
Dim hdc As IntPtr = e.Graphics.GetHdc()
' Draw rectangle with GDI using default pen.
Rectangle(hdc, 10, 70, 110, 120)
' Release handle to device context.
e.Graphics.ReleaseHdc(hdc)
End Sub

ROSTAM2
جمعه 09 تیر 1391, 16:43 عصر
حالا .....


Imports System.Runtime.InteropServices
Public Class Form1
Dim Grp As Graphics
Dim hDC As HandleRef

Private Sub Form1_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleCreated
Grp = Me.CreateGraphics
hDC = New HandleRef(Grp, Grp.GetHdc)
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
DirectCast(hDC.Wrapper, Graphics).ReleaseHdc()
Grp.Dispose()
End Sub
End Class

Veteran
سه شنبه 17 مرداد 1391, 00:15 صبح
:متفکر:
اخر نفهمیدم این hdc رو چجور به دست بیارم
توی vb6 راحت میشد
form1.hdc

the king
سه شنبه 17 مرداد 1391, 00:29 صبح
:متفکر:
اخر نفهمیدم این hdc رو چجور به دست بیارم
توی vb6 راحت میشد
form1.hdc

دقت کنید که hDC یک منبع موقت و امانتی یه، اگر تحویل اش گرفتید امانت بهتون داده شده، تا کار تون باهاش تمام شد آزاد اش کنید.
مقدارش رو هم در حین عملیات های مجزا نگهداری نکنید چون ممکنه بنا به تغییرات در تنظیمات صفحه نمایش و کارت گرافیکی شماره اش عوض بشه :

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim hDC As IntPtr = e.Graphics.GetHdc()
'
'
'
Dim n As Integer = hDC.ToInt32()
'
'
'
e.Graphics.ReleaseHdc(hDC)
End Sub


یا اگه در خارچ از رخداد Paint هستید و Graphics آماده نیست :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using g As Graphics = Me.CreateGraphics()
Dim hDC As IntPtr = g.GetHdc()
'
'
'
Dim n As Integer = hDC.ToInt32()
'
'
'
g.ReleaseHdc(hDC)
End Using
End Sub


یا اصلا هنوز Form1 رو نمایش نداده اید :

Dim f1 As New Form1
Using g As Graphics = f1.CreateGraphics()
Dim hDC As IntPtr = g.GetHdc()
'
'
'
g.ReleaseHdc(hDC)
End Using