void XListbox_DrawItem(object sender, DrawItemEventArgs e) { bool max_width_updated = false; if (e.Index < 0) { return; } //Log.log( "Draw Item " + e.Index + " in " + Name ); GraphicsContainer gc = e.Graphics.BeginContainer(); try { object item = this.Items[e.Index]; if (item == null) { Log.log("No Item?!"); } //Log.log( "Item is a " + item.GetType() ); object display_member = item; // default value. if (this.DisplayMember != null) { if (this.DisplayMember == "") { // not really a display member.. DataRowView drv = display_member as DataRowView; if (drv != null) { if (drv.Row.RowState == DataRowState.Deleted) { display_member = "<Deleted Row>"; } else if (drv.Row.RowState == DataRowState.Detached) { display_member = "<Detached Row>"; } else { display_member = drv.Row; } } else { DataRow row = display_member as DataRow; if (drv != null) { display_member = row; } } } else { System.Reflection.PropertyInfo pi = item.GetType().GetProperty(this.DisplayMember); if (pi != null) { display_member = pi.GetValue(item, null); } else { DataRowView drv = item as DataRowView; if (drv != null && drv.Row.RowState != DataRowState.Detached && drv.Row.RowState != DataRowState.Deleted) { try { display_member = drv.Row[DisplayMember]; } catch (Exception ex) { display_member = ex.Message; } } DataRow row = item as DataRow; if (row != null && row.RowState != DataRowState.Detached && row.RowState != DataRowState.Deleted) { try { display_member = row[DisplayMember]; } catch (Exception ex) { display_member = ex.Message; } } } } } String output = display_member.ToString(); //String output = this.Items[e.Index].ToString(); //SizeF szf = e.Graphics.MeasureString( output, Font ); if (e.State.HasFlag(DrawItemState.Selected)) { e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); } else { e.Graphics.FillRectangle(new SolidBrush(this.BackColor), e.Bounds); } // bounds should be the result of the measure, and the measure should be resulting in real size. e.Graphics.TranslateTransform(e.Bounds.X - (scale_x * e.Bounds.X) , e.Bounds.Y - (scale_y * e.Bounds.Y)); e.Graphics.ScaleTransform(scale_x.ToFloat(), scale_y.ToFloat()); Brush textbrush; if (e.State.HasFlag(DrawItemState.Selected)) { textbrush = SystemBrushes.HighlightText; } else { textbrush = new SolidBrush(this.ForeColor); } //Log.log( "...Putting String " + output ); //Rectangle actual = Fraction.Scale( e.Bounds, scale_x, scale_y ); if (output.Contains("\t")) { int tabstop = 0; int tabpos = 0; String segment; for (segment = output.Substring(0, output.IndexOf("\t")); segment != null;) { if (tabstop > 0) { if (tab_stops != null && tabstop <= tab_stops.Length) { tabpos = tab_stops[tabstop - 1]; } else { tabpos = 96 * tabstop; } } tabstop++; SizeF szf = FontTracker.MeasureString(e.Graphics, segment, scale_x, scale_y); FontTracker.DrawString(e.Graphics , segment , textbrush , new Point(e.Bounds.X + tabpos + (int)(szf.Width / 2) , e.Bounds.Y + (int)(szf.Height / 2)) , scale_x , scale_y); /* * e.Graphics.DrawString( segment * , Font * , textbrush * , e.Bounds.X + tabpos * , e.Bounds.Y ); */ output = output.Substring(output.IndexOf('\t') + 1); if (output.Contains("\t")) { segment = output.Substring(0, output.IndexOf('\t')); } else { segment = null; } } if (tabstop > 0) { if (tab_stops != null && tabstop <= tab_stops.Length) { tabpos = tab_stops[tabstop - 1]; } else { Log.log("Using default tabstop"); tabpos = 96 * tabstop; } } //Log.log( "... " + output ); { SizeF szf = FontTracker.MeasureString(e.Graphics, output, scale_x, scale_y); FontTracker.DrawString(e.Graphics , output , textbrush , new Point(e.Bounds.X + tabpos + (int)(szf.Width / 2) , e.Bounds.Y + (int)(szf.Height / 2)) , scale_x , scale_y); } /* * e.Graphics.DrawString( output * , Font * , textbrush * , e.Bounds.X + tabpos * , e.Bounds.Y ); */ if (segment == null) // last one to go out { SizeF szf = FontTracker.MeasureString(e.Graphics, output, scale_x, scale_y); if (e.Bounds.X + tabpos + (int)szf.Width > max_width) { max_width_updated = true; max_width = e.Bounds.X + tabpos + (int)szf.Width; } } } else { SizeF szf = FontTracker.MeasureString(e.Graphics, output, scale_x, scale_y); FontTracker.DrawString(e.Graphics , output , textbrush , new Point(e.Bounds.X + (int)(szf.Width / 2) , e.Bounds.Y + (int)(szf.Height / 2)) , scale_x , scale_y); if (e.Bounds.X + (int)szf.Width > max_width) { max_width_updated = true; max_width = e.Bounds.X + (int)szf.Width; } } if (max_width_updated) { //Log.log( "..." + max_width + "..." + scale_x ); //Log.log( "w:" + Width ); max_width = scale_x * max_width; //Log.log( "...becomes " + max_width ); if (max_width > HorizontalExtent && UpdateExtentTimer == null) { //Log.log( "New Timer" ); UpdateExtentTimer = new Timer(); UpdateExtentTimer.Tick += new EventHandler(UpdateExtentTimer_Tick); UpdateExtentTimer.Interval = 25; UpdateExtentTimer.Start(); //this.HorizontalScrollbar = true; //this.HorizontalExtent = max_width + 10; //Log.log( "Extent change caused immediate redraw" + max_width ); } } } catch (Exception excep) { Log.log(excep.Message); } e.Graphics.EndContainer(gc); }
void DrawText(Graphics gout, bool offset_text, int Width, int Height, String outputText) { { int offset = 0; String realtext = outputText; List <SizeF> output_size; List <string> output; output = new List <string>(); output_size = new List <SizeF>(); int idx; do { int idx_a = realtext.IndexOf('\n', offset); int idx_b = realtext.IndexOf('_', offset); if (idx_a >= 0) { if (idx_b >= 0) { if (idx_a < idx_b) { idx = idx_a; } else { idx = idx_b; } } else { idx = idx_a; } } else { idx = idx_b; } //idx = realtext.IndexOf( '_', offset ); if (idx >= 0) { output.Add(realtext.Substring(offset, idx - offset)); offset = idx + 1; } else { output.Add(realtext.Substring(offset, realtext.Length - offset)); } } while(idx >= 0); int height = 0; int lineheight = 0; foreach (string s in output) { SizeF size; output_size.Add(size = FontTracker.MeasureString(gout, s, scale_x, scale_y)); //output_size.Add( size = new SizeF( gout.MeasureString( s, c.FontTracker ) ) ); height += (int)((lineheight = (int)size.Height) + (height > 0 ? 0 : 0)); } Point _point = new Point(Width, Height); Point point = new Point(); if (!offset_text) { //SizeF size = new SizeF(gout.MeasureString(realtext, c.Font)); _point.X /= 2; _point.Y /= 2; _point.Y -= (int)((height - lineheight) / 2); } else { _point.X /= 2; _point.Y /= 3; _point.Y -= (int)((height - lineheight) / 2); } point.Y = _point.Y; int n = 0; foreach (string s in output) { point.X = _point.X /*- (int)(output_size[n].Width / 2)*/; FontTracker.DrawString(gout, s, new SolidBrush(ForeColor), point, scale_x, scale_y); //gout.DrawString( s // , c.FontTracker // , attrib.text_output // , point ); point.Y += (int)(output_size[n].Height) + ((n > 0) ? 2 : 0); n++; } } }