gdi32.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // Copyright 2010-2012 The W32 Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build windows
  5. package w32
  6. import (
  7. "syscall"
  8. "unsafe"
  9. )
  10. var (
  11. modgdi32 = syscall.NewLazyDLL("gdi32.dll")
  12. procGetDeviceCaps = modgdi32.NewProc("GetDeviceCaps")
  13. procDeleteObject = modgdi32.NewProc("DeleteObject")
  14. procCreateFontIndirect = modgdi32.NewProc("CreateFontIndirectW")
  15. procAbortDoc = modgdi32.NewProc("AbortDoc")
  16. procBitBlt = modgdi32.NewProc("BitBlt")
  17. procCloseEnhMetaFile = modgdi32.NewProc("CloseEnhMetaFile")
  18. procCopyEnhMetaFile = modgdi32.NewProc("CopyEnhMetaFileW")
  19. procCreateBrushIndirect = modgdi32.NewProc("CreateBrushIndirect")
  20. procCreateCompatibleDC = modgdi32.NewProc("CreateCompatibleDC")
  21. procCreateDC = modgdi32.NewProc("CreateDCW")
  22. procCreateDIBSection = modgdi32.NewProc("CreateDIBSection")
  23. procCreateEnhMetaFile = modgdi32.NewProc("CreateEnhMetaFileW")
  24. procCreateIC = modgdi32.NewProc("CreateICW")
  25. procDeleteDC = modgdi32.NewProc("DeleteDC")
  26. procDeleteEnhMetaFile = modgdi32.NewProc("DeleteEnhMetaFile")
  27. procEllipse = modgdi32.NewProc("Ellipse")
  28. procEndDoc = modgdi32.NewProc("EndDoc")
  29. procEndPage = modgdi32.NewProc("EndPage")
  30. procExtCreatePen = modgdi32.NewProc("ExtCreatePen")
  31. procGetEnhMetaFile = modgdi32.NewProc("GetEnhMetaFileW")
  32. procGetEnhMetaFileHeader = modgdi32.NewProc("GetEnhMetaFileHeader")
  33. procGetObject = modgdi32.NewProc("GetObjectW")
  34. procGetStockObject = modgdi32.NewProc("GetStockObject")
  35. procGetTextExtentExPoint = modgdi32.NewProc("GetTextExtentExPointW")
  36. procGetTextExtentPoint32 = modgdi32.NewProc("GetTextExtentPoint32W")
  37. procGetTextMetrics = modgdi32.NewProc("GetTextMetricsW")
  38. procLineTo = modgdi32.NewProc("LineTo")
  39. procMoveToEx = modgdi32.NewProc("MoveToEx")
  40. procPlayEnhMetaFile = modgdi32.NewProc("PlayEnhMetaFile")
  41. procRectangle = modgdi32.NewProc("Rectangle")
  42. procResetDC = modgdi32.NewProc("ResetDCW")
  43. procSelectObject = modgdi32.NewProc("SelectObject")
  44. procSetBkMode = modgdi32.NewProc("SetBkMode")
  45. procSetBrushOrgEx = modgdi32.NewProc("SetBrushOrgEx")
  46. procSetStretchBltMode = modgdi32.NewProc("SetStretchBltMode")
  47. procSetTextColor = modgdi32.NewProc("SetTextColor")
  48. procSetBkColor = modgdi32.NewProc("SetBkColor")
  49. procStartDoc = modgdi32.NewProc("StartDocW")
  50. procStartPage = modgdi32.NewProc("StartPage")
  51. procStretchBlt = modgdi32.NewProc("StretchBlt")
  52. procSetDIBitsToDevice = modgdi32.NewProc("SetDIBitsToDevice")
  53. procChoosePixelFormat = modgdi32.NewProc("ChoosePixelFormat")
  54. procDescribePixelFormat = modgdi32.NewProc("DescribePixelFormat")
  55. procGetEnhMetaFilePixelFormat = modgdi32.NewProc("GetEnhMetaFilePixelFormat")
  56. procGetPixelFormat = modgdi32.NewProc("GetPixelFormat")
  57. procSetPixelFormat = modgdi32.NewProc("SetPixelFormat")
  58. procSwapBuffers = modgdi32.NewProc("SwapBuffers")
  59. )
  60. func GetDeviceCaps(hdc HDC, index int) int {
  61. ret, _, _ := procGetDeviceCaps.Call(
  62. uintptr(hdc),
  63. uintptr(index))
  64. return int(ret)
  65. }
  66. func DeleteObject(hObject HGDIOBJ) bool {
  67. ret, _, _ := procDeleteObject.Call(
  68. uintptr(hObject))
  69. return ret != 0
  70. }
  71. func CreateFontIndirect(logFont *LOGFONT) HFONT {
  72. ret, _, _ := procCreateFontIndirect.Call(
  73. uintptr(unsafe.Pointer(logFont)))
  74. return HFONT(ret)
  75. }
  76. func AbortDoc(hdc HDC) int {
  77. ret, _, _ := procAbortDoc.Call(
  78. uintptr(hdc))
  79. return int(ret)
  80. }
  81. func BitBlt(hdcDest HDC, nXDest, nYDest, nWidth, nHeight int, hdcSrc HDC, nXSrc, nYSrc int, dwRop uint) {
  82. ret, _, _ := procBitBlt.Call(
  83. uintptr(hdcDest),
  84. uintptr(nXDest),
  85. uintptr(nYDest),
  86. uintptr(nWidth),
  87. uintptr(nHeight),
  88. uintptr(hdcSrc),
  89. uintptr(nXSrc),
  90. uintptr(nYSrc),
  91. uintptr(dwRop))
  92. if ret == 0 {
  93. panic("BitBlt failed")
  94. }
  95. }
  96. func CloseEnhMetaFile(hdc HDC) HENHMETAFILE {
  97. ret, _, _ := procCloseEnhMetaFile.Call(
  98. uintptr(hdc))
  99. return HENHMETAFILE(ret)
  100. }
  101. func CopyEnhMetaFile(hemfSrc HENHMETAFILE, lpszFile *uint16) HENHMETAFILE {
  102. ret, _, _ := procCopyEnhMetaFile.Call(
  103. uintptr(hemfSrc),
  104. uintptr(unsafe.Pointer(lpszFile)))
  105. return HENHMETAFILE(ret)
  106. }
  107. func CreateBrushIndirect(lplb *LOGBRUSH) HBRUSH {
  108. ret, _, _ := procCreateBrushIndirect.Call(
  109. uintptr(unsafe.Pointer(lplb)))
  110. return HBRUSH(ret)
  111. }
  112. func CreateCompatibleDC(hdc HDC) HDC {
  113. ret, _, _ := procCreateCompatibleDC.Call(
  114. uintptr(hdc))
  115. if ret == 0 {
  116. panic("Create compatible DC failed")
  117. }
  118. return HDC(ret)
  119. }
  120. func CreateDC(lpszDriver, lpszDevice, lpszOutput *uint16, lpInitData *DEVMODE) HDC {
  121. ret, _, _ := procCreateDC.Call(
  122. uintptr(unsafe.Pointer(lpszDriver)),
  123. uintptr(unsafe.Pointer(lpszDevice)),
  124. uintptr(unsafe.Pointer(lpszOutput)),
  125. uintptr(unsafe.Pointer(lpInitData)))
  126. return HDC(ret)
  127. }
  128. func CreateDIBSection(hdc HDC, pbmi *BITMAPINFO, iUsage uint, ppvBits *unsafe.Pointer, hSection HANDLE, dwOffset uint) HBITMAP {
  129. ret, _, _ := procCreateDIBSection.Call(
  130. uintptr(hdc),
  131. uintptr(unsafe.Pointer(pbmi)),
  132. uintptr(iUsage),
  133. uintptr(unsafe.Pointer(ppvBits)),
  134. uintptr(hSection),
  135. uintptr(dwOffset))
  136. return HBITMAP(ret)
  137. }
  138. func CreateEnhMetaFile(hdcRef HDC, lpFilename *uint16, lpRect *RECT, lpDescription *uint16) HDC {
  139. ret, _, _ := procCreateEnhMetaFile.Call(
  140. uintptr(hdcRef),
  141. uintptr(unsafe.Pointer(lpFilename)),
  142. uintptr(unsafe.Pointer(lpRect)),
  143. uintptr(unsafe.Pointer(lpDescription)))
  144. return HDC(ret)
  145. }
  146. func CreateIC(lpszDriver, lpszDevice, lpszOutput *uint16, lpdvmInit *DEVMODE) HDC {
  147. ret, _, _ := procCreateIC.Call(
  148. uintptr(unsafe.Pointer(lpszDriver)),
  149. uintptr(unsafe.Pointer(lpszDevice)),
  150. uintptr(unsafe.Pointer(lpszOutput)),
  151. uintptr(unsafe.Pointer(lpdvmInit)))
  152. return HDC(ret)
  153. }
  154. func DeleteDC(hdc HDC) bool {
  155. ret, _, _ := procDeleteDC.Call(
  156. uintptr(hdc))
  157. return ret != 0
  158. }
  159. func DeleteEnhMetaFile(hemf HENHMETAFILE) bool {
  160. ret, _, _ := procDeleteEnhMetaFile.Call(
  161. uintptr(hemf))
  162. return ret != 0
  163. }
  164. func Ellipse(hdc HDC, nLeftRect, nTopRect, nRightRect, nBottomRect int) bool {
  165. ret, _, _ := procEllipse.Call(
  166. uintptr(hdc),
  167. uintptr(nLeftRect),
  168. uintptr(nTopRect),
  169. uintptr(nRightRect),
  170. uintptr(nBottomRect))
  171. return ret != 0
  172. }
  173. func EndDoc(hdc HDC) int {
  174. ret, _, _ := procEndDoc.Call(
  175. uintptr(hdc))
  176. return int(ret)
  177. }
  178. func EndPage(hdc HDC) int {
  179. ret, _, _ := procEndPage.Call(
  180. uintptr(hdc))
  181. return int(ret)
  182. }
  183. func ExtCreatePen(dwPenStyle, dwWidth uint, lplb *LOGBRUSH, dwStyleCount uint, lpStyle *uint) HPEN {
  184. ret, _, _ := procExtCreatePen.Call(
  185. uintptr(dwPenStyle),
  186. uintptr(dwWidth),
  187. uintptr(unsafe.Pointer(lplb)),
  188. uintptr(dwStyleCount),
  189. uintptr(unsafe.Pointer(lpStyle)))
  190. return HPEN(ret)
  191. }
  192. func GetEnhMetaFile(lpszMetaFile *uint16) HENHMETAFILE {
  193. ret, _, _ := procGetEnhMetaFile.Call(
  194. uintptr(unsafe.Pointer(lpszMetaFile)))
  195. return HENHMETAFILE(ret)
  196. }
  197. func GetEnhMetaFileHeader(hemf HENHMETAFILE, cbBuffer uint, lpemh *ENHMETAHEADER) uint {
  198. ret, _, _ := procGetEnhMetaFileHeader.Call(
  199. uintptr(hemf),
  200. uintptr(cbBuffer),
  201. uintptr(unsafe.Pointer(lpemh)))
  202. return uint(ret)
  203. }
  204. func GetObject(hgdiobj HGDIOBJ, cbBuffer uintptr, lpvObject unsafe.Pointer) int {
  205. ret, _, _ := procGetObject.Call(
  206. uintptr(hgdiobj),
  207. uintptr(cbBuffer),
  208. uintptr(lpvObject))
  209. return int(ret)
  210. }
  211. func GetStockObject(fnObject int) HGDIOBJ {
  212. ret, _, _ := procGetDeviceCaps.Call(
  213. uintptr(fnObject))
  214. return HGDIOBJ(ret)
  215. }
  216. func GetTextExtentExPoint(hdc HDC, lpszStr *uint16, cchString, nMaxExtent int, lpnFit, alpDx *int, lpSize *SIZE) bool {
  217. ret, _, _ := procGetTextExtentExPoint.Call(
  218. uintptr(hdc),
  219. uintptr(unsafe.Pointer(lpszStr)),
  220. uintptr(cchString),
  221. uintptr(nMaxExtent),
  222. uintptr(unsafe.Pointer(lpnFit)),
  223. uintptr(unsafe.Pointer(alpDx)),
  224. uintptr(unsafe.Pointer(lpSize)))
  225. return ret != 0
  226. }
  227. func GetTextExtentPoint32(hdc HDC, lpString *uint16, c int, lpSize *SIZE) bool {
  228. ret, _, _ := procGetTextExtentPoint32.Call(
  229. uintptr(hdc),
  230. uintptr(unsafe.Pointer(lpString)),
  231. uintptr(c),
  232. uintptr(unsafe.Pointer(lpSize)))
  233. return ret != 0
  234. }
  235. func GetTextMetrics(hdc HDC, lptm *TEXTMETRIC) bool {
  236. ret, _, _ := procGetTextMetrics.Call(
  237. uintptr(hdc),
  238. uintptr(unsafe.Pointer(lptm)))
  239. return ret != 0
  240. }
  241. func LineTo(hdc HDC, nXEnd, nYEnd int) bool {
  242. ret, _, _ := procLineTo.Call(
  243. uintptr(hdc),
  244. uintptr(nXEnd),
  245. uintptr(nYEnd))
  246. return ret != 0
  247. }
  248. func MoveToEx(hdc HDC, x, y int, lpPoint *POINT) bool {
  249. ret, _, _ := procMoveToEx.Call(
  250. uintptr(hdc),
  251. uintptr(x),
  252. uintptr(y),
  253. uintptr(unsafe.Pointer(lpPoint)))
  254. return ret != 0
  255. }
  256. func PlayEnhMetaFile(hdc HDC, hemf HENHMETAFILE, lpRect *RECT) bool {
  257. ret, _, _ := procPlayEnhMetaFile.Call(
  258. uintptr(hdc),
  259. uintptr(hemf),
  260. uintptr(unsafe.Pointer(lpRect)))
  261. return ret != 0
  262. }
  263. func Rectangle(hdc HDC, nLeftRect, nTopRect, nRightRect, nBottomRect int) bool {
  264. ret, _, _ := procRectangle.Call(
  265. uintptr(hdc),
  266. uintptr(nLeftRect),
  267. uintptr(nTopRect),
  268. uintptr(nRightRect),
  269. uintptr(nBottomRect))
  270. return ret != 0
  271. }
  272. func ResetDC(hdc HDC, lpInitData *DEVMODE) HDC {
  273. ret, _, _ := procResetDC.Call(
  274. uintptr(hdc),
  275. uintptr(unsafe.Pointer(lpInitData)))
  276. return HDC(ret)
  277. }
  278. func SelectObject(hdc HDC, hgdiobj HGDIOBJ) HGDIOBJ {
  279. ret, _, _ := procSelectObject.Call(
  280. uintptr(hdc),
  281. uintptr(hgdiobj))
  282. if ret == 0 {
  283. panic("SelectObject failed")
  284. }
  285. return HGDIOBJ(ret)
  286. }
  287. func SetBkMode(hdc HDC, iBkMode int) int {
  288. ret, _, _ := procSetBkMode.Call(
  289. uintptr(hdc),
  290. uintptr(iBkMode))
  291. if ret == 0 {
  292. panic("SetBkMode failed")
  293. }
  294. return int(ret)
  295. }
  296. func SetBrushOrgEx(hdc HDC, nXOrg, nYOrg int, lppt *POINT) bool {
  297. ret, _, _ := procSetBrushOrgEx.Call(
  298. uintptr(hdc),
  299. uintptr(nXOrg),
  300. uintptr(nYOrg),
  301. uintptr(unsafe.Pointer(lppt)))
  302. return ret != 0
  303. }
  304. func SetStretchBltMode(hdc HDC, iStretchMode int) int {
  305. ret, _, _ := procSetStretchBltMode.Call(
  306. uintptr(hdc),
  307. uintptr(iStretchMode))
  308. return int(ret)
  309. }
  310. func SetTextColor(hdc HDC, crColor COLORREF) COLORREF {
  311. ret, _, _ := procSetTextColor.Call(
  312. uintptr(hdc),
  313. uintptr(crColor))
  314. if ret == CLR_INVALID {
  315. panic("SetTextColor failed")
  316. }
  317. return COLORREF(ret)
  318. }
  319. func SetBkColor(hdc HDC, crColor COLORREF) COLORREF {
  320. ret, _, _ := procSetBkColor.Call(
  321. uintptr(hdc),
  322. uintptr(crColor))
  323. if ret == CLR_INVALID {
  324. panic("SetBkColor failed")
  325. }
  326. return COLORREF(ret)
  327. }
  328. func StartDoc(hdc HDC, lpdi *DOCINFO) int {
  329. ret, _, _ := procStartDoc.Call(
  330. uintptr(hdc),
  331. uintptr(unsafe.Pointer(lpdi)))
  332. return int(ret)
  333. }
  334. func StartPage(hdc HDC) int {
  335. ret, _, _ := procStartPage.Call(
  336. uintptr(hdc))
  337. return int(ret)
  338. }
  339. func StretchBlt(hdcDest HDC, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest int, hdcSrc HDC, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc int, dwRop uint) {
  340. ret, _, _ := procStretchBlt.Call(
  341. uintptr(hdcDest),
  342. uintptr(nXOriginDest),
  343. uintptr(nYOriginDest),
  344. uintptr(nWidthDest),
  345. uintptr(nHeightDest),
  346. uintptr(hdcSrc),
  347. uintptr(nXOriginSrc),
  348. uintptr(nYOriginSrc),
  349. uintptr(nWidthSrc),
  350. uintptr(nHeightSrc),
  351. uintptr(dwRop))
  352. if ret == 0 {
  353. panic("StretchBlt failed")
  354. }
  355. }
  356. func SetDIBitsToDevice(hdc HDC, xDest, yDest, dwWidth, dwHeight, xSrc, ySrc int, uStartScan, cScanLines uint, lpvBits []byte, lpbmi *BITMAPINFO, fuColorUse uint) int {
  357. ret, _, _ := procSetDIBitsToDevice.Call(
  358. uintptr(hdc),
  359. uintptr(xDest),
  360. uintptr(yDest),
  361. uintptr(dwWidth),
  362. uintptr(dwHeight),
  363. uintptr(xSrc),
  364. uintptr(ySrc),
  365. uintptr(uStartScan),
  366. uintptr(cScanLines),
  367. uintptr(unsafe.Pointer(&lpvBits[0])),
  368. uintptr(unsafe.Pointer(lpbmi)),
  369. uintptr(fuColorUse))
  370. return int(ret)
  371. }
  372. func ChoosePixelFormat(hdc HDC, pfd *PIXELFORMATDESCRIPTOR) int {
  373. ret, _, _ := procChoosePixelFormat.Call(
  374. uintptr(hdc),
  375. uintptr(unsafe.Pointer(pfd)),
  376. )
  377. return int(ret)
  378. }
  379. func DescribePixelFormat(hdc HDC, iPixelFormat int, nBytes uint, pfd *PIXELFORMATDESCRIPTOR) int {
  380. ret, _, _ := procDescribePixelFormat.Call(
  381. uintptr(hdc),
  382. uintptr(iPixelFormat),
  383. uintptr(nBytes),
  384. uintptr(unsafe.Pointer(pfd)),
  385. )
  386. return int(ret)
  387. }
  388. func GetEnhMetaFilePixelFormat(hemf HENHMETAFILE, cbBuffer uint32, pfd *PIXELFORMATDESCRIPTOR) uint {
  389. ret, _, _ := procGetEnhMetaFilePixelFormat.Call(
  390. uintptr(hemf),
  391. uintptr(cbBuffer),
  392. uintptr(unsafe.Pointer(pfd)),
  393. )
  394. return uint(ret)
  395. }
  396. func GetPixelFormat(hdc HDC) int {
  397. ret, _, _ := procGetPixelFormat.Call(
  398. uintptr(hdc),
  399. )
  400. return int(ret)
  401. }
  402. func SetPixelFormat(hdc HDC, iPixelFormat int, pfd *PIXELFORMATDESCRIPTOR) bool {
  403. ret, _, _ := procSetPixelFormat.Call(
  404. uintptr(hdc),
  405. uintptr(iPixelFormat),
  406. uintptr(unsafe.Pointer(pfd)),
  407. )
  408. return ret == TRUE
  409. }
  410. func SwapBuffers(hdc HDC) bool {
  411. ret, _, _ := procSwapBuffers.Call(uintptr(hdc))
  412. return ret == TRUE
  413. }