golang 调用GdiPlus 打印
时间: 2023-08-03 18:06:53 浏览: 191
可以使用 Go 国际化方式(CGo)调用 GdiPlus 库来打印。以下是一个简单的示例:
```go
package main
/*
#include <windows.h>
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")
int print(const wchar_t *filename) {
Gdiplus::GdiplusStartupInput input;
ULONG_PTR token;
Gdiplus::GdiplusStartup(&token, &input, NULL);
Gdiplus::Image* image = new Gdiplus::Image(filename);
HDC hdc = GetDC(NULL);
HDC hdcPrn = CreateDC(L"WINSPOOL", NULL, NULL, NULL);
RECT prnRect;
prnRect.left = prnRect.top = 0;
prnRect.right = GetDeviceCaps(hdcPrn, PHYSICALWIDTH);
prnRect.bottom = GetDeviceCaps(hdcPrn, PHYSICALHEIGHT);
HDC hdcMem = CreateCompatibleDC(hdcPrn);
HBITMAP hBmp = CreateCompatibleBitmap(hdcPrn, prnRect.right, prnRect.bottom);
SelectObject(hdcMem, hBmp);
Gdiplus::Graphics graphics(hdcMem);
graphics.DrawImage(image, 0, 0, prnRect.right, prnRect.bottom);
DOCINFO di;
ZeroMemory(&di, sizeof(DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = L"Test Print";
StartDoc(hdcPrn, &di);
StartPage(hdcPrn);
BitBlt(hdcPrn, 0, 0, prnRect.right, prnRect.bottom, hdcMem, 0, 0, SRCCOPY);
EndPage(hdcPrn);
EndDoc(hdcPrn);
DeleteDC(hdcPrn);
DeleteObject(hBmp);
DeleteDC(hdcMem);
ReleaseDC(NULL, hdc);
delete image;
Gdiplus::GdiplusShutdown(token);
return 0;
}
*/
import "C"
import (
"unsafe"
)
func main() {
filename := "test.png"
cname := C.CString(filename)
defer C.free(unsafe.Pointer(cname))
C.print(cname)
}
```
这个示例将加载一个 PNG 图像文件并将其打印到默认打印机上。它使用 GdiPlus 库来绘制图像和打印输出。在 Go 中使用 CGo 调用 C 函数,可以轻松地与 Windows API 进行交互并使用 GdiPlus 库进行绘图和打印操作。