The idea here is that I redraw the combol "cell" so that it shows the block of colour and text. This is when form displays and it is about to show the dropdown:
After I have selected a colour it does weird:
Now it is all wrong. I have to hover the mouse over the control to render other bits. Just not working right.
My handler:
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if(e.ColumnIndex == 0 && e.RowIndex > 0)
{
e.PaintBackground(e.ClipBounds, true);
e.PaintContent(e.ClipBounds);
Graphics g = e.Graphics;
Color c = Color.Empty;
string s = "";
Brush br = SystemBrushes.WindowText;
Brush brBack;
Rectangle rDraw;
rDraw = e.ClipBounds;
rDraw.Inflate(-1, -1);
{
brBack = Brushes.White;
g.FillRectangle(brBack, e.ClipBounds);
}
try
{
ComboboxColorItem oColorItem = (ComboboxColorItem)((ComboBox)sender).SelectedItem;
s = oColorItem.ToString();
c = oColorItem.Value;
}
catch
{
s = "red";
c = Color.Red;
}
SolidBrush b = new SolidBrush(c);
Rectangle r = new Rectangle(e.ClipBounds.Left + 5, e.ClipBounds.Top + 3, 10, 10);
g.FillRectangle(b, r);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, Form.DefaultFont, Brushes.Black, e.ClipBounds.Left + 25, e.ClipBounds.Top + 1);
b.Dispose();
g.Dispose();
e.Handled = true;
}
}
}
Is there something I am missing? Must be.
Update:
I tried this in the CellPainting event:
if(e.ColumnIndex == 0 && e.RowIndex > 0)
{
using (Graphics g = e.Graphics)
{
g.FillRectangle(Brushes.Aqua, e.CellBounds);
}
}
else
{
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
}
e.Handled = true;
That improves things in the sense that it does not go as weird. Ofcourse, it is not actually drawing anything. But then it doe snot take long for the left most cells (with the editing symbols) to only show in white. So the mechanics of it are still not right.
Thank you.
If I try it the way suggested I end up with:
Made progress! Can we adkjust it to still include the grid lines? Like in normal cells?
After
exchanging all ClipBounds by CellBounds
Deleting the g.Dispose();
..things look almost normal.
This is the result :
Of this Paint event:
private void dataGridView2_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 4 && e.RowIndex == 0) // use your own checks here!!
{
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
Graphics g = e.Graphics;
Color c = Color.Empty;
string s = "";
Brush br = SystemBrushes.WindowText;
Brush brBack;
Rectangle rDraw;
rDraw = e.CellBounds;
rDraw.Inflate(-1, -1);
{
brBack = Brushes.White;
g.FillRectangle(brBack, rDraw); // **
}
try
{ // use your own code here again!
// ComboboxColorItem oColorItem =
// (ComboboxColorItem)((ComboBox)sender).SelectedItem;
s = "WW";// oColorItem.ToString();
c = Color.LawnGreen;// oColorItem.Value;
} catch
{
s = "red";
c = Color.Red;
}
// asuming a square is right; make it a few pixels smaller!
int butSize = e.CellBounds.Height;
Rectangle rbut = new Rectangle(e.CellBounds.Right - butSize ,
e.CellBounds.Top, butSize , butSize );
ComboBoxRenderer.DrawDropDownButton(e.Graphics, rbut,
System.Windows.Forms.VisualStyles.ComboBoxState.Normal);
SolidBrush b = new SolidBrush(c);
Rectangle r = new Rectangle( e.CellBounds.Left + 5,
e.CellBounds.Top + 3, 10, 10);
g.FillRectangle(b, r);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, Form.DefaultFont, Brushes.Black,
e.CellBounds.Left + 25, e.CellBounds.Top + 1);
b.Dispose();
//g.Dispose(); <-- do not dispose of thing you have not created!
e.Handled = true;
}
}
Note that I only have one CombBoxCell, so I changed the checks. And that I have no ComboboxColorItem, so I substituted a random string & color.
Update from OP: I had some of the syntax wrong and needed:
// use your own code here again!
if(DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
ComboboxColorItem oColorItem = (ComboboxColorItem)DataGridView1
.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
s = oColorItem.ToString();
c = oColorItem.Value;
}
Related
The basic TabControl doesn't fit my needs design wise and needed a redesign.
The things that i needed were:
No borders
Gray background with white text for the selected tab
Black background with gray text for not selected tab
No doted line inside the selected tab (this one is a bit low priority)
I managed to fix some of these issues by creating a new class except for the last two. Seeing my limited experience (first time doing something like this) I was hoping somebody here could point me in the right direction.
Below an illustration of what it currently is and how I would want it to look. And of course the code of the class I used to do this.
What it looks like:
What i want it to look like:
The code:
class CustomTabControl : TabControl
{
public CustomTabControl(): base()
{
this.DrawMode = TabDrawMode.OwnerDrawFixed;
this.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
Font fntTab;
Brush bshBack;
Brush bshFore;
if (e.Index == this.SelectedIndex)
{
fntTab = new Font(e.Font, FontStyle.Bold);
bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.FromArgb(64,64,64), Color.FromArgb(0, 0, 0), System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
bshFore = Brushes.White;
}
else
{
fntTab = new Font(e.Font, FontStyle.Bold);
bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.FromArgb(0,0,0), Color.FromArgb(0, 0, 0), System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
bshFore = Brushes.Gray;
}
string tabName = this.TabPages[e.Index].Text;
StringFormat sftTab = new StringFormat();
e.Graphics.FillRectangle(bshBack, e.Bounds);
Rectangle recTab = e.Bounds;
recTab = new Rectangle(recTab.X +3 , recTab.Y+3, recTab.Width, recTab.Height );
e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
Rectangle r = this.GetTabRect(this.TabPages.Count - 1);
RectangleF tf = new RectangleF(r.X + r.Width, r.Y - 2, this.Width - (r.X + r.Width) + 0, r.Height + 4);
Brush b = Brushes.Black;
e.Graphics.FillRectangle(b, tf);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0005)
{
int Width = unchecked((short)m.LParam);
int Height = unchecked((short)((uint)m.LParam >> 16));
Region = new Region(new Rectangle(4, 2, Width - 8, Height - 6));
}
base.WndProc(ref m);
}
}
(And suggestions on improving this class are welcome.)
First i tried changing dataGridView1.BackgroundColor, dataGridView1.GridColor but didn't worked.. then i tried dataGridView1.EnableHeadersVisualStyles = false dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.White but nothing worked for me..
You need to handle CellPainting event and fill the background with desired color, for example the same color as GridColor, then perform the rest of painting by limiting the paint area to a rectangle excluding divider:
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1)
{
var dgv = (DataGridView)sender;
var r = e.CellBounds;
var w = 0;
if (e.ColumnIndex > -1)
{
w = dgv.Columns[e.ColumnIndex].DividerWidth;
r.Width = r.Width - w;
}
e.Graphics.SetClip(r);
e.Paint(r, DataGridViewPaintParts.All);
e.Graphics.SetClip(e.CellBounds);
if (w > 0)
{
r = new Rectangle(r.Right - 1, r.Top, w + 1, r.Height);
using (var brush = new SolidBrush(dgv.GridColor))
e.Graphics.FillRectangle(brush, r);
}
e.Handled = true;
}
}
For example, if you set DividerWidth for the columns to 10 and set GridColor to Color.Red you can get the following result using above code:
So I have some code that creates a highlight effect on top of a picture box with gdi32.dll and I am wondering if there is a simpler way to do it with System.Drawing.Graphics? Basically with the gdi32.dll, I have to capture a screen shot after drawing, post that to my picturebox and then I am able to draw more stuff and change the color of the pen that I use. If I just try to change the pen thickness and color and draw on the screen again, if changes what I already drew.
Now I have a version of this that uses System.Drawing.Graphics and lots of math with FillPolygon but if I draw over an area I already drew on, it just makes the area I drew on darker. It does not do this with gdi32.dll which justr shades as long as you have not already shaded the area with the mouse. Any suggestions?
public partial class Form9 : Form
{
private bool is_mouse_down { get; set; } // Will check if the mouse is down or not.
private Color Pen_Color = new Color();
private int Pen_Type { get; set; }
private int Thickness { get; set; }
private bool Start { get; set; }
List<Point> Points = new List<Point>();
public Form9()
{
InitializeComponent();
pictureBox1.Dock = DockStyle.Fill;
Pen_Color = Color.Blue;
Pen_Type = 13; // Type = 9 for highlighter, Type = 13 for solid.
Thickness = 2;
Start = false;
pictureBox1.MouseDown += pictureBox1_MouseDown;
pictureBox1.MouseUp += pictureBox1_MouseUp;
pictureBox1.MouseMove += pictureBox1_MouseMove;
pictureBox1.Paint += pictureBox1_OnPaint;
}
private void DrawHighlight(Graphics g, Point[] usePoints, int brushSize, int penType, Color brushColor)
{
int useColor = System.Drawing.ColorTranslator.ToWin32(brushColor);
IntPtr pen = GetImage.GDI32.CreatePen(GetImage.GDI32.PS_SOLID, brushSize, (uint)useColor);
IntPtr hDC = g.GetHdc();
IntPtr xDC = GetImage.GDI32.SelectObject(hDC, pen);
GetImage.GDI32.SetROP2(hDC, penType);//GetImage.GDI32.R2_MASKPEN);
for (int i = 1; i <= usePoints.Length - 1; i++)
{
Point p1 = usePoints[i - 1];
Point p2 = usePoints[i];
GetImage.GDI32.MoveToEx(hDC, p1.X, p1.Y, IntPtr.Zero);
GetImage.GDI32.LineTo(hDC, p2.X, p2.Y);
}
GetImage.GDI32.SetROP2(hDC, GetImage.GDI32.R2_COPYPEN);
GetImage.GDI32.SelectObject(hDC, xDC);
GetImage.GDI32.DeleteObject(pen);
g.ReleaseHdc(hDC);
}
private void pictureBox1_OnPaint(object sender, PaintEventArgs e)
{
if (Start)
{
base.OnPaint(e);
if (is_mouse_down)
{
DrawHighlight(e.Graphics, Points.ToArray(), Thickness, Pen_Type, Pen_Color);
}
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Points.Clear();
Start = true;
is_mouse_down = true;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
is_mouse_down = false;
using (Image img = CaptureScreen())
{
try
{
if (System.IO.File.Exists(Program.ProgramPath + #"\Temp\marked.bmp"))
{
System.IO.File.Delete(Program.ProgramPath + #"\Temp\marked.bmp");
}
}
catch (Exception Ex)
{
MessageBox.Show("File Delete Error" + Environment.NewLine + Convert.ToString(Ex));
}
try
{
img.Save(Program.ProgramPath + #"\Temp\marked.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
catch (Exception Ex)
{
MessageBox.Show("Unable to save Screenshot" + Environment.NewLine + Convert.ToString(Ex));
}
}
if (System.IO.File.Exists(Program.ProgramPath + #"\Temp\marked.bmp"))
{
using (FileStream fs = new System.IO.FileStream(Program.ProgramPath + #"\Temp\marked.bmp", System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.Read))
{
pictureBox1.Image = Image.FromStream(fs);
}
}
pictureBox1.Invalidate(); // Refreshes picturebox image.
}
public Image CaptureScreen()
{
GetImage gi = new GetImage();
return gi.CaptureWindow(GetImage.User32.GetDesktopWindow());
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (is_mouse_down == true) // Check to see if the mouse button is down while moving over the form.
{
Points.Add(new Point(e.X, e.Y));
pictureBox1.Invalidate(); // Refreshes picturebox image.
}
}
Here are a couple photos of what I am talking about:
Using System.Drawing.Graphics:
Using gdi32.dll:
UPDATE
After testing some of your code...I got a some strange stuff.
This is a way to draw multiple independent stroke of semi-transparent color without piling up the alpha:
It uses tow lists, one for the strokes and one for the current stroke:
List<List<Point>> strokes = new List<List<Point>>();
List<Point> currentStroke = new List<Point>();
They are filled in the usual way
private void canvas_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button.HasFlag(MouseButtons.Left))
{
currentStroke.Add(e.Location);
if (currentStroke.Count == 1)
currentStroke.Add(new Point(currentStroke[0].X + 1,
currentStroke[0].Y));
canvasInvalidate();
}
}
private void canvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button.HasFlag(MouseButtons.Left))
{
currentStroke.Add(e.Location);
canvas.Invalidate();
}
}
private void canvas_MouseUp(object sender, MouseEventArgs e)
{
if (currentStroke.Count > 1)
{
strokes.Add(currentStroke.ToList());
currentStroke.Clear();
}
canvas.Invalidate();
}
In this version we avoid overlay effects of overlapping strokes by drawing all pixels in only one call. All the pixels are painted by creating a GraphicsPath from the strokes and and filling it:
private void canvas_Paint(object sender, PaintEventArgs e)
{
if (strokes.Count > 0 || currentStroke.Count > 0)
{
GraphicsPath gp = new GraphicsPath();
gp.FillMode = FillMode.Winding;
if (currentStroke.Count > 0)
{
gp.AddCurve(currentStroke.ToArray());
gp.CloseFigure();
}
foreach (var stroke in strokes)
{
gp.AddCurve(stroke.ToArray());
gp.CloseFigure();
}
using (SolidBrush b = new SolidBrush(Color.FromArgb(77, 177, 99, 22)))
{
e.Graphics.FillPath(b, gp);
}
}
}
Note that you should take care not to move back onto the current stroke while drawing it or else the croosing path parts will create holes!
A Clear or Save Button are simple, the former Clears the two list and invalidates, the latter would use DrawToBitmap to save the control..
Note: To avoid flicker do make sure the canval Panel is DoubleBuffered!
Update:
Here is another way that uses a Pen to draw the overlay. To avoid the piling up of alpha and changed color values (depending on the PixelFormat) it uses a fast function to modify all set pixels in the overlay to have the same overlay color:
The stroke collection code is the same. The Paint is reduced to calling a function to create an overlay bitmap and drawing it:
private void canvas_Paint(object sender, PaintEventArgs e)
{
using (Bitmap bmp = new Bitmap(canvas.ClientSize.Width,
canvas.ClientSize.Height, PixelFormat.Format32bppPArgb))
{
PaintToBitmap(bmp);
e.Graphics.DrawImage(bmp, 0, 0);
}
The first function does the drawing, pretty much like before, but with simple pen strokes:
private void PaintToBitmap(Bitmap bmp)
{
Color overlayColor = Color.FromArgb(77, 22, 99, 99);
using (Graphics g = Graphics.FromImage(bmp))
using (Pen p = new Pen(overlayColor, 15f))
{
p.MiterLimit = p.Width / 2;
p.EndCap = LineCap.Round;
p.StartCap = LineCap.Round;
p.LineJoin = LineJoin.Round;
g.SmoothingMode = SmoothingMode.AntiAlias;
if (currentStroke.Count > 0)
{
g.DrawCurve(p, currentStroke.ToArray());
}
foreach (var stroke in strokes)
g.DrawCurve(p, stroke.ToArray());
}
SetAlphaOverlay(bmp, overlayColor);
}
It also calls the function that 'flattens' all set pixels to the overlay color:
void SetAlphaOverlay(Bitmap bmp, Color col)
{
Size s = bmp.Size;
PixelFormat fmt = bmp.PixelFormat;
Rectangle rect = new Rectangle(Point.Empty, s);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, fmt);
int size1 = bmpData.Stride * bmpData.Height;
byte[] data = new byte[size1];
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, size1);
for (int y = 0; y < s.Height; y++)
{
for (int x = 0; x < s.Width; x++)
{
int index = y * bmpData.Stride + x * 4;
if (data[index + 0] + data[index + 1] + data[index + 2] > 0)
{
data[index + 0] = col.B;
data[index + 1] = col.G;
data[index + 2] = col.R;
data[index + 3] = col.A;
}
}
}
System.Runtime.InteropServices.Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
bmp.UnlockBits(bmpData);
}
It uses LockBits, so it is pretty fast..
Here it is in action:
Update 2:
Just for the fun of it here is an extension of only a few lines that adds the option of drawing filled curves:
The fill mode is stored in a cheapo hack by having the 1st element twice. These are the changes:
In the MouseDown:
currentStroke.Add(e.Location);
if (cbx_Fill.Checked)
currentStroke.Add(e.Location);
And in the PaintToBitmap:
g.SmoothingMode = SmoothingMode.AntiAlias;
if (currentStroke.Count > 0)
{
if (cbx_Fill.Checked)
g.FillClosedCurve(b, currentStroke.ToArray());
else
g.DrawCurve(p, currentStroke.ToArray());
}
foreach (var stroke in strokes)
if (stroke[0]==stroke[1])
g.FillClosedCurve(b, stroke.ToArray());
else
g.DrawCurve(p, stroke.ToArray());
And one more demo:
I'm using a theme with a custom TabControl control.
The control draws text for each tab using the DrawString method like so:
using (SolidBrush B = new SolidBrush(FC))
{
G.DrawString(TabPages[i].Text, Theme.GlobalFont(FontStyle.Regular, 10), B, new Point(TabRect.X + 50, TabRect.Y + 12));
}
After changing the text to Hebrew, and changing the control's RightToLeftLayout to true, the text is flipped like so:
I tried to search online and found out that you can use Transform to modify the text, however I believe this is not the case since it should be a simple task. Here's the full class for the control:
class FirefoxMainTabControl : TabControl
{
#region " Private "
private Graphics G;
private Rectangle TabRect;
#endregion
private Color FC = Color.Blue;
#region " Control "
public FirefoxMainTabControl()
{
DoubleBuffered = true;
ItemSize = new Size(43, 152);
Alignment = TabAlignment.Left;
SizeMode = TabSizeMode.Fixed;
}
protected override void OnCreateControl()
{
base.OnCreateControl();
SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
try
{
for (int i = 0; i <= TabPages.Count - 1; i++)
{
TabPages[i].BackColor = Color.White;
TabPages[i].ForeColor = Color.FromArgb(66, 79, 90);
TabPages[i].Font = Theme.GlobalFont(FontStyle.Regular, 10);
}
}
catch
{
}
}
protected override void OnPaint(PaintEventArgs e)
{
G = e.Graphics;
G.SmoothingMode = SmoothingMode.HighQuality;
G.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
base.OnPaint(e);
G.Clear(Color.FromArgb(66, 79, 90));
for (int i = 0; i <= TabPages.Count - 1; i++)
{
TabRect = GetTabRect(i);
if (SelectedIndex == i)
{
using (SolidBrush B = new SolidBrush(Color.FromArgb(52, 63, 72)))
{
G.FillRectangle(B, TabRect);
}
FC = Helpers.GreyColor(245);
using (SolidBrush B = new SolidBrush(Color.FromArgb(255, 175, 54)))
{
G.FillRectangle(B, new Rectangle(TabRect.Location.X - 3, TabRect.Location.Y + 1, 5, TabRect.Height - 2));
}
}
else
{
FC = Helpers.GreyColor(192);
using (SolidBrush B = new SolidBrush(Color.FromArgb(66, 79, 90)))
{
G.FillRectangle(B, TabRect);
}
}
using (SolidBrush B = new SolidBrush(FC))
{
G.DrawString(TabPages[i].Text, Theme.GlobalFont(FontStyle.Regular, 10), B, new Point(TabRect.X + 50, TabRect.Y + 12));
}
if ((ImageList != null))
{
if (!(TabPages[i].ImageIndex < 0))
{
G.DrawImage(ImageList.Images[TabPages[i].ImageIndex], new Rectangle(TabRect.X + 19, TabRect.Y + ((TabRect.Height / 2) - 10), 18, 18));
}
}
}
}
#endregion
}
Straight to the question: Is it possible to insert a string in a subitem which each leter has a different color???
I would like to represent time delay using colors. Example:
Subitem string "10 14 50" and values 10 and 50 with color red and 14 with green.
Try setting the OwnerDraw mode to true and supply the drawing routine yourself:
listView1.OwnerDraw = true;
listView1.DrawColumnHeader += listView1_DrawColumnHeader;
listView1.DrawSubItem += listView1_DrawSubItem;
void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) {
e.DrawDefault = true;
}
void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) {
if (e.ColumnIndex == 1) {
e.Graphics.SetClip(e.Bounds);
using (SolidBrush br = new SolidBrush(listView1.BackColor)) {
e.Graphics.FillRectangle(br, e.Bounds);
}
int textLeft = e.Bounds.Left;
string[] subItems = e.Item.SubItems[1].Text.Split(' ');
for (int i = 0; i < subItems.Length; ++i) {
int textWidth = TextRenderer.MeasureText(subItems[i], listView1.Font).Width;
TextRenderer.DrawText(e.Graphics, subItems[i], listView1.Font,
new Rectangle(textLeft, e.Bounds.Top, textWidth, e.Bounds.Height),
i == 0 ? Color.Red : i == subItems.Length - 1 ? Color.Green : Color.Black,
Color.Empty,
TextFormatFlags.VerticalCenter | TextFormatFlags.PreserveGraphicsClipping);
textLeft += textWidth;
}
e.Graphics.ResetClip();
} else {
e.DrawDefault = true;
}
}
Result: