简体   繁体   English

C#相当于C ++ GDI代码

[英]C# equivalent of the C++ GDI code

Can somebody please help me to translate the following to the C# equivalent. 有人可以帮我翻译下面的C#等价物。 mapPicture is a picture object in GDI code however it is a panel in C#. mapPicture是GDI代码中的图片对象,但它是C#中的面板。 I have done the code as follows, 我已经完成了如下代码,

Original MFC code, 原始MFC代码,

CDC *dc = mapPicture.GetDC();
CRect maprect;
mapPicture.GetClientRect(&maprect); 
CPen pen,*oldpen;
dc->Rectangle(&rect);
pen.CreatePen(PS_SOLID,1,RGB(0,0,0));
oldpen=dc->SelectObject(&pen);
dc->SetMapMode(MM_LOMETRIC);
CPoint b1=dc->SetViewportOrg(25,rect.Height()-25);
dc->SetTextColor(RGB(0,0,0));
dc->MoveTo(0,0);
dc->LineTo((2*rect.right - 60),0);
dc->MoveTo(0,0);
dc->LineTo(0,2*rect.bottom);
dc->MoveTo((2*rect.right - 60)-15,8);
dc->LineTo((2*rect.right - 60),0);
dc->LineTo((2*rect.right - 60)-15,-8);
dc->SetTextAlign(TA_RIGHT|TA_BOTTOM);
dc->TextOut((2*rect.right - 55),-55,"X");

C# code I have done so far, 到目前为止,我已经完成了C#代码

myPen = new Pen(Color.Black, 1);
formGraphics = mapPicture.CreateGraphics();
Rectangle rect = mapPicture.ClientRectangle;
formGraphics.PageUnit = GraphicsUnit.Millimeter;
formGraphics.TranslateTransform(25, rect.Height - 25);
formGraphics.DrawLine(myPen, 0, 0, (2 * rect.Right - 60), 0);
formGraphics.DrawLine(myPen, 0, 0, 0, 2 * rect.Bottom);
formGraphics.DrawLine(myPen, (2 * rect.Right - 60) - 15, 8, (2 * rect.Right - 60), 0);
formGraphics.DrawLine(myPen, (2 * rect.Right - 60), 0, (2 * rect.Right - 60) - 15, -8);
SolidBrush drawBrush = new SolidBrush(Color.Black);
Font drawFont = new Font("Microsoft Sans Serif", 9);
formGraphics.DrawString("X", drawFont, drawBrush, (2 * rect.Right - 55), -55);

Without setting the Map mode, you might well encounter issues with co-ordinates. 如果不设置地图模式,则坐标很可能会遇到问题。 It was in the original code for a reason. 它在原始代码中是有原因的。 I don't believe there is a direct C# equivalent so you'll need to look at using PInvoke to call the Win32 SetMapMode function directly. 我不相信有直接的C#等效项,因此您需要研究使用PInvoke直接调用Win32 SetMapMode函数。

(copied from http://www.pinvoke.net/default.aspx/gdi32/SetMapMode.html ) (从http://www.pinvoke.net/default.aspx/gdi32/SetMapMode.html复制)

C# Signature C#签名

[DllImport("gdi32.dll")]
static extern int SetMapMode(IntPtr hdc, int fnMapMode);

Constants 常量

//Mapping Modes
static int MM_TEXT = 1;
static int MM_LOMETRIC = 2;
static int MM_HIMETRIC = 3;
static int MM_LOENGLISH = 4;
static int MM_HIENGLISH = 5;
static int MM_TWIPS = 6;
static int MM_ISOTROPIC = 7;
static int MM_ANISOTROPIC = 8;

//Minimum and Maximum Mapping Mode values
static int MM_MIN = MM_TEXT;
static int MM_MAX = MM_ANISOTROPIC;
static int MM_MAX_FIXEDSCALE = MM_TWIPS;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM