Click here to Skip to main content
15,922,427 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralThanks to John and Florin. I am reading up the docs. Do you have any more suggestions on Pin
DengJW16-Mar-03 15:41
DengJW16-Mar-03 15:41 
AnswerThanks to John and Florin. I learnt a lots from your posts and got my thing done. Pin
DengJW16-Mar-03 23:16
DengJW16-Mar-03 23:16 
GeneralCoordinate Conversions Pin
georgiek5016-Mar-03 2:56
georgiek5016-Mar-03 2:56 
GeneralRe: Coordinate Conversions Pin
Brian Shifrin16-Mar-03 3:04
Brian Shifrin16-Mar-03 3:04 
GeneralRe: Coordinate Conversions Pin
georgiek5016-Mar-03 3:07
georgiek5016-Mar-03 3:07 
GeneralRe: Coordinate Conversions Pin
73Zeppelin16-Mar-03 3:13
73Zeppelin16-Mar-03 3:13 
GeneralRe: Coordinate Conversions Pin
georgiek5016-Mar-03 6:28
georgiek5016-Mar-03 6:28 
GeneralRe: Coordinate Conversions Pin
73Zeppelin16-Mar-03 7:03
73Zeppelin16-Mar-03 7:03 
When you do any type of drawing, for example LineTo(), MoveTo(), etc... that involves the device context (DC) - these functions assume you are passing logical coordinates. These functions are members of the device context (DC) which has its own set of coordinates (the logical coordinates). Your mapping mode (a property of the DC) determines the units of measurement when you draw something.
The coordinate data received from the mouse messages is, however, not in logical coordinate form. Thus, points passed to OnLButtonDown(), OnMouseMove(), etc... are in DEVICE UNITS, that is pixels. These are measured relative to the top left corner of the client area. These are called client coordinates. When you call InvalidateRect() the rectangle is assumed to be defined in terms of client coordinates.
If your mapping mode is MM_TEXT, then client coordinates and logical coordinates in the device context are both in units of pixels and so they are the SAME, as long as you don't scroll the window.
Thus to properly convert between coordinates there are two things you need to do:

1. Convert client coordinates that you get from the mouse to logical coordinates

2. Convert any bounding rectangles (regions, etc...) back to client coordinates (if, for example you want to call InvalidateRect())

Thus you could do something like this in OnLButtonDown() or OnMouseMove() handler:

<br />
void CMyView::OnLButtonDown()<br />
{<br />
     CClientDC aDC(this);    // Create a DC<br />
     OnPrepareDC(&aDC);      // Get origin adjusted<br />
     aDC.DPtoLP(&point);     // convert point to logical<br />
<br />
     // your code here<br />
}<br />


In the above, you obtain a DC for the current view by creating a CClientDC object and passing 'this' to the constructor. The advantage of CClientDC is that windows automatically releases the DC when the object goes out of scope. This is necessary because there are a liminted number of DC's in windows.

For a geometric shape (Rgn, Rect) I suppose you could try the following:

<br />
CClientDC aDC(this);<br />
OnPrepareDC(&aDC);<br />
<br />
CRect aRect = /* Code to construct rect goes here */;<br />
aDC.LPtoDP(aRect);<br />
InvalidateRect(aRect);<br />


OR

<br />
CRgn aRgn = /* Code to create region */;<br />
aDC.LPtoDP(aRgn);<br />
<br />
// Your code here....<br />
if ( PtInRegion(hRgn, pt.x, pt.y) != 0 )<br />
   ...<br />
else<br />
   ...<br />


Thus, the coordinates should be properly converted...

Hope this helps.
GeneralRe: Coordinate Conversions Pin
georgiek5016-Mar-03 7:41
georgiek5016-Mar-03 7:41 
GeneralRe: Coordinate Conversions Pin
georgiek5016-Mar-03 14:38
georgiek5016-Mar-03 14:38 
GeneralRe: Coordinate Conversions Pin
73Zeppelin16-Mar-03 15:51
73Zeppelin16-Mar-03 15:51 
GeneralNeed a windows registry expert's help Pin
Abin16-Mar-03 2:18
Abin16-Mar-03 2:18 
QuestionHow to calculate the running time of each function? Pin
George216-Mar-03 1:24
George216-Mar-03 1:24 
GeneralMessage routing Pin
JamesLaing16-Mar-03 1:01
JamesLaing16-Mar-03 1:01 
GeneralRe: Message routing Pin
Brian Shifrin16-Mar-03 2:33
Brian Shifrin16-Mar-03 2:33 
GeneralRe: Message routing Pin
JamesLaing16-Mar-03 2:38
JamesLaing16-Mar-03 2:38 
GeneralChanging Ownership of child CView Pin
ganon15-Mar-03 17:23
ganon15-Mar-03 17:23 
GeneralRe: Changing Ownership of child CView Pin
Brian Shifrin16-Mar-03 2:41
Brian Shifrin16-Mar-03 2:41 
GeneralTimer Pin
progman15-Mar-03 13:20
progman15-Mar-03 13:20 
GeneralRe: Timer Pin
Nish Nishant15-Mar-03 15:29
sitebuilderNish Nishant15-Mar-03 15:29 
GeneralRe: Timer Pin
progman16-Mar-03 12:17
progman16-Mar-03 12:17 
GeneralRe: Timer Pin
Jambolo16-Mar-03 14:20
Jambolo16-Mar-03 14:20 
GeneralInternet connection Pin
progman15-Mar-03 13:05
progman15-Mar-03 13:05 
GeneralRe: Internet connection Pin
Nish Nishant15-Mar-03 15:32
sitebuilderNish Nishant15-Mar-03 15:32 
GeneralRe: Internet connection Pin
d_kilshtein16-Mar-03 5:51
d_kilshtein16-Mar-03 5:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.