I add a bunch of rectangles to my WPF Canvas like this:
Rectangle rectangle = new Rectangle
{
Width = tuple.Width,
Height = tuple.Height,
Stroke = Brushes.Black,
StrokeThickness = 1
};
rectangle.MouseEnter += (s, e) => rectangle.Stroke = Brushes.Gray;
rectangle.MouseLeave += (s, e) => rectangle.Stroke = Brushes.Black;
Canvas.SetLeft(rectangle, tuple.X);
Canvas.SetTop(rectangle, tuple.Y);
canvas.Children.Add(rectangle);
What did I expect:
Border-Color of the rectangle the mouse is entering changes to gray and it stays gray as long as the mouse is inside of the area of that rectangle
What actually happens:
The border color only changes to gray if the mouse is directly on the border but remains black if the mouse is anywhere else (even inside the rectangle).
So why is that? How can I implement my expected behaviour?
If you give the rectangle a fill, everything works as expected:
Rectangle rectangle = new Rectangle
{
Width = tuple.Width,
Height = tuple.Height,
Stroke = Brushes.DarkGray,
StrokeThickness = 1,
Fill = Brushes.Transparent
};
Related
I am trying to draw a circle within my form.
But it is strange to me I set form width and height to a fixed number, i do the same for the circle, but the circle figure goes outside the form.
private void Form3_Paint(object sender, PaintEventArgs e)
{
this.SuspendLayout();
gr = this.CreateGraphics();
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Brush fill_circ3 = Brushes.Blue;
Pen ellipse_pen = new Pen(Color.Blue);
ellipse_pen.Width = (float)2.0;
this.Width = this.Height = 400;
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
gr.DrawEllipse(ellipse_pen, rect);
this.ResumeLayout();
}
The 3rd and 4th parameters of the Rectangle constructor defines the size in width and height of the circle.
See the circle I got
Why does the circle goes outside the form???! I have set form and circle sizes the same!!!
It's because you use the Window size, not the "client" size. Just replace your code by this:
gr.DrawEllipse(ellipse_pen, this.ClientRectangle);
The client area of a control is the bounds of the control, minus the
nonclient elements such as scroll bars, borders, title bars, and
menus.
I'm working to make a ColorPicker for my WPF app (I know about Extended Toolkit) and I've copied the xaml from this and it's working well but now I need to get the color is pointing the mouse. As hue is inside Canvas I've tried this:
Rectangle o = sender as Rectangle;
Canvas picker = ((Canvas)((VisualBrush)o.Fill).Visual);
Point point = e.GetPosition(picker);
RenderTargetBitmap render = new RenderTargetBitmap((int)picker.RenderSize.Width, (int)picker.RenderSize.Height, 96, 96, PixelFormats.Default);
render.Render(picker);
image.Source = render;
if ((point.X <= render.PixelWidth) && (point.Y <= render.PixelHeight)) {
var crop = new CroppedBitmap(render, new Int32Rect((int)point.X, (int)point.X, 1, 1));
var pixels = new byte[4];
crop.CopyPixels(pixels, 4, 0);
selectedColor = new SolidColorBrush(Color.FromArgb(255, pixels[2], pixels[1], pixels[0]));
selectedColorRect.Fill = selectedColor;
}
}
on my mouse move event but render.Render(picker) won't render the opacity mask used to make the gradient.
EDIT
The left is the original rectangle and in the right side is the result of the render.Render(picker); image.Source = render;, as you can see the right side is not applying the opacity mask.
I'm having a difficult time correctly rendering cells in a C1FlexGrid when I need to set a background color, draw an image, and fill a rectangle for a wide border. I can't seem to get the right combination of DrawCell, DrawImage, and FillRectangle for each cell to draw properly.
The "OwnerDrawCell" event is where I am drawing the contents, border, and image.
First I am setting the cell backcolor of each cell to something like this:
e.Style.BackColor = lockedBackColor;
Then for some cells I am drawing an image and text.
// CENTER TEXT IN CELL; IMAGE IS RIGHT JUSTIFIED, CENTERED VERTICALLY
// Must draw cell first - background color, borders, etc..
e.DrawCell(DrawCellFlags.Background | DrawCellFlags.Border);
// Draw cell text
int textWidth = (int)e.Graphics.MeasureString(e.Text, e.Style.Font).Width;
int textHeight = (int)e.Graphics.MeasureString(e.Text, e.Style.Font).Height;
float textCenterX = e.Bounds.Left + ((e.Bounds.Width - textWidth) / 2);
float textCenterY = e.Bounds.Top + ((e.Bounds.Height - textHeight) / 2);
e.Graphics.DrawString(e.Text, e.Style.Font, brushColorForString, textCenterX, textCenterY);
if (e.Row == 8 || PlantHasBins())
{
// Draw cell image
int cellImageX = e.Bounds.Right - _cellImage.Width;
int cellImageY = e.Bounds.Top + ((e.Bounds.Height - _cellImage.Height) / 2);
var cellImagePoint = new Point(cellImageX, cellImageY);
e.Graphics.DrawImage(_cellImage, cellImagePoint);
}
e.Handled = true;
Then for some columns I want to draw a heavy right border to give a visual separation between groups of columns.
e.DrawCell(DrawCellFlags.Border);
Rectangle rc;
Margins m = new Margins(0, 0, 0, 0);
m.Right = 3;
CellRange rg;
rg = PlantAlleyBinGrid.GetCellRange(e.Row, e.Col);
rc = e.Bounds;
rg.c1 = rg.c2 = 2 + 1;
rg.c1 = rg.c2 = 2;
rc.X = rc.Right - m.Right;
rc.Width = m.Right;
e.Graphics.FillRectangle(new SolidBrush(Color.Black), rc);
e.Handled = true;
The code as written is almost there. I've tried many alternatives and flows but it ends up not drawing the cell image, the cell border, the cell contents, or any combination thereof.
I need help on how to draw everything on a cell.
I resolved this by calling DrawCell and DrawString for cells that do not draw an image.
I'm drawing a rectangle onto a Panel object (PanelArea) using the following code:
private void GenerateGraphic()
{
Graphics RandomArea = PanelArea.CreateGraphics();
RandomArea.Clear(Color.Beige);
SolidBrush Brush1 = new SolidBrush(Color.FromArgb(128,220,20,60));
Rectangle rect = new Rectangle();
//Offset is from the top left hand corner.
rect.X = 25;
rect.Y = 25;
rect.Width = 1;
rect.Height = 1;
rect.Inflate(XArea / 2, YArea / 2);
RandomArea.FillRectangle(Brush1, rect);
}
I'm setting the BackgroundImage of the Panel to a snippet of the screen.
I'd like to be able to keep the background image, whilst resetting the rectangle for it to be resized.
I need RandomArea.Clear(Color.Beige) to reset the graphics in order to remove the previous rectangle. The snag is though, is that this places the colour over the background image of the panel.
How can I clear the graphics area/rectangle each time, whilst still being able to view a background image for the Panel?
in My LocationChanged event, i'd like to change the width/Height when i get closer with the screen borders.
i'm currently using the following code:
private void Form1_LocationChanged(object sender, EventArgs e)
{
int BORDER_SIZE = 100;
Rectangle border = new Rectangle(
BORDER_SIZE,
BORDER_SIZE,
Screen.PrimaryScreen.Bounds.Width - BORDER_SIZE,
Screen.PrimaryScreen.Bounds.Height - BORDER_SIZE);
if(!border.Contains(Cursor.Position) )
{
this.Height = 400;
this.Width = 50;
radPanel1.Height = 400;
radPanel1.Width = 50;
this.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
radMenu1.Orientation = System.Windows.Forms.Orientation.Vertical;
}
When i drag my form to the border in question (keeping my click pressed) it is working, i see my height and width changing but when i release my mouse, the width and height go back to their default values.
I don't understand why.
Moreover, the anchor does not work.
What am i missing please?
Thanks in advance
it drives me crazy
resolved using the link posted