github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/windows/winc/imageview.go (about)

     1  //go:build windows
     2  
     3  /*
     4   * Copyright (C) 2019 The Winc Authors. All Rights Reserved.
     5   */
     6  
     7  package winc
     8  
     9  import "github.com/secoba/wails/v2/internal/frontend/desktop/windows/winc/w32"
    10  
    11  type ImageView struct {
    12  	ControlBase
    13  
    14  	bmp *Bitmap
    15  }
    16  
    17  func NewImageView(parent Controller) *ImageView {
    18  	iv := new(ImageView)
    19  
    20  	iv.InitWindow("winc_ImageView", parent, w32.WS_EX_CONTROLPARENT, w32.WS_CHILD|w32.WS_VISIBLE)
    21  	RegMsgHandler(iv)
    22  
    23  	iv.SetFont(DefaultFont)
    24  	iv.SetText("")
    25  	iv.SetSize(200, 65)
    26  	return iv
    27  }
    28  
    29  func (iv *ImageView) DrawImageFile(filepath string) error {
    30  	bmp, err := NewBitmapFromFile(filepath, RGB(255, 255, 0))
    31  	if err != nil {
    32  		return err
    33  	}
    34  	iv.bmp = bmp
    35  	return nil
    36  }
    37  
    38  func (iv *ImageView) DrawImage(bmp *Bitmap) {
    39  	iv.bmp = bmp
    40  }
    41  
    42  func (iv *ImageView) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
    43  	switch msg {
    44  	case w32.WM_SIZE, w32.WM_SIZING:
    45  		iv.Invalidate(true)
    46  
    47  	case w32.WM_ERASEBKGND:
    48  		return 1 // important
    49  
    50  	case w32.WM_PAINT:
    51  		if iv.bmp != nil {
    52  			canvas := NewCanvasFromHwnd(iv.hwnd)
    53  			defer canvas.Dispose()
    54  			iv.SetSize(iv.bmp.Size())
    55  			canvas.DrawBitmap(iv.bmp, 0, 0)
    56  		}
    57  	}
    58  	return w32.DefWindowProc(iv.hwnd, msg, wparam, lparam)
    59  }