Пример #1
0
func withCompatibleDC(f func(hdc win.HDC) error) error {
	hdc := win.CreateCompatibleDC(0)
	if hdc == 0 {
		return newError("CreateCompatibleDC failed")
	}
	defer win.DeleteDC(hdc)

	return f(hdc)
}
Пример #2
0
func (cw *CustomWidget) bufferedPaint(canvas *Canvas, updateBounds Rectangle) error {
	hdc := win.CreateCompatibleDC(canvas.hdc)
	if hdc == 0 {
		return newError("CreateCompatibleDC failed")
	}
	defer win.DeleteDC(hdc)

	buffered := Canvas{hdc: hdc, doNotDispose: true}
	if _, err := buffered.init(); err != nil {
		return err
	}

	w, h := int32(updateBounds.Width), int32(updateBounds.Height)
	if w < 1 {
		w = 1
	}
	if h < 1 {
		h = 1
	}
	hbmp := win.CreateCompatibleBitmap(canvas.hdc, w, h)
	if hbmp == 0 {
		return lastError("CreateCompatibleBitmap failed")
	}
	defer win.DeleteObject(win.HGDIOBJ(hbmp))

	oldbmp := win.SelectObject(buffered.hdc, win.HGDIOBJ(hbmp))
	if oldbmp == 0 {
		return newError("SelectObject failed")
	}
	defer win.SelectObject(buffered.hdc, oldbmp)

	win.SetViewportOrgEx(buffered.hdc, -int32(updateBounds.X), -int32(updateBounds.Y), nil)
	win.SetBrushOrgEx(buffered.hdc, -int32(updateBounds.X), -int32(updateBounds.Y), nil)

	err := cw.paint(&buffered, updateBounds)

	if !win.BitBlt(canvas.hdc,
		int32(updateBounds.X), int32(updateBounds.Y), w, h,
		buffered.hdc,
		int32(updateBounds.X), int32(updateBounds.Y), win.SRCCOPY) {
		return lastError("buffered BitBlt failed")
	}

	return err
}
Пример #3
0
func NewCanvasFromImage(image Image) (*Canvas, error) {
	switch img := image.(type) {
	case *Bitmap:
		hdc := win.CreateCompatibleDC(0)
		if hdc == 0 {
			return nil, newError("CreateCompatibleDC failed")
		}
		succeeded := false

		defer func() {
			if !succeeded {
				win.DeleteDC(hdc)
			}
		}()

		if win.SelectObject(hdc, win.HGDIOBJ(img.hBmp)) == 0 {
			return nil, newError("SelectObject failed")
		}

		succeeded = true

		return (&Canvas{hdc: hdc}).init()

	case *Metafile:
		c, err := newCanvasFromHDC(img.hdc)
		if err != nil {
			return nil, err
		}

		c.recordingMetafile = img

		return c, nil
	}

	return nil, newError("unsupported image type")
}
Пример #4
0
func hBitmapFromWindow(window Window) (win.HBITMAP, error) {
	hdcMem := win.CreateCompatibleDC(0)
	if hdcMem == 0 {
		return 0, newError("CreateCompatibleDC failed")
	}
	defer win.DeleteDC(hdcMem)

	var r win.RECT
	if !win.GetWindowRect(window.Handle(), &r) {
		return 0, newError("GetWindowRect failed")
	}

	hdc := win.GetDC(window.Handle())
	width, height := r.Right-r.Left, r.Bottom-r.Top
	hBmp := win.CreateCompatibleBitmap(hdc, width, height)
	win.ReleaseDC(window.Handle(), hdc)

	hOld := win.SelectObject(hdcMem, win.HGDIOBJ(hBmp))
	flags := win.PRF_CHILDREN | win.PRF_CLIENT | win.PRF_ERASEBKGND | win.PRF_NONCLIENT | win.PRF_OWNED
	window.SendMessage(win.WM_PRINT, uintptr(hdcMem), uintptr(flags))

	win.SelectObject(hdcMem, hOld)
	return hBmp, nil
}