Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem Translating Window Coordinates

Problem Translating Window Coordinates

Scheduled Pinned Locked Moved C#
questionhelpgraphicsdata-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • . Offline
    . Offline
    ...---...
    wrote on last edited by
    #1

    Hi, I have asked this question about a day ago and I was given a very good answer about some concepts I was not aware of-- Region.Translate, and Matrix.Transform. So I've been experimenting with them and I'm able to now draw Rects and Translate and Rotate them but I still haven't solved what I ultimately want to do... ///////////////////// PointF pt_1 = new PointF(0,0) PointF pt_1 = new PointF(100,100) public void DrawLine(Graphics g) { SolidBrush brush = new SolidBrush(Color.SteelBlue); Pen pen = new Pen(brush, 10); g.DrawLine(pen, pt_1.X, pt_1.Y, pt_2.X, pt_2.Y); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { DrawLine(e.Graphics); } //////////////////////////////////////////// How do I change to my own coordinate system and have it permanently stay that way? That is, I want (0,0) to always be the center of the Form, no matter what event fires. What I have (not the test code above) is a program that can draw vectors on an XY axis. I can do it-- only because I've hard-coded the coordinates for the Graph Origin, which besides being a lousy way to do things, also prevents me from any more progress.... Any (further) help would be greatly appreciated.. thanks

    C 1 Reply Last reply
    0
    • . ...---...

      Hi, I have asked this question about a day ago and I was given a very good answer about some concepts I was not aware of-- Region.Translate, and Matrix.Transform. So I've been experimenting with them and I'm able to now draw Rects and Translate and Rotate them but I still haven't solved what I ultimately want to do... ///////////////////// PointF pt_1 = new PointF(0,0) PointF pt_1 = new PointF(100,100) public void DrawLine(Graphics g) { SolidBrush brush = new SolidBrush(Color.SteelBlue); Pen pen = new Pen(brush, 10); g.DrawLine(pen, pt_1.X, pt_1.Y, pt_2.X, pt_2.Y); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { DrawLine(e.Graphics); } //////////////////////////////////////////// How do I change to my own coordinate system and have it permanently stay that way? That is, I want (0,0) to always be the center of the Form, no matter what event fires. What I have (not the test code above) is a program that can draw vectors on an XY axis. I can do it-- only because I've hard-coded the coordinates for the Graph Origin, which besides being a lousy way to do things, also prevents me from any more progress.... Any (further) help would be greatly appreciated.. thanks

      C Offline
      C Offline
      Curtis Schlak
      wrote on last edited by
      #2

      Since reading your post two days ago, I've tried implementing this in as many ways as I could think. Unfortunately, I just can't get it to stick. I think that you will have to translate the coordinates for every message/event that you receive. Admittedly, you don't have too much work ahead of you. For messages/events that require painting, you only need to do the Graphics.TranslateTransform( Width / 2, Height / 2 ) call. For messages/events that have client coordinates associated with them, you'll need to subtract Width / 2 from the x and Height / 2 from the y. Finally, with messages/events that have screen coordintates, you need to call PointToClient(Point) and then subtract like the last call. I would write three static methods in a helper class with signatures like:

      Point CenteredPointFromClient( Point point, Form form );
      int CenteredPointFromScreen( Point point, Form form );
      void CenteredGraphics( Graphics g, Form form );

      I wish a better solution existed. I don't think that it does because it would mess up the way that the .NET runtime draws child controls. Sorry. "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups