问题
I hope this is a simple question. I do the following:
- In VS2010, I create a Windows Form Application
- From the Toolbox, drag RichTextBox control to the form
- Size the form and RichTextBox control large enough to display a small picture.
- Run (start debugging).
- Copy a small image from a web browser and paste to richtextbox (using ctrl-v).
- Select the image in the richtextbox. A resize frame is displayed with small boxes.
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?
回答1:
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);
}
}
回答2:
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 isSelected
.If true, then change the cursor for
Cross
. If false, then change it toI-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);
}
}
回答3:
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.
来源:https://stackoverflow.com/questions/32304388/cursor-flickers-when-trying-to-resize-image-in-richtextbox