# Getting DC for a TxtCtrl

**URL:** <https://www.ruby-forum.com/t/getting-dc-for-a-txtctrl/159725>\
**Category:** wxRuby\
**Created:** [February 7, 2009, 6:06pm UTC](https://www.ruby-forum.com/t/getting-dc-for-a-txtctrl/159725 "2009-02-07T18:06:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rossgoodell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rossgoodell](https://www.ruby-forum.com/u/rossgoodell)\
**Post date:** [February 7, 2009, 6:06pm UTC](https://www.ruby-forum.com/t/getting-dc-for-a-txtctrl/159725/1 "2009-02-07T18:06:25Z")

</div>

I need to find where the mouse has been clicked in a TextCtrl. To do  
this I’ve been using “@lastPixelPt = evt.get\_position” in my  
evt\_left\_down method but have discovered that this gives me a physical  
position in the visible window. It looks like I could probably use  
get\_logical\_position(dc) to get a position relative to the text in the  
control, that is adjusted for scrolling, but I haven’t found how to set  
up dc (device context). I would greatly appreciate an example of how to  
set up dc so that “@lastPixelPt = evt.get\_logical\_position(dc)” will  
work or any advice I can get about getting a position relative to the  
text in the control.

Thank you,  
Ross Goodell

---

<div class="post-metadata">

**Author:** ![rossgoodell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rossgoodell](https://www.ruby-forum.com/u/rossgoodell)\
**Post date:** [February 7, 2009, 9:05pm UTC](https://www.ruby-forum.com/t/getting-dc-for-a-txtctrl/159725/2 "2009-02-07T21:05:51Z")

</div>

Ross Goodell wrote:

> I need to find where the mouse has been clicked in a TextCtrl.

Do you want the character position, or the unscrolled pixel position? If  
it’s just the character position, use TextCtrl#hit\_test plus possibly  
#xy\_to\_position.

For some reason, wxWidgets doesn’t provide TextCtrl#hit\_test on Mac, but  
it’s not hard to do your own implementation. I have one kicking around  
somewhere if that’s what you need.

> To do  
> this I’ve been using “@lastPixelPt = evt.get\_position” in my  
> evt\_left\_down method but have discovered that this gives me a physical  
> position in the visible window. It looks like I could probably use  
> get\_logical\_position(dc) to get a position relative to the text in the  
> control, that is adjusted for scrolling,

If you want the pixel position taking into account scrolling, then  
TextCtrl may not work for you. There isn’t a way to get the scroll  
position in pixels - get\_scroll\_pos doesn’t work for TextCtrl in  
wxWidgets. As I understand it, this is down to limitations of the  
underlying OSes - the native controls don’t all support getting the  
scroll position in pixels.

You might try using RichTextCtrl, which has a get\_view\_start method to  
return the x, y scrolling offset.

alex

---

<div class="post-metadata">

**Author:** ![rossgoodell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rossgoodell](https://www.ruby-forum.com/u/rossgoodell)\
**Post date:** [February 8, 2009, 3:57am UTC](https://www.ruby-forum.com/t/getting-dc-for-a-txtctrl/159725/3 "2009-02-08T03:57:50Z")

</div>

Alex F. wrote:

> Do you want the character position, or the unscrolled pixel position? If  
> it’s just the character position, use TextCtrl#hit\_test plus possibly  
> #xy\_to\_position.

Yes, I think the character position is all that I really need, and  
hit\_test does the job for me

> For some reason, wxWidgets doesn’t provide TextCtrl#hit\_test on Mac, but  
> it’s not hard to do your own implementation. I have one kicking around  
> somewhere if that’s what you need.

Thanks. That’s good to know. For now I’m concentrating on getting my  
program working on MS Windows.

> If you want the pixel position taking into account scrolling, then  
> TextCtrl may not work for you. There isn’t a way to get the scroll  
> position in pixels - get\_scroll\_pos doesn’t work for TextCtrl in  
> wxWidgets. As I understand it, this is down to limitations of the  
> underlying OSes - the native controls don’t all support getting the  
> scroll position in pixels.
> 
> You might try using RichTextCtrl, which has a get\_view\_start method to  
> return the x, y scrolling offset.
> 
> alex

You’ve gotten me out of a jam again. Thank you very much. And thanks  
for the tip on RichTextCtrl, which I might want to use in a later  
version.

Ross
