Go/window_api

[Go] Windows API - 창 위치/크기 조정

_HelloWorld_ 2025. 3. 5. 15:26

윈도우 핸들을 사용해서 창의 위치, 크기, Z-Order(창 순서) 등을 바꿉니다.

 

package window

import (
	"syscall"
	"unsafe"
)

func FindWindows(mod *syscall.LazyDLL) uintptr {
	proc := mod.NewProc("FindWindowW")

	className := syscall.StringToUTF16Ptr("Notepad")

	hWnd, _, err := proc.Call(uintptr(unsafe.Pointer(className)), 0)
	if hWnd == 0 {
		panic(err)
	}

	return hWnd
}

func SetWindowPosition(mod *syscall.LazyDLL, hWnd uintptr, x, y, width, height int32) {
	proc := mod.NewProc("SetWindowPos")

	proc.Call(
		hWnd,
		uintptr(0), // Z-Order
		uintptr(x),
		uintptr(y),
		uintptr(width),
		uintptr(height),
		0x0040, // 플래그 SWP_SHOWWINDOW
	)
}
func callSetWindowPosition() {
	mod := syscall.NewLazyDLL("user32.dll")

	hWnd := window.FindWindows(mod)

	window.SetWindowPosition(mod, hWnd, 0, 0, 800, 600)
}

 

결과

  1. 메모장 열기 (창 제목: 상관없음)
  2. 이 코드를 실행하면:
    • 메모장이 화면의 (0, 0) 위치로 이동
    • 창 크기는 800x600으로 변경

 

SetWindowPos 플래그 값

  • 0x0040: SWP_SHOWWINDOW → 창 보이기
  • 0x0001: SWP_NOSIZE → 크기 변경 안 함
  • 0x0002: SWP_NOMOVE → 위치 변경 안 함
  • 0x0200: SWP_NOACTIVATE → 창 활성화 안 함