コード例 #1
0
// Change the window's icon
func (wi *windowInternal) setIcon(icon image.Image) error {
	// First destroy the previous one
	if wi.icon != nil {
		C.DestroyIcon(wi.icon)
	}

	type BGRA [4]uint8

	// Windows wants BGRA pixels: swap red and blue channels
	Rect := icon.Bounds()
	iconPixels := make([]BGRA, Rect.Dy()*Rect.Dx())
	for i, y := 0, 0; y < Rect.Dy(); y++ {
		for x := 0; x < Rect.Dx(); x++ {
			r, g, b, a := icon.At(x, y).RGBA()
			iconPixels[i][0] = uint8(b)
			iconPixels[i][1] = uint8(g)
			iconPixels[i][2] = uint8(r)
			iconPixels[i][3] = uint8(a)
		}
	}

	// Create the icon from the pixel array
	width, height := C.int(Rect.Dx()), C.int(Rect.Dy())
	wi.icon = C.CreateIcon(C.GetModuleHandle(nil), width, height, 1, 32, nil, (*C.BYTE)(&iconPixels[0][0]))

	if wi.icon == nil {
		return fmt.Errorf("could not create icon (%d)", C.GetLastError())
	}

	C.SendMessage(wi.window.Handle, C.WM_SETICON, C.ICON_BIG, C.LPARAM(uintptr(unsafe.Pointer(wi.icon))))
	C.SendMessage(wi.window.Handle, C.WM_SETICON, C.ICON_SMALL, C.LPARAM(uintptr(unsafe.Pointer(wi.icon))))

	return nil
}
コード例 #2
0
func (wi *windowInternal) close() ThreadError {
	// Destroy the custom icon, if any
	if wi.icon != nil {
		C.DestroyIcon(wi.icon)
	}

	if wi.callback == nil {
		// Destroy the window
		if wi.window.IsValid() {
			C.DestroyWindow(wi.window.Handle)
		}
	} else {
		// The window is external : remove the hook on its message callback
		C.__SetWindowLongPtr(wi.window.Handle, C.GWLP_WNDPROC, wi.callback)
	}

	return nil
}