📄 g2_win32.c
字号:
return 0; }int g2_win32_FilledRectangle(int pid, void *pdp, int x1, int y1, int x2, int y2) { SelectObject(PDP->hMemDC,PDP->hBrush); SelectObject(PDP->hMemDC,PDP->hNullPen); return Rectangle(PDP->hMemDC,x1,y1,x2+1,y2+1); // add one since windows excludes lower right point SelectObject(PDP->hMemDC,PDP->hPen); return 0; }int g2_win32_Polygon(int pid, void *pdp, int N, int *points) { POINT *PointList; int i; PointList = (POINT *)malloc(N*sizeof(POINT)); if (PointList == NULL) { fprintf(stderr,"g2: not enough memory !\n"); return -1; } for (i=0;i<N;i++) { PointList[i].x = points[2*i]; PointList[i].y = points[2*i+1]; } SelectObject(PDP->hMemDC,GetStockObject(NULL_BRUSH)); Polygon(PDP->hMemDC,PointList,N); free(PointList); return 0; }int g2_win32_FilledPolygon(int pid, void *pdp, int N, int *points) { POINT *PointList; int i; PointList = (POINT *)malloc(N*sizeof(POINT)); if (PointList == NULL) { fprintf(stderr,"g2: not enough memory !\n"); return -1; } for (i=0;i<N;i++) { PointList[i].x = points[2*i]; PointList[i].y = points[2*i+1]; } SelectObject(PDP->hMemDC,PDP->hBrush); SelectObject(PDP->hMemDC,PDP->hNullPen); Polygon(PDP->hMemDC,PointList,N); SelectObject(PDP->hMemDC,PDP->hPen); free(PointList); return 0; }int g2_win32_Ellipse(int pid, void *pdp, int x, int y, int r1, int r2) { SelectObject(PDP->hMemDC,GetStockObject(NULL_BRUSH)); return Ellipse(PDP->hMemDC,x-r1,y-r2,x+r1+1,y+r2+1); // add one since windows is end exclusive }int g2_win32_FilledEllipse(int pid, void *pdp, int x, int y, int r1, int r2) { SelectObject(PDP->hMemDC,PDP->hBrush); SelectObject(PDP->hMemDC,PDP->hNullPen); return Ellipse(PDP->hMemDC,x-r1,y-r2,x+r1+1,y+r2+1); // add one since windows is end exclusive SelectObject(PDP->hMemDC,PDP->hPen); return 0; }int g2_win32_Arc(int pid, void *pdp, int x, int y, int r1, int r2, double a1, double a2) { a1 *= PI/180.; a2 *= PI/180.; SelectObject(PDP->hMemDC,GetStockObject(NULL_BRUSH)); if (PDP->type == g2_win32) return Arc(PDP->hMemDC,x-r1,y-r2,x+r1,y+r2,dtoi(x+r1*cos(a1)),dtoi(y-r2*sin(a1)),dtoi(x+r1*cos(a2)),dtoi(y-r2*sin(a2))); else return Arc(PDP->hMemDC,x-r1,y-r2,x+r1,y+r2,dtoi(x+r1*cos(a2)),dtoi(y-r2*sin(a2)),dtoi(x+r1*cos(a1)),dtoi(y-r2*sin(a1))); }int g2_win32_FilledArc(int pid, void *pdp, int x, int y, int r1, int r2, double a1, double a2) { a1 *= PI/180.; a2 *= PI/180.; SelectObject(PDP->hMemDC,PDP->hBrush); SelectObject(PDP->hMemDC,PDP->hNullPen); if (PDP->type == g2_win32) Pie(PDP->hMemDC,x-r1,y-r2,x+r1,y+r2,dtoi(x+r1*cos(a1)),dtoi(y-r2*sin(a1)),dtoi(x+r1*cos(a2)),dtoi(y-r2*sin(a2))); else Pie(PDP->hMemDC,x-r1,y-r2,x+r1,y+r2,dtoi(x+r1*cos(a2)),dtoi(y-r2*sin(a2)),dtoi(x+r1*cos(a1)),dtoi(y-r2*sin(a1))); SelectObject(PDP->hMemDC,PDP->hPen); return 0; }int g2_win32_DrawString(int pid, void *pdp, int x, int y, const char *text) { SetTextColor(PDP->hMemDC,PDP->PenColor); SetBkMode(PDP->hMemDC,TRANSPARENT); return TextOut(PDP->hMemDC,x,y,text,strlen(text)); }int g2_win32_QueryPointer(int pid, void *pdp, int *x, int *y, unsigned int *button)//// Thanks to input by Martin st閜hane// { POINT point; GetCursorPos(&point); ScreenToClient(PDP->hwndThreadWindow,&point); *y=point.y; *x=point.x; *button=0; if (PDP->hwndThreadWindow != GetForegroundWindow()) return; // return if our window does not have the focus if (GetKeyState(VK_LBUTTON)<0) *button=*button+256; if (GetKeyState(VK_MBUTTON)<0) *button=*button+512; if (GetKeyState(VK_RBUTTON)<0) *button=*button+1024; return 0; }void errhandler(LPSTR errtxt,HWND hwnd){ LPVOID lpMessageBuffer; char szError[255]; DWORD LastError; LastError = GetLastError(); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM, NULL,LastError,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language (LPTSTR) &lpMessageBuffer,0,NULL ); sprintf(szError,"Error in %s\n Error code %d : %s\n" ,errtxt,LastError,lpMessageBuffer); MessageBox(hwnd,szError,"error",MB_OK); LocalFree( lpMessageBuffer );}int InitApplication(){ WNDCLASS wc; // Fill in window class structure with parameters that describe the // main window. g2res_DLL = LoadLibrary("g2res.dll"); if (g2res_DLL == NULL) printf("Warning: Could not load g2 resource DLL\n Menu and Icon are disabled\n"); wc.style = CS_HREDRAW | CS_VREDRAW; // Class style(s). wc.lpfnWndProc = g2_WndProc; // Function to retrieve messages for windows of this class. wc.cbClsExtra = 0; // No per-class extra data. wc.cbWndExtra = 0; // No per-window extra data. wc.hInstance = 0; // Application that owns the class. wc.hIcon = NULL; if (g2res_DLL != NULL) { wc.hIcon = LoadIcon(g2res_DLL, MAKEINTRESOURCE(IDI_ICON1)); if (wc.hIcon == NULL) errhandler("Error in LoadIcon",NULL); } wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = NULL; wc.lpszMenuName = "G2WIN32"; wc.lpszClassName = "g2Window"; // Name used in call to CreateWindow. /* Register the window class and return success/failure code. */ if(!RegisterClass(&wc)) { errhandler("RegisterClass",NULL); return FALSE; } g2_win32_registered = TRUE; return TRUE;}/** * \ingroup physdev * \defgroup win32 MS Windows *//** * * Create a Windows device. * * \param width window width * \param height window height * \param title window title * \param type window type, see ::g2_win32_type * * \return physical device id * * \ingroup win32 */int g2_open_win32(int width, int height, const char *title, int type) { int pid=0,vid; long ThreadID; g2_win32_STRUCT *pdp; pdp = (g2_win32_STRUCT *)malloc(sizeof(g2_win32_STRUCT)); PDP->type = type; PDP->NoOfInks = 0; PDP->Inks = NULL; PDP->PenStyle = 0; PDP->PenWidth = 1; PDP->PenColor = RGB(1,1,1); PDP->PenDash = NULL; PDP->nWidth = width; PDP->nHeight = height; PDP->messageloop = 0; PDP->hFont = NULL; switch(type) { case g2_win32: { if(g2_win32_registered == FALSE) InitApplication(); PDP->x = 1; PDP->y = 1; if (title != NULL) PDP->title = title; else PDP->title = "g2 Window"; PDP->hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)g2_StartThread, (LPVOID)(pdp), 0, (LPDWORD) &ThreadID ); if (PDP->hThread == NULL) fprintf(stderr,"g2_win32: Thread could not be started\n"); SetThreadPriority(PDP->hThread,THREAD_PRIORITY_ABOVE_NORMAL); //Wait till window is created by Thread while( PDP->messageloop == 0) Sleep(10); break; } case g2_wmf32 : { DWORD dwInchesX; DWORD dwInchesY; DWORD dwDPI = 72; RECT Rect = { 0, 0, 0, 0 }; TCHAR szDesc[] = "Created by g2\0\0"; HDC hScreenDC; float PixelsX, PixelsY, MMX, MMY; dwInchesX = PDP->nWidth/72; dwInchesY = PDP->nHeight/72; // dwInchesX x dwInchesY in .01mm units SetRect( &Rect, 0, 0,dwInchesX*2540, dwInchesY*2540 ); // Get a Reference DC hScreenDC = GetDC( NULL ); // Get the physical characteristics of the reference DC PixelsX = (float)GetDeviceCaps( hScreenDC, HORZRES ); PixelsY = (float)GetDeviceCaps( hScreenDC, VERTRES ); MMX = (float)GetDeviceCaps( hScreenDC, HORZSIZE ); MMY = (float)GetDeviceCaps( hScreenDC, VERTSIZE ); // Create the Metafile PDP->hMemDC = CreateEnhMetaFile(hScreenDC, title, &Rect, szDesc); //tstDC = CreateEnhMetaFile(hScreenDC, "test.emf", &Rect, szDesc); // Release the reference DC ReleaseDC( NULL, hScreenDC ); // Anisotropic mapping mode SetMapMode( PDP->hMemDC, MM_ANISOTROPIC ); // Set the Windows extent SetWindowExtEx( PDP->hMemDC, dwInchesX*dwDPI, dwInchesY*dwDPI, NULL ); // Set the viewport extent to reflect // dwInchesX" x dwInchesY" in device units SetViewportExtEx( PDP->hMemDC, (int)((float)dwInchesX*25.4f*PixelsX/MMX), (int)((float)dwInchesY*25.4f*PixelsY/MMY), NULL );// printf("viewport: %d %d\n",(int)((float)dwInchesX*25.4f*PixelsX/MMX),// (int)((float)dwInchesY*25.4f*PixelsY/MMY)); // create the device context// PDP->hMemDC = CreateMetaFile(NULL) ; // PDP->hMemDC = CreateEnhMetaFile( (HDC)NULL, title, &WmfRect, "Created by g2"); // PDP->hMemDC = CreateMetaFile(title);// SetMapMode(PDP->hMemDC,MM_LOMETRIC );// SetWindowExtEx(PDP->hMemDC,width,height,NULL);// SetViewportExtEx(PDP->hMemDC,width*1000,height*1000,NULL); if (PDP->hMemDC == NULL) errhandler("Could not Create Metafile !\n",NULL); SetArcDirection(PDP->hMemDC,AD_CLOCKWISE); break; } default: return height; } SetTextAlign(PDP->hMemDC,TA_BOTTOM | TA_LEFT); vid = g2_register_physical_device(pid, pdp, g2_IntCoor, g2_win32_funix, 1.0, -1.0, 0.0, height-1); g2_allocate_basic_colors(vid); g2_pen(vid,1); g2_set_background(vid, 0); g2_set_font_size(vid, 10); g2_clear(vid); return vid; }#undef PDP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -