Reputation: 43
I hope this is a simple question. I do the following:
Now when I position the cursor over one of the small resizer boxes, the cursor flickers. I see glimpses of the resize arrow cursor but most the time it displays the I-beam cursor. It does not steadily show the arrow cursors as it does when the a picture is pasted into WordPad and the cursor placed over the one of the small resize boxes. Should resizing a picture in the RichTextBox behave the same as in WordPad? How can I stop the cursor flicker?
Upvotes: 4
Views: 787
Reputation: 3025
With this hack you will be able to resize the image without flickering, and with the correct Arrows Cursors
.
How
First you will need to subclass the RichTextBox
and override the method WndProc
, so when the RichTextBox
receives the message to change its Cursor
, we will check if the image is select --- well, I don't really known if is an Image
, but it is an Object
and not Text
.
If the Image
is selected, we redirect the message
to DefWndProc
--- which is the Default Window Procedure.
The code:
public class RichTextBoxEx : RichTextBox
{
private const int WM_SETCURSOR = 0x20;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SETCURSOR)
{
if (SelectionType == RichTextBoxSelectionTypes.Object)
{
DefWndProc(ref m);
return;
}
}
base.WndProc(ref m);
}
}
Upvotes: 2
Reputation: 3025
It's 2018 and this problem is still happening...
It is not the best, but I create a workaround. I believe that we can improve this code --- maybe in the future I do it myself.
You need to subclass RichTextBox
and then add the following to force the Cursor
to be what it should be.
Notice that the Cursor
is either Cross
for Objects like pictures OR I-Beam
for texts.
How it works:
Every time that the RichTextBox
requests a Cursor change (SetCursor
), we intercept it and check if an Object is Selected
.
If true, then change the cursor for Cross
. If false, then change it to I-Beam
.
class RichTextBoxEx : RichTextBox
{
private const int WM_SETCURSOR = 0x20;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SetCursor(IntPtr hCursor);
protected override void WndProc(ref Message m) {
if (m.Msg == WM_SETCURSOR)
{
if (SelectionType == RichTextBoxSelectionTypes.Object)
{
// Necessary to avoid recursive calls
if (Cursor != Cursors.Cross)
{
Cursor = Cursors.Cross;
}
}
else
{
// Necessary to avoid recursive calls
if (Cursor != Cursors.IBeam)
{
Cursor = Cursors.IBeam;
}
}
SetCursor(Cursor.Handle);
return;
}
base.WndProc(ref m);
}
}
Upvotes: 1
Reputation: 5990
Use the following property
/// <summary>
/// The Lower property CreateParams is being used to reduce flicker
/// </summary>
protected override CreateParams CreateParams
{
get
{
const int WS_EX_COMPOSITED = 0x02000000;
var cp = base.CreateParams;
cp.ExStyle |= WS_EX_COMPOSITED;
return cp;
}
}
I have already answered here.
Upvotes: 0