-
Aug 21st, 2015, 01:21 AM
#1
Thread Starter
Hyperactive Member
WinForm Code to WPF
I have the following code which works in WinForms but I need to redo this in WPF.
Can someone either please help me get started or rewrite this for WPF.
WPF has a Image not a picturebox. Exactly what else is different and where do I start in WPF?
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myMethod(78.5, PictureBox1)
End Sub
Private Sub myMethod(ByVal PP As Single, ByVal PB As PictureBox)
PB.BackColor = Color.Transparent
'Do your drawing here
Dim img As New Bitmap(PB.Width, PB.Height)
Dim gr As Graphics = Graphics.FromImage(img)
gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
gr.Clear(Color.Transparent)
Dim rect As Rectangle = PictureBox1.ClientRectangle
rect.Inflate(-10, -10) 'margin of outer circle
Dim startAngle As Single = 0
Dim sweepAngle As Single = CSng(PP / 100 * 360)
'draw the pie
gr.FillPie(Brushes.AliceBlue, rect, startAngle, sweepAngle)
'draw the centre circle
rect.Inflate(-5, -5) 'doughnut thickness
gr.FillEllipse(Brushes.Black, rect)
'draw the value text
Dim str As String = (PP / 100).ToString("P1")
Using fnt As New Font(Me.Font.FontFamily, 30)
Using sf As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
gr.DrawString(str, fnt, Brushes.White, rect, sf)
End Using
End Using
Using sfnt As New Font(Me.Font.FontFamily, 12)
Using sf As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Near}
gr.DrawString("Cars", sfnt, Brushes.White, rect, sf)
End Using
End Using
PB.Image = img
End Sub
End Class
-
Aug 26th, 2015, 07:40 AM
#2
Re: WinForm Code to WPF
Especially when it comes to custom painting, WPF is another world and there's often no direct conversion. What you're doing is doable in WPF, but you take a very different approach.
In Windows Forms, you have to get a Graphics context to draw anything.Once you have that graphics context, you call methods that manipulate the bits in the bitmap, then let the image control display the result.
WPF has a concept of WriteableBitmap, but does not have direct drawing operations. Instead, you compose scenes that consist of elements, and Windows displays that scene. So to draw on an image, you'd create a scene with an Image element, then layer other elements on top.
I'm not at a machine on which I can experiment, but from the top of your code to the bottom, you'd compose an element with something like this:
- An Image element with its ImageSource set to the image you're overlaying.
- A Path element, with instructions to draw a portion of an ellipse. (This page has an example)
- An Ellipse element.
- Two labels.
The main difference to get used to in WPF is you don't ever get to directly manipulate pixels in the way GDI does. You have to describe everything in terms of the elements that exist. But that's a heck of an oversimplification, there's at least half a dozen concepts to discuss and I've never seen it laid out in a way I like. Josh Smith's guided tour is a good start, but doesn't really focus on applications or composing controls in this way. That's very typical, the bulk of the WPF community somehow seems to never need to create custom elements, or at least they never take the time to blog about it.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|