0.0.14 Added drawing area. /JL

This commit is contained in:
2022-12-12 00:11:28 +01:00
parent da934131f4
commit 771d15aabc
10 changed files with 148 additions and 3 deletions
+13
View File
@@ -0,0 +1,13 @@
package main
import "github.com/lxn/walk"
type (
Canvas struct {
*walk.Canvas
}
)
func NewCanvas() *Canvas {
return new(Canvas)
}
+4
View File
@@ -0,0 +1,4 @@
Most image files in this directory are from the base icon theme of the
Tango Desktop Project at http://tango.freedesktop.org.
Thanks for releasing those to the Public Domain.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 369 B

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

+131 -3
View File
@@ -2,6 +2,7 @@ package main
import (
"log"
"math"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
@@ -10,7 +11,8 @@ import (
type MyMainWindow struct {
*walk.MainWindow
colors *walk.ScrollView
canvas *walk.ScrollView
canvasscroll *walk.ScrollView
drawWidget *walk.CustomWidget
properties *walk.ScrollView
pallette Pallette
beads []*BeadColor
@@ -24,7 +26,7 @@ type MyMainWindow struct {
const (
AppName string = "BeadImager"
Version string = "0.0.13"
Version string = "0.0.14"
CopyRight string = "©2022 Jan Lerking"
STD_MESS string = "Ready"
UserPath string = "C:\\Users\\janle\\BeadImager"
@@ -163,8 +165,16 @@ func main() {
Layout: VBox{},
Children: []Widget{
ScrollView{
AssignTo: &mw.canvas,
AssignTo: &mw.canvasscroll,
Layout: VBox{MarginsZero: true},
Children: []Widget{
CustomWidget{
AssignTo: &mw.drawWidget,
ClearsBackground: true,
InvalidatesOnResize: true,
Paint: mw.drawStuff,
},
},
},
},
},
@@ -186,3 +196,121 @@ func main() {
log.Fatal(err)
}
}
func (mw *MyMainWindow) drawStuff(canvas *walk.Canvas, updateBounds walk.Rectangle) error {
bmp, err := createBitmap()
if err != nil {
return err
}
defer bmp.Dispose()
bounds := mw.drawWidget.ClientBounds()
rectPen, err := walk.NewCosmeticPen(walk.PenSolid, walk.RGB(255, 0, 0))
if err != nil {
return err
}
defer rectPen.Dispose()
if err := canvas.DrawRectangle(rectPen, bounds); err != nil {
return err
}
ellipseBrush, err := walk.NewHatchBrush(walk.RGB(0, 255, 0), walk.HatchCross)
if err != nil {
return err
}
defer ellipseBrush.Dispose()
if err := canvas.FillEllipse(ellipseBrush, bounds); err != nil {
return err
}
linesBrush, err := walk.NewSolidColorBrush(walk.RGB(0, 0, 255))
if err != nil {
return err
}
defer linesBrush.Dispose()
linesPen, err := walk.NewGeometricPen(walk.PenDash, 8, linesBrush)
if err != nil {
return err
}
defer linesPen.Dispose()
if err := canvas.DrawLine(linesPen, walk.Point{bounds.X, bounds.Y}, walk.Point{bounds.Width, bounds.Height}); err != nil {
return err
}
if err := canvas.DrawLine(linesPen, walk.Point{bounds.X, bounds.Height}, walk.Point{bounds.Width, bounds.Y}); err != nil {
return err
}
points := make([]walk.Point, 10)
dx := bounds.Width / (len(points) - 1)
for i := range points {
points[i].X = i * dx
points[i].Y = int(float64(bounds.Height) / math.Pow(float64(bounds.Width/2), 2) * math.Pow(float64(i*dx-bounds.Width/2), 2))
}
if err := canvas.DrawPolyline(linesPen, points); err != nil {
return err
}
bmpSize := bmp.Size()
if err := canvas.DrawImage(bmp, walk.Point{(bounds.Width - bmpSize.Width) / 2, (bounds.Height - bmpSize.Height) / 2}); err != nil {
return err
}
return nil
}
func createBitmap() (*walk.Bitmap, error) {
bounds := walk.Rectangle{Width: 200, Height: 200}
bmp, err := walk.NewBitmap(bounds.Size())
if err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
bmp.Dispose()
}
}()
canvas, err := walk.NewCanvasFromImage(bmp)
if err != nil {
return nil, err
}
defer canvas.Dispose()
brushBmp, err := walk.NewBitmapFromFile("images/plus.png")
if err != nil {
return nil, err
}
defer brushBmp.Dispose()
brush, err := walk.NewBitmapBrush(brushBmp)
if err != nil {
return nil, err
}
defer brush.Dispose()
if err := canvas.FillRectangle(brush, bounds); err != nil {
return nil, err
}
font, err := walk.NewFont("Times New Roman", 40, walk.FontBold|walk.FontItalic)
if err != nil {
return nil, err
}
defer font.Dispose()
if err := canvas.DrawText("Walk Drawing Example", font, walk.RGB(0, 0, 0), bounds, walk.TextWordbreak); err != nil {
return nil, err
}
succeeded = true
return bmp, nil
}