Home > Net >  Why installed double buffering, start the program does not display after g.d rawstring content, use
Why installed double buffering, start the program does not display after g.d rawstring content, use

Time:05-27

The class DisplayTextForm: Form
{
Public struct TextInfo
{
Public string [] displayText;
Public Rectangle [] the 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);
Enclosing the Location=new Point (100, 100);
Enclosing BackColor=Color. Red;
This. The Show ();
Enclosing textInfos. DisplayText=displayText;
This. TextInfos. The rect=new Rectangle [5].
This. Paint +=new System. Windows. Forms. PaintEventHandler (enclosing DisplayTextForm_Paint);
Enclosing the MouseDown +=new System. Windows. Forms. MouseEventHandler (enclosing DisplayTextForm_MouseDown);
Enclosing MouseMove +=new System. Windows. Forms. MouseEventHandler (enclosing DisplayTextForm_MouseMove);
Enclosing DoubleBuffered=true;//set the form
Invalidate ().
SetStyle (ControlStyles. UserPaint, true);
SetStyle (ControlStyles. AllPaintingInWmPaint, true);//erase background is prohibited.
SetStyle (ControlStyles. DoubleBuffer, true);//double buffer
}


Private void DisplayTextForm_Paint (object sender, PaintEventArgs e)
{
The Draw ();
}

Public void the Draw ()
{
If (textInfos displayText==null | | textInfos. The rect==null)
{
return;
}
Point textPos=new Point (0, 0);
Using (Graphics g=this. CreateGraphics ())
{
G.P ageUnit=GraphicsUnit. Pixel;
G.S moothingMode=SmoothingMode. HighQuality;
StringFormat sf=new StringFormat ();
Sf. FormatFlags=StringFormatFlags. MeasureTrailingSpaces;
For (int I=0; i {
TextPos=new Point (0, I * 20);
The Font Font=new Font (" tahoma ", 10);
SizeF SizeF=g. easureString (textInfos displayText [I], font, 500, sf);
The Size Size=System. Drawing. The Size. Between (sizeF);
TextInfos. The rect [I]=new Rectangle (textPos, size);

G.D rawRectangle (new Pen (Brushes. Black), the new Rectangle (textPos, size));
If (mouseFlag!=1)
{
If (lastflag==mouseFlag) return;
G.F illRectangle (Brushes. Gray, textInfos. The rect [mouseFlag]);
Lastflag=mouseFlag;
}
G.D rawString (textInfos displayText [I], font, and Brushes. Black, textPos);
}
}
}

Private int lastflag=2;

Private void DisplayTextForm_MouseMove (object sender, MouseEventArgs e)
{
Point mousePoint=e.L ocation.
for(int i=0; i{
If (textInfos. The rect [I] the Contains (mousePoint))
{
MouseFlag=I;
Invalidate (textInfos. The rect [I]);
}
}
}

CodePudding user response:

Don't set up the double buffer, just start Graphics rendering useful content

CodePudding user response:

Just try, even set the code to the
 
The namespace double-buffering
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
Enclosing DoubleBuffered=true;//set the form
SetStyle (ControlStyles. UserPaint, true);
SetStyle (ControlStyles. AllPaintingInWmPaint, true);//erase background is prohibited.
SetStyle (ControlStyles. DoubleBuffer, true);//double buffer
}

Private void Form1_Paint (object sender, PaintEventArgs e)
{
Graphics g=this. CreateGraphics ();
G.D rawString (" song typeface SSSSS ", the new Font (" tahoma ", 10), Brushes, Black, new Point (0, 0));
}
}
}

Also don't show, the
Enclosing DoubleBuffered=true;//set the form
SetStyle (ControlStyles. UserPaint, true);
SetStyle (ControlStyles. AllPaintingInWmPaint, true);//erase background is prohibited.
SetStyle (ControlStyles. DoubleBuffer, true);//double buffer
To comment, you can show the song typeface SSSSS

CodePudding user response:

DoubleBuffered=true
Is to enable the double cache
Apparently again SetStyle (ControlStyles. DoubleBuffer, true);//double buffer
Is extra
Although SetStyle (ControlStyles. DoubleBuffer, true);//double buffer
At compile time, there is no problem, but will not pass the when debugging

CodePudding user response:

To find the answer, knot,
Double - the buffering is only enabled for a Paint event. You are directly drawing to the screen with CreateGraphics (), the right to Lear () call is very noticeable since it instantly erases the drawn image. The Not drawing in the Paint event or OnPaint method is almost always a mistake.
  •  Tags:  
  • C#
  • Related