Thread: How to convert screen coordinates

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Angry How to convert screen coordinates

    I am sure I am making this more difficult than it should be but here is my problem.


    I am trying to figure out how to convert the dimension of my client area from pixels to inches. I've read Petzolds book on viewports and device coordinates but I've only succeeded in freeze drying my brain!

    For example lets say I want to draw a line from the left side of the client area over 2 inches like so (using MM_TWIPS map mode):
    Code:
    	MoveToEx (hdc,0,250,NULL);
    	LineTo (hdc, 2*1440, 250);
    Now, lets say I want to incorporate a zoom feature in my program that will resize this line to fill the width of my client area.
    I know the size of the client area (in pixels) from my WM_SIZE case:
    Code:
    	case WM_SIZE:
    		cxClient = LOWORD (lParam);
    		cyClient = HIWORD (lParam);
    and I know that my line is 2 inches wide so If I can figure out how to convert pixels into inches or vice versa I can easily come up with a ratio to do this.

    What I need is an explaination on how to do this type of conversion, because I have thoroughly confused myself!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Here is a discription of each mapping mode:

    MM_ANISOTROPIC Logical units are converted to arbitrary units with arbitrarily scaled axes. Setting the mapping mode to MM_ANISOTROPIC does not change the current window or viewport settings. To change the units, orientation, and scaling, call the SetWindowExt and SetViewportExt member functions.
    MM_HIENGLISH Each logical unit is converted to 0.001 inch. Positive x is to the right; positive y is up.
    MM_HIMETRIC Each logical unit is converted to 0.01 millimeter. Positive x is to the right; positive y is up.
    MM_ISOTROPIC Logical units are converted to arbitrary units with equally scaled axes; that is, 1 unit along the x-axis is equal to 1 unit along the y-axis. Use the SetWindowExt and SetViewportExt member functions to specify the desired units and the orientation of the axes. GDI makes adjustments as necessary to ensure that the x and y units remain the same size.
    MM_LOENGLISH Each logical unit is converted to 0.01 inch. Positive x is to the right; positive y is up.
    MM_LOMETRIC Each logical unit is converted to 0.1 millimeter. Positive x is to the right; positive y is up.
    MM_TEXT Each logical unit is converted to 1 device pixel. Positive x is to the right; positive y is down.
    MM_TWIPS Each logical unit is converted to 1/20 of a point. (Because a point is 1/72 inch, a twip is 1/1440 inch.) Positive x is to the right; positive y is up.

    Now if you do the math per inch in say MM_TWIPS then your size would be 1440 for one inch or 2880 for two inches and so forth. Of course if you use MM_LOENGLISH then it is 100 per inch and 200 for two inches and on and on. I hope this clarifies this for you.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Velius thank you for the reply.
    I think I understand what is going on now. Thanks again for taking to the time to explain something so remedial to me.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Not a problem. Happy Win32 API coding.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get coordinates on screen when user clicks?
    By Jake.c in forum Windows Programming
    Replies: 10
    Last Post: 01-18-2009, 10:54 PM
  2. Largest screen buffer size?
    By Ash1981 in forum C Programming
    Replies: 2
    Last Post: 01-30-2006, 04:31 AM
  3. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  4. Screen coordinates and mouse pointer manipulation
    By Zewu in forum Windows Programming
    Replies: 1
    Last Post: 03-25-2005, 05:30 PM
  5. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM