Wie vermeiden Sie von Richtextbox bis zum Flackern?C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Wie vermeiden Sie von Richtextbox bis zum Flackern?

Post by Anonymous »

Ich nenne die Methode updaterichTextBox in einem PictureBox -Mausereignis. < /p>

Code: Select all

private void pbImage_MouseMove(object sender, MouseEventArgs e)
{
if (pbImage.Image == null) return;

Bitmap img = new Bitmap(pbImage.Image);
int imgWidth = img.Width;
int imgHeight = img.Height;
int pbWidth = pbImage.Width;
int pbHeight = pbImage.Height;

// Compute scale to fit in PictureBox while maintaining aspect ratio
float scaleX = (float)pbWidth / imgWidth;
float scaleY = (float)pbHeight / imgHeight;
float scale = Math.Min(scaleX, scaleY); // Maintain aspect ratio

// Calculate the actual displayed image size in PictureBox
int displayWidth = (int)(imgWidth * scale);
int displayHeight = (int)(imgHeight * scale);

// Compute offsets (padding) due to aspect ratio
int offsetX = (pbWidth - displayWidth) / 2;
int offsetY = (pbHeight - displayHeight) / 2;

// Adjust mouse coordinates relative to image
int imgX = (int)((e.X - offsetX) / scale);
int imgY = (int)((e.Y - offsetY) / scale);

// Ensure coordinates are within the image bounds
if (imgX >= 0 && imgX < imgWidth && imgY >= 0 && imgY < imgHeight)
{
// LockBits for fast pixel reading
BitmapData bmpData = img.LockBits(new Rectangle(0, 0, imgWidth, imgHeight),
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);

IntPtr ptr = bmpData.Scan0;
int stride = bmpData.Stride;
int bytesPerPixel = 3;

int index = (imgY * stride) + (imgX * bytesPerPixel);
byte[] pixelData = new byte[3];

Marshal.Copy(ptr + index, pixelData, 0, 3);

img.UnlockBits(bmpData);

Color pixelColor = Color.FromArgb(pixelData[2], pixelData[1], pixelData[0]);
string hexColor = $"#{pixelColor.R:X2}{pixelColor.G:X2}{pixelColor.B:X2}";
string colorName = GetClosestColorName(pixelColor);
string mouseInfoText =
$"🖱 **Mouse Hover Information**\n"
+ "----------------------------------\n"
+ $"📍 **Coordinates:** X = {imgX}, Y = {imgY}\n"
+ $"🎨 **RGB:** ({pixelColor.R}, {pixelColor.G}, {pixelColor.B})\n"
+ $"🌈 **HEX:** {hexColor}\n"
+ $"🖌 **Color Name:** {colorName}\n";

UpdateRichTextBox(imageInfoText + mouseInfoText);
}
}
< /code>
Die Methode updieSichTextBox < /p>
private void UpdateRichTextBox(string text)
{
if (InvokeRequired)
{
Invoke(new Action(() => UpdateRichTextBox(text)));
return;
}

rtbImageInfo.Text = text;
rtbImageInfo.SelectionStart = 0;
rtbImageInfo.ScrollToCaret();
}
< /code>
Ich habe im Form111 -Konstruktor versucht, < /p>
hinzuzufügenDoubleBuffered = true;
< /code>
Aber es hat nicht viel geholfen. und resumelayout (); < /p>
private void UpdateRichTextBox(string text)
{
if (InvokeRequired)
{
Invoke(new Action(() => UpdateRichTextBox(text)));
return;
}

rtbImageInfo.SuspendLayout();
rtbImageInfo.Text = text;
rtbImageInfo.SelectionStart = 0;
rtbImageInfo.ScrollToCaret();
rtbImageInfo.ResumeLayout();
rtbImageInfo.Update();
}
< /code>
Aber bisher hat nichts geholfen.  < /p>
Ich habe auch versucht, diese Methoden hinzuzufügenprivate void SuspendRichTextBox()
{
SendMessage(rtbImageInfo.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
}

private void ResumeRichTextBox()
{
SendMessage(rtbImageInfo.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
rtbImageInfo.Invalidate();
}

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
private const int WM_SETREDRAW = 0x0B;
< /code>
und verwenden Sie es wie diese < /p>
private void UpdateRichTextBox(string text)
{
if (InvokeRequired)
{
Invoke(new Action(() => UpdateRichTextBox(text)));
return;
}

SuspendRichTextBox(); // Stop flickering

rtbImageInfo.Text = text; // Overwrite instead of appending
rtbImageInfo.SelectionStart = 0; // Keep scrollbar at the top
rtbImageInfo.ScrollToCaret(); // Ensure scrollbar stays fixed

ResumeRichTextBox(); // Resume normal behavior
}
Aber die Richtextbox flackert immer noch.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post