364,112 Members | 2322 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

how to change cursor shape in RichTextBox

S.W. Rasmussen
P: n/a
S.W. Rasmussen
A trivial (?) question: does anyone know how to change the shape of the
cursor in a RichTextBox control from the normal vertical line to an
underscore?


Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


Rick Rothstein
P: n/a
Rick Rothstein
> A trivial (?) question: does anyone know how to change the shape of the[color=blue]
> cursor in a RichTextBox control from the normal vertical line to an
> underscore?[/color]

I'm kind of thinking you can't do that, but I don't know for sure. However,
if you could do it, where would

RichTextBox1.SelStart = 0

send the text cursor to? Before you answer that, think about where the text
cursor would go with this

RichTextBox1.SelStart = 1

Rick - MVP


Jul 17 '05 #2

J French
P: n/a
J French
On Wed, 7 Jan 2004 08:47:36 -0000, "S.W. Rasmussen" <swr@seqtools.dk>
wrote:
[color=blue]
>A trivial (?) question: does anyone know how to change the shape of the
>cursor in a RichTextBox control from the normal vertical line to an
>underscore?[/color]

That is not really called the cursor
- it is called a 'Caret'

I can't see any API method of tinkering with a Caret

You could simply hide it and tinker with the Font style of the
character where it would appear

You would then need a timer to get the blinking effect
Jul 17 '05 #3

S.W. Rasmussen
P: n/a
S.W. Rasmussen
Well, I suspected that changing the shape of the Caret - as I understand is
the correct term - is not easy (having looked quite a number of places). I
will have a look at the "underscore" idea - although this may slow down
navigation and of course require special handling of the selstart = 0
situation.

Thanks anyway for the replies...

Cheers

Soeren

--
D.Sci. Soeren W. Rasmussen
Valby DataCenter
Valbygaardsvej 18
DK-2500 Valby
Denmark
swr@seqtools.dk
www.seqtools.dk


"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:meedndK_pJF1S2aiRVn-jw@comcast.com...[color=blue][color=green]
> > A trivial (?) question: does anyone know how to change the shape of the
> > cursor in a RichTextBox control from the normal vertical line to an
> > underscore?[/color]
>
> I'm kind of thinking you can't do that, but I don't know for sure.[/color]
However,[color=blue]
> if you could do it, where would
>
> RichTextBox1.SelStart = 0
>
> send the text cursor to? Before you answer that, think about where the[/color]
text[color=blue]
> cursor would go with this
>
> RichTextBox1.SelStart = 1
>
> Rick - MVP
>
>[/color]


Jul 17 '05 #4

mayayana
P: n/a
mayayana
I did it once because I wanted to be able to switch to
a red caret, but I don't remember how I did it. (Not much help,
I know!) I think it might have been with CreateCursor.
As I recall, the process wasn't difficult but after replacing
an RTB cursor with a 1-pixel-wide red line bitmap I found that
1 pixel was just too big and clunky-looking for my purposes -
and didn't pursue it any further.

--
--
S.W. Rasmussen <swr@seqtools.dk> wrote in message
news:btgpfb$v8u$1@news.cybercity.dk...[color=blue]
> Well, I suspected that changing the shape of the Caret - as I understand[/color]
is[color=blue]
> the correct term - is not easy (having looked quite a number of places). I
> will have a look at the "underscore" idea - although this may slow down
> navigation and of course require special handling of the selstart = 0
> situation.
>
> Thanks anyway for the replies...
>
> Cheers
>
> Soeren
>
> --
> D.Sci. Soeren W. Rasmussen
> Valby DataCenter
> Valbygaardsvej 18
> DK-2500 Valby
> Denmark
> swr@seqtools.dk
> www.seqtools.dk
>
>
> "Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
> news:meedndK_pJF1S2aiRVn-jw@comcast.com...[color=green][color=darkred]
> > > A trivial (?) question: does anyone know how to change the shape of[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > cursor in a RichTextBox control from the normal vertical line to an
> > > underscore?[/color]
> >
> > I'm kind of thinking you can't do that, but I don't know for sure.[/color]
> However,[color=green]
> > if you could do it, where would
> >
> > RichTextBox1.SelStart = 0
> >
> > send the text cursor to? Before you answer that, think about where the[/color]
> text[color=green]
> > cursor would go with this
> >
> > RichTextBox1.SelStart = 1
> >
> > Rick - MVP
> >
> >[/color]
>
>[/color]


Jul 17 '05 #5

S.W. Rasmussen
P: n/a
S.W. Rasmussen
you were almost right:

CreateCaret Text1.hwnd, Picture1.Picture, 8, 13
ShowCaret Text1.hwnd

do the trick

Soeren

"mayayana" <mayaXXyaYYna1a@mindZZspring.com> wrote in message
news:TSVKb.31165$IM3.23113@newsread3.news.atl.eart hlink.net...[color=blue]
> I did it once because I wanted to be able to switch to
> a red caret, but I don't remember how I did it. (Not much help,
> I know!) I think it might have been with CreateCursor.
> As I recall, the process wasn't difficult but after replacing
> an RTB cursor with a 1-pixel-wide red line bitmap I found that
> 1 pixel was just too big and clunky-looking for my purposes -
> and didn't pursue it any further.
>
> --
> --
> S.W. Rasmussen <swr@seqtools.dk> wrote in message
> news:btgpfb$v8u$1@news.cybercity.dk...[color=green]
> > Well, I suspected that changing the shape of the Caret - as I understand[/color]
> is[color=green]
> > the correct term - is not easy (having looked quite a number of places).[/color][/color]
I[color=blue][color=green]
> > will have a look at the "underscore" idea - although this may slow down
> > navigation and of course require special handling of the selstart = 0
> > situation.
> >
> > Thanks anyway for the replies...
> >
> > Cheers
> >
> > Soeren
> >
> > --
> > D.Sci. Soeren W. Rasmussen
> > Valby DataCenter
> > Valbygaardsvej 18
> > DK-2500 Valby
> > Denmark
> > swr@seqtools.dk
> > www.seqtools.dk
> >
> >
> > "Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
> > news:meedndK_pJF1S2aiRVn-jw@comcast.com...[color=darkred]
> > > > A trivial (?) question: does anyone know how to change the shape of[/color][/color]
> the[color=green][color=darkred]
> > > > cursor in a RichTextBox control from the normal vertical line to an
> > > > underscore?
> > >
> > > I'm kind of thinking you can't do that, but I don't know for sure.[/color]
> > However,[color=darkred]
> > > if you could do it, where would
> > >
> > > RichTextBox1.SelStart = 0
> > >
> > > send the text cursor to? Before you answer that, think about where the[/color]
> > text[color=darkred]
> > > cursor would go with this
> > >
> > > RichTextBox1.SelStart = 1
> > >
> > > Rick - MVP
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Jul 17 '05 #6

J French
P: n/a
J French
On Thu, 8 Jan 2004 18:36:12 -0000, "S.W. Rasmussen" <swr@seqtools.dk>
wrote:
[color=blue]
>you were almost right:
>
>CreateCaret Text1.hwnd, Picture1.Picture, 8, 13
>ShowCaret Text1.hwnd
>
>do the trick
>[/color]

That is a new one to me - thanks
Jul 17 '05 #7

Rick Rothstein
P: n/a
Rick Rothstein
> >you were almost right:[color=blue][color=green]
> >
> >CreateCaret Text1.hwnd, Picture1.Picture, 8, 13
> >ShowCaret Text1.hwnd
> >
> >do the trick[/color]
>
> That is a new one to me - thanks[/color]

Although I *now* remember having seen those API functions, I have never
played with them before. After taking a quick look, I noted this would only
be good to use for a fixed-width font and the caret size won't change on a
per character basis. Also, instead of fixing the width and height with
"magic" numbers (which, of course, means you would have to come up with new
numbers for each font size you used), this will work for all font sizes
(again, assuming a fixed-width font)...

Private Declare Function CreateCaret Lib "user32" _
(ByVal hwnd As Long, _
ByVal hBitmap As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long) As Long

Private Declare Function ShowCaret Lib "user32" _
(ByVal hwnd As Long) As Long

Private Sub Text1_GotFocus()
With Me
Set .Font = Text1.Font
CreateCaret Text1.hwnd, 0, _
.TextWidth("X") \ Screen.TwipsPerPixelX, _
.TextHeight("X") \ Screen.TwipsPerPixelY
End With
ShowCaret Text1.hwnd
End Sub

A couple of additional notes. First, I did away with the PictureBox which I
assume was being used to provide the basis for the "bitmap" that would
become the caret; passing a 0 into that parameter produces the black-block
caret also. Next, in order to generalize the sizes, I needed a way of
getting to the width and height of the text for the font being used in the
TextBox. I did this by setting the form's Font property to that of the
TextBox and pulling the values from the form's TextWidth and TextHeight
properties (the PictureBox could have been used for this purpose had I
retained it). If you have more than one TextBox on your form, then simply
duplicate the above for their GotFocus events like this

Private Sub Text2_GotFocus()
With Me
Set .Font = Text2.Font
CreateCaret Text2.hwnd, 0, _
.TextWidth("X") \ Screen.TwipsPerPixelX, _
.TextHeight("X") \ Screen.TwipsPerPixelY
End With
ShowCaret Text2.hwnd
End Sub

and all of the TextBox'es will work correctly even if they use different
fonts from each other and/or if the font sizes vary between them.

Rick - MVP


Jul 17 '05 #8

S.W. Rasmussen
P: n/a
S.W. Rasmussen
I solved the fontsize / fixed-width (I use a fixed width font anyway for
display of nucleotide and protein sequences) problem rather primitively with
a few different bitmaps to handle the most comment fontsizes but the
solution you describe below is a bit more functionel and elegant. I will
change the code accordingly...

Thanks for the assistance Rick

Soeren

"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:CIGdnURPeLMxUWOiRVn-jQ@comcast.com...[color=blue][color=green][color=darkred]
> > >you were almost right:
> > >
> > >CreateCaret Text1.hwnd, Picture1.Picture, 8, 13
> > >ShowCaret Text1.hwnd
> > >
> > >do the trick[/color]
> >
> > That is a new one to me - thanks[/color]
>
> Although I *now* remember having seen those API functions, I have never
> played with them before. After taking a quick look, I noted this would[/color]
only[color=blue]
> be good to use for a fixed-width font and the caret size won't change on a
> per character basis. Also, instead of fixing the width and height with
> "magic" numbers (which, of course, means you would have to come up with[/color]
new[color=blue]
> numbers for each font size you used), this will work for all font sizes
> (again, assuming a fixed-width font)...
>
> Private Declare Function CreateCaret Lib "user32" _
> (ByVal hwnd As Long, _
> ByVal hBitmap As Long, _
> ByVal nWidth As Long, _
> ByVal nHeight As Long) As Long
>
> Private Declare Function ShowCaret Lib "user32" _
> (ByVal hwnd As Long) As Long
>
> Private Sub Text1_GotFocus()
> With Me
> Set .Font = Text1.Font
> CreateCaret Text1.hwnd, 0, _
> .TextWidth("X") \ Screen.TwipsPerPixelX, _
> .TextHeight("X") \ Screen.TwipsPerPixelY
> End With
> ShowCaret Text1.hwnd
> End Sub
>
> A couple of additional notes. First, I did away with the PictureBox which[/color]
I[color=blue]
> assume was being used to provide the basis for the "bitmap" that would
> become the caret; passing a 0 into that parameter produces the black-block
> caret also. Next, in order to generalize the sizes, I needed a way of
> getting to the width and height of the text for the font being used in the
> TextBox. I did this by setting the form's Font property to that of the
> TextBox and pulling the values from the form's TextWidth and TextHeight
> properties (the PictureBox could have been used for this purpose had I
> retained it). If you have more than one TextBox on your form, then simply
> duplicate the above for their GotFocus events like this
>
> Private Sub Text2_GotFocus()
> With Me
> Set .Font = Text2.Font
> CreateCaret Text2.hwnd, 0, _
> .TextWidth("X") \ Screen.TwipsPerPixelX, _
> .TextHeight("X") \ Screen.TwipsPerPixelY
> End With
> ShowCaret Text2.hwnd
> End Sub
>
> and all of the TextBox'es will work correctly even if they use different
> fonts from each other and/or if the font sizes vary between them.
>
> Rick - MVP
>
>[/color]


Jul 17 '05 #9

Post your reply

Help answer this question



Didn't find the answer to your Visual Basic 4 / 5 / 6 question?

You can also browse similar questions: Visual Basic 4 / 5 / 6