问题
I've been working on a screen recorder lately and I wanted to implement a region recorder. I set up the code required to draw the region but the problem is that I can't get rid of the form.
I tried
TransparencyKey = BackColor;
but that made the form click through which doesn't allow the user to draw the regionI also tried
Opacity = 0F;
but that made the form invisible, and when set to very low values, it makes even the rectangle transparent.
My goal is to make the form fully transparent and clickable while the graphics I draw into it are still opaque.
EDIT: Here's the code I use to draw the region
private void frmDraw_MouseDown(object sender, MouseEventArgs e) //determines if the user has his finger on the mouse
{
if (e.Button != MouseButtons.Left) return;
currentPos = startPos = e.Location;
drawing = true;
}
private void frmDraw_MouseMove(object sender, MouseEventArgs e) //refreshes the form to redraw the rectangle
{
if (!drawing) return;
currentPos = e.Location;
Invalidate();
}
private void frmDraw_MouseUp(object sender, MouseEventArgs e) //disables drawing when the user releases the click
{
drawing = false;
Invalidate();
DialogResult d = MessageBox.Show(strings.frmDrawMessage, strings.frmDrawConfirmation, MessageBoxButtons.YesNoCancel);
switch (d)
{
case DialogResult.Yes:
finalRectangle = getRectangle();
isNotCanceled = true;
Close();
break;
case DialogResult.No:
Close();
break;
}
}
public Rectangle getRectangle() //returns a rectangle from the coordinates selected
{
return new Rectangle(
Math.Min(startPos.X, currentPos.X),
Math.Min(startPos.Y, currentPos.Y),
Math.Abs(startPos.X - currentPos.X) + (firstRectangle ? 0 : 1),
Math.Abs(startPos.Y - currentPos.Y) + (firstRectangle ? 0 : 1)
);
}
private void frmDraw_Paint(object sender, PaintEventArgs e) //this block is responsible of drawing the rectangle
{
Pen p = new Pen(new HatchBrush(HatchStyle.SmallCheckerBoard, Color.Black,Color.White), 2F); //this is the pen used to draw the rectangle
Rectangle r = getRectangle(); //this is the rectangle that needs to be drawn
firstRectangle = false;
string strToDraw = "{X=" + r.X + ", Y=" + r.Y + "} {W=" + r.Width + ", H=" + r.Height + "}"; //this string is going to be drawn to show the specifications of the rectangle
Font f = new Font("Arial", 15, FontStyle.Bold, GraphicsUnit.Pixel); //this is the font use to draw the string
Size size = e.Graphics.MeasureString(strToDraw, f).ToSize(); //this is the size it will take to draw the string
int x = r.X > Width - size.Width ? r.X - (size.Width - r.Width) : r.X; // selects the x coordinate depending
int y = r.Y < ((int)f.Size + 3) ? 0 : r.Y - (int)f.Size - 3; // on the (0, 0) point of the rectangle
Point loc = new Point(x, y); //creates a point from the previous coordinates where we draw the stats
e.Graphics.DrawRectangle(p, r); //draws the main rectangle
e.Graphics.FillRectangle(Brushes.White,new Rectangle(loc, size)); //draws the background rectangle for the string
e.Graphics.DrawString(strToDraw,f,Brushes.Gray,loc); //and finally draws the string
e.Graphics.Flush(); //flushes resources
}
Thanks
来源:https://stackoverflow.com/questions/21009054/draw-opaque-graphics-on-transparent-form-c-sharp-winforms