By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

how to change cursor shape in RichTextBox

P: n/a
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


P: n/a
> 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?


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

P: n/a
On Wed, 7 Jan 2004 08:47:36 -0000, "S.W. Rasmussen" <sw*@seqtools.dk>
wrote:
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?


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

P: n/a
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
sw*@seqtools.dk
www.seqtools.dk
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:me********************@comcast.com...
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?
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 #4

P: n/a
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 <sw*@seqtools.dk> wrote in message
news:bt**********@news.cybercity.dk...
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
sw*@seqtools.dk
www.seqtools.dk
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:me********************@comcast.com...
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?


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 #5

P: n/a
you were almost right:

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

do the trick

Soeren

"mayayana" <ma************@mindZZspring.com> wrote in message
news:TS*******************@newsread3.news.atl.eart hlink.net...
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 <sw*@seqtools.dk> wrote in message
news:bt**********@news.cybercity.dk...
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
sw*@seqtools.dk
www.seqtools.dk
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:me********************@comcast.com...
> 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?

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 #6

P: n/a
On Thu, 8 Jan 2004 18:36:12 -0000, "S.W. Rasmussen" <sw*@seqtools.dk>
wrote:
you were almost right:

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

do the trick


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

P: n/a
> >you were almost right:

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

do the trick


That is a new one to me - thanks


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

P: n/a
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" <ri************@NOSPAMcomcast.net> wrote in message
news:CI********************@comcast.com...
you were almost right:

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

do the trick
That is a new one to me - thanks


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 #9

This discussion thread is closed

Replies have been disabled for this discussion.