有人可以帮我将以下内容翻译成 C# 等价物吗? mapPicture 是 GDI 代码中的图片对象,但它是 C# 中的面板。 我做了如下代码,
原始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# 代码,
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);
最佳答案
如果不设置 map 模式,您很可能会遇到坐标问题。它在原始代码中是有原因的。我不相信有直接的 C# 等效项,因此您需要考虑使用 PInvoke 直接调用 Win32 SetMapMode 函数。
(从 http://www.pinvoke.net/default.aspx/gdi32/SetMapMode.html 复制)
C# 签名
[DllImport("gdi32.dll")]
static extern int SetMapMode(IntPtr hdc, int fnMapMode);
常量
//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;
关于C# 等效于 C++ GDI 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7942287/