Monday, 15 February 2010

.net - How to migrate this Winform app to WPF? -



.net - How to migrate this Winform app to WPF? -

i resurrected code 10 years ago draw cards (uses cards.dll). i'll have programme manipulate them. got working in winform, figure out way access graphics in wpf.

it works i'll paste it, 1 time again winform:

form1.vb:

imports system.runtime.interopservices public class form1 private sq card = new card(suit.spade, face.queen) private dk card = new card(suit.diamond, face.king) private c10 card = new card(suit.club, face.ten) private h2 card = new card(suit.heart, face.two) private sa card = new card(suit.spade, face.ace) #region "form events" private sub main_load(byval sender system.object, byval e system.eventargs) handles mybase.load ' mybase.load card.init() end sub private sub main_closing(byval sender object, byval e system.componentmodel.canceleventargs) handles mybase.closing card.deinit() end sub #end part protected overrides sub onpaint(byval e 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

just getting far took bit of work/digging!

now card.vb:

public class card #region "construcor" public sub new(byval cardsuit suit, byval cardface face) init() fcardsuit = cardsuit fcardface = cardface end sub #end part #region "private class vars" private fcardface face private fcardsuit suit #end part #region "external methods , related fields" private shared initialized boolean = false private shared width integer = 75 private shared height integer = 100 ' private declare function cdtinit lib "cards.dll" (byref width integer, byref height integer) boolean private declare function cdtdrawext lib "cards.dll" (byval hdc intptr, byval x integer, byval y integer, byval dx integer, byval dy integer, byval card integer, byval suit integer, byval color integer) boolean private declare sub cdtterm lib "cards.dll" () #end part #region "properties" public property cardsuit() suit homecoming fcardsuit end set(byval value suit) fcardsuit = value end set end property #end part #region "open & close" public shared sub init() if (initialized) homecoming initialized = true cdtinit(width, height) end sub public shared sub deinit() if (not initialized) homecoming initialized = false cdtterm() end sub #end part #region "painting" public sub paintgraphicface(byval g graphics, byval posx integer, byval posy integer) paintgraphicface(g, posx, posy, width, height) end sub public sub paintgraphicface(byval g graphics, byval posx integer, byval posy integer, byval sizex integer, byval sizey integer) dim hdc intptr = g.gethdc() seek dim card integer = ctype(me.fcardface, integer) * 4 + ctype(me.fcardsuit, integer) cdtdrawext(hdc, posx, posy, sizex, sizey, card, 0, 0) g.releasehdc(hdc) end seek end sub public sub paintgraphicback(byval g graphics, byval x integer, byval y integer) paintgraphicback(g, x, y, width, height) end sub public sub paintgraphicback(byval g graphics, byval x integer, _ byval y integer, byval dx integer, byval dy integer) dim hdc intptr = g.gethdc() seek cdtdrawext(hdc, x, y, dx, dy, 59, 1, 0) g.releasehdc(hdc) end seek end sub #end part end class public enum suit diamond = 1 heart = 2 spade = 3 club = 4 end enum public enum face ace = 0 2 = 1 3 = 2 4 = 3 5 = 4 6 = 5 7 = 6 8 = 7 9 = 8 10 = 9 jack = 10 queen = 11 king = 12 end enum

tweaked bit original sources create more sense, , rid of lots of typos in examples on years... weird...

anyway, above plenty painting cards immediately. not find such working examples. catch? set cards.dll in same dir exe, or somewhere else environ find it...

because of calls hdc, can't find parallels wpf.

based on this answer, can utilize bitmap draw on (you need add together reference system.drawing in wpf application), , utilize bitmap source wpf image control.

mainwindow.xaml

class="lang-xml prettyprint-override"><window x:class="mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="600" width="600"> <dockpanel lastchildfill="true"> <scrollviewer horizontalscrollbarvisibility="auto" verticalscrollbarvisibility="auto"> <image name="myimage" stretch="none"/> </scrollviewer> </dockpanel> </window>

mainwindow.xaml.vb

class="lang-vb prettyprint-override">imports system.drawing imports system.windows.interop class mainwindow private sq card = new card(suit.spade, face.queen) private dk card = new card(suit.diamond, face.king) private c10 card = new card(suit.club, face.ten) private h2 card = new card(suit.heart, face.two) private sa card = new card(suit.spade, face.ace) private sub mainwindow_loaded(sender object, e routedeventargs) handles me.loaded using tempbitmap = new bitmap(1000, 1000) using g = graphics.fromimage(tempbitmap) sq.paintgraphicface(g, 10, 10) dk.paintgraphicface(g, 50, 10) c10.paintgraphicface(g, 110, 200) c10.paintgraphicback(g, 30, 300) h2.paintgraphicback(g, 250, 370) sa.paintgraphicface(g, 200, 50) dim hbmp = tempbitmap.gethbitmap() dim options = bitmapsizeoptions.fromemptyoptions() me.myimage.source = imaging.createbitmapsourcefromhbitmap(hbmp, intptr.zero, int32rect.empty, options) end using end using me.myimage.invalidatemeasure() me.myimage.invalidatevisual() end sub end class

.net wpf vb.net winforms

No comments:

Post a Comment