1

Can somebody please help me to translate the following to the C# equivalent. mapPicture is a picture object in GDI code however it is a panel in C#. I have done the code as follows,

Original MFC code,

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,

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);
nixgadget
  • 6,983
  • 16
  • 70
  • 103
  • Which part are you getting stuck on specifically? The way you've asked this, it looks like you're just asking for a code review. – Merlyn Morgan-Graham Oct 29 '11 at 23:18
  • Could you be a little more specific as to what you're having trouble with? is it not working, what's not showing up etc. – TJB Oct 29 '11 at 23:19
  • I haven't actually tested it yet but all I wanted to confirm was the SetMapMode and SetViewportOrg equivalent in C#. Not sure if I have done it correctly ! – nixgadget Oct 29 '11 at 23:32
  • it seems thats the y coordinate is point downwards rather than up. how can i reflect it ? – nixgadget Oct 30 '11 at 04:41
  • Inverse the value...Have you even tried to do this on your own? – Security Hound Oct 31 '11 at 13:28
  • yeah i accomplished it in a very ugly way. ie used matrix transforms to invert the y axis. the reason why i say its ugly is because of the reflection on text. so you have to reverse the process for text again. – nixgadget Oct 31 '11 at 23:22

1 Answers1

3

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.

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

C# Signature

[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;
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • i tried using this native code with what i had and i seem to be getting an error "InvalidOperationException". Obviously doing something wrong. Is there any documentation regarding DllImport of gdi ? – nixgadget Oct 31 '11 at 23:43