[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

Making the coordinates of a DC match up with the coordinates of a CWnd in a CPropertyPage

Hi there,

Here's my problem: I have a subclassed CListCtrl (LessonListCtrl) inside a CPropertyPage.  Part of the functionality of the LessonListCtrl is to draw little ticks or crosses to the left of the LessonListCtrl beside every item in it. (It's in report view).  However, I'm having trouble making the ticks and crosses lined up with the items.  I tried this:

* returns a CRect* giving the coordinates that the tick or cross will appear in */
CRect* LessonListCtrl::getBoxRect(int selMark) {
      CRect itemRect;
      GetItemRect(selMark, &itemRect, LVIR_BOUNDS);
      CRect windowRect;
      GetWindowRect(&windowRect);
      int top = itemRect.top + windowRect.top;
      int bottom = itemRect.bottom + windowRect.top;
      int right = itemRect.left + windowRect.left - parentRect.left - 5;
      int left = right - (itemRect.bottom - itemRect.top); //square
      CRect* boxRect = new CRect(left, top, right, bottom);
      return boxRect;
}

The DC is created in a method of the LessonListCtrl as CClientDC dc(GetParent());

Unfortunately it seems the coordinates of the CRect from getBoxRect() are relative to the screen, or the top-level window of the project, including title bar, menu bar etc while the coordinates of the DC are relative to only the rectangle inside the property page.  This means that the ticks and crosses appear a couple of rows too low down.

I can kinda solve this problem by using CClientDC dc(GetParent()->GetParent());
or CClientDC dc(NULL) which takes the DC up to the whoe screen, but I then have nasty issues with the ticks and crosses redrawing even when my project is not on top (and since immediately after the drawing the program starts up Explorer, this is a problem).

I think what I want to do is work out how high the stuff above the CPropertyPage is and then subtract this from my top amd bottom.  But I can't work out how to do that.  If someone could tell me how to do that, or some other solution to my problem, I would be very grateful.

Thank you.
0
wilsonian
Asked:
wilsonian
  • 2
1 Solution
 
DexstarCommented:
wilsonian:

> Unfortunately it seems the coordinates of the CRect from getBoxRect() are
> relative to the screen, or the top-level window of the project, including
> title bar, menu bar etc while the coordinates of the DC are relative to only
> the rectangle inside the property page.  This means that the ticks and crosses
> appear a couple of rows too low down.

You need to use CWnd's ClientToScreen and ScreenToClient functions.  They will do the adjustments for you.

ClientToScreen Info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cwnd.3a3a.clienttoscreen.asp

ScreenToClient Info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cwnd.3a3a.screentoclient.asp


Hope That Helps,
Dex*
0
 
wilsonianAuthor Commented:
Well that's sure the quickest I've ever had a question answered.

Thank you very much.
0
 
DexstarCommented:
No problem!  :)

Dex*
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now