class DisplayTextForm : Form
{
public struct TextInfo
{
public string[] displayText;
public Rectangle[] rect;
}
[DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
private static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);
[DllImport("user32", EntryPoint = "SetWindowLong")]
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
private TextInfo textInfos = new TextInfo();
private Label label1;
private int mouseFlag = -1;
public DisplayTextForm(string[] displayText,Form parentForm)
{
this.Size = new Size(300, 300);
this.Location = new Point(100, 100);
this.BackColor = Color.Red;
this.Show();
this.textInfos.displayText = displayText;
this.textInfos.rect = new Rectangle[5];
this.Paint += new System.Windows.Forms.PaintEventHandler(this.DisplayTextForm_Paint);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DisplayTextForm_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.DisplayTextForm_MouseMove);
this.DoubleBuffered = true;//設定本表單
Invalidate();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 雙緩沖
}
private void DisplayTextForm_Paint(object sender, PaintEventArgs e)
{
Draw();
}
public void Draw()
{
if (textInfos.displayText == null || textInfos.rect == null)
{
return;
}
Point textPos = new Point(0, 0);
using (Graphics g = this.CreateGraphics())
{
g.PageUnit = GraphicsUnit.Pixel;
g.SmoothingMode = SmoothingMode.HighQuality;
StringFormat sf = new StringFormat();
sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
for (int i = 0; i < textInfos.displayText.Length; i++)
{
textPos = new Point(0, i * 20);
Font font = new Font("宋體", 10);
SizeF sizeF = g.MeasureString(textInfos.displayText[i], font, 500, sf);
Size size = System.Drawing.Size.Ceiling(sizeF);
textInfos.rect[i] = new Rectangle(textPos, size);
g.DrawRectangle(new Pen(Brushes.Black), new Rectangle(textPos, size));
if(mouseFlag != -1)
{
if (lastflag == mouseFlag) return;
g.FillRectangle(Brushes.Gray, textInfos.rect[mouseFlag]);
lastflag = mouseFlag;
}
g.DrawString(textInfos.displayText[i], font, Brushes.Black, textPos);
}
}
}
private int lastflag = -2;
private void DisplayTextForm_MouseMove(object sender, MouseEventArgs e)
{
Point mousePoint = e.Location;
for(int i = 0; i< textInfos.rect.Length; i++)
{
if(textInfos.rect[i].Contains(mousePoint))
{
mouseFlag = i;
Invalidate(textInfos.rect[i]);
}
}
}
uj5u.com熱心網友回復:
不設定雙緩沖,剛啟動就有用Graphics繪制的內容uj5u.com熱心網友回復:
剛才試了下,就算設定代碼為
namespace 雙緩沖
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;//設定本表單
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 雙緩沖
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawString("宋體sssss", new Font("宋體", 10), Brushes.Black, new Point(0, 0));
}
}
}
也是不顯示的,把
this.DoubleBuffered = true;//設定本表單
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 雙緩沖
給注釋了,就能顯示宋體sssss
uj5u.com熱心網友回復:
DoubleBuffered = true就是啟用雙快取
顯然再 SetStyle(ControlStyles.DoubleBuffer, true); // 雙緩沖
就多余了
雖然 SetStyle(ControlStyles.DoubleBuffer, true); // 雙緩沖
在編譯時并無問題。但在除錯時就通不過了
uj5u.com熱心網友回復:
找到答案了,結帖。Double-buffering is only enabled for the Paint event. You are directly drawing to the screen with CreateGraphics(), the g.Clear() call is very noticeable since it instantly erases the drawn image. Not drawing in the Paint event or OnPaint method is almost always a mistake.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/285237.html
標籤:C#
上一篇:當在datagridview內拖動行時,拖動到頂部或者底端,滾動條有隱藏的行時,如何讓滾動條跟著拖動而向上向下滾動
