private static int getFirstRowIndex(FontSettings settings, int offset) { if (offset < PADDING_TOP + settings.BlockHeight) { return 0; } int rows = (offset - PADDING_TOP) / (settings.BlockHeight + BLOCK_MARGIN); return rows; }
/// <summary> /// Get scroll largeChange value, for reset vScrollbar when font parameters changed /// </summary> /// <param name="settings"></param> /// <returns></returns> public static int getScrollLargeChange(FontSettings settings, int height) { int largeScrollHeight = ROWS_OF_LARGE_SCROLL * (settings.BlockHeight + BLOCK_MARGIN); int workHeight = height - PADDING_BOTTOM - PADDING_TOP; if (largeScrollHeight > workHeight) { largeScrollHeight = workHeight; } return largeScrollHeight / SCROLL_STEP; }
/// <summary> /// Get scroll maximum value, used for reset vScrollbar when font parameters changed /// </summary> /// <param name="height">picturebox client height</param> /// <param name="settings"></param> /// <returns></returns> public static int getScrollMaximum(int height, FontSettings settings) { int rowHeight = settings.BlockHeight + BLOCK_MARGIN; int rows = settings.Chars.Length / 16; if (settings.Chars.Length % 16 > 0) rows++; int blockTotalHeight = rows * rowHeight; int workHeight = height - PADDING_TOP - PADDING_BOTTOM; // pad to full page blockTotalHeight += (blockTotalHeight % workHeight); int steps = (blockTotalHeight + SCROLL_STEP) / SCROLL_STEP; return steps > 0 ? steps : 1; }
/// <summary> /// Get char's index by position of content image /// </summary> /// <remarks> /// if outrange return -1 /// </remarks> /// <param name="x"></param> /// <param name="y"></param> /// <param name="scrollOffset"></param> /// <param name="settings"></param> /// <returns>chars string index</returns> public static int getCurrentCharIndex(int x, int y, int scrollOffset, FontSettings settings) { // convert to font draw region's location x -= PADDING_LEFT; y = y - PADDING_TOP + scrollOffset * SCROLL_STEP; // outrange return -1 if (x < 0 || y < 0) return -1; // calculate row index int rows = y / (settings.BlockHeight + BLOCK_MARGIN); // calculate column index int cols = x / (settings.BlockWidth + BLOCK_MARGIN); // outrange of columns return -1 if (cols >= 16) return -1; // get index of chars string int pos = rows * 16 + cols; // if greate than chars length return -1 pos = pos >= settings.Chars.Length ? -1 : pos; return pos; }
/// <summary> /// Draw content view's image /// </summary> /// <param name="g">Graphics of Content picturebox</param> /// <param name="height">picturebox's client height</param> /// <param name="settings"></param> /// <param name="scrollOffset">vScrollbar's value</param> public static void DrawContentImage(Graphics g, int height, FontSettings settings, int scrollOffset) { int charIndex = getCurrentCharIndex(PADDING_LEFT, PADDING_TOP, scrollOffset, settings); if (charIndex == -1) return; scrollOffset *= SCROLL_STEP; int workHeight = height - PADDING_TOP - PADDING_BOTTOM; int rowHeight = settings.BlockHeight + BLOCK_MARGIN; int columnWidth = settings.BlockWidth + BLOCK_MARGIN; int rows = workHeight / rowHeight + 1; int rowOffset = scrollOffset % rowHeight; if (rowOffset > 0) rows++; g.Clear(Properties.Settings.Default.ContentBackColor); using (Bitmap bmp = new Bitmap(settings.BlockWidth, settings.BlockHeight)) { using (Graphics frame = Graphics.FromImage(bmp)) { int x = PADDING_LEFT, y = PADDING_TOP; Rectangle rectCopy = new Rectangle(0, rowOffset, bmp.Width, bmp.Height - rowOffset); Rectangle rectIndex = new Rectangle(0, rowOffset > 0 ? y - rowOffset : y, PADDING_LEFT, bmp.Height); int charCount = settings.Chars.Length; //for (int row = 0; row < rows; row++) while(y < height - PADDING_BOTTOM) { // out of range, break if (charIndex >= charCount) break; // display row index TextRenderer.DrawText(g, ((charIndex / 16).ToString("X4")), Properties.Settings.Default.RowIndexFont, rectIndex, Properties.Settings.Default.RowIndexColor, Properties.Settings.Default.ContentBackColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter); for (int column = 0; column < 16; column++) { if (charIndex >= charCount) break; DrawChar(frame, settings, settings.Chars[charIndex++]); g.DrawImage(bmp, x, y, rectCopy, GraphicsUnit.Pixel); x += columnWidth; } // after clip first row, reset copy rectangle if (PADDING_TOP == y) { rectCopy.Y = 0; rectCopy.Height = bmp.Height; } x = PADDING_LEFT; y += (PADDING_TOP == y ? (rowHeight - rowOffset) : rowHeight); rectIndex.Y += rowHeight; // clip last row if (y + rowHeight> height - PADDING_BOTTOM) { rectCopy.Height = height - PADDING_BOTTOM - y; } } // clear row index Brush b = new SolidBrush(Color.Red);//Properties.Settings.Default.ContentBackColor); g.FillRectangle(b, 0, 0, PADDING_LEFT, PADDING_TOP); g.FillRectangle(b, 0, height - PADDING_BOTTOM, PADDING_LEFT, PADDING_BOTTOM); } } }
/// <summary> /// Draw a char in given graphics /// </summary> /// <param name="g"></param> /// <param name="settings"></param> /// <param name="c"></param> public static void DrawChar(Graphics g, FontSettings settings, char c) { g.TextRenderingHint = settings.RenderingHint; g.Clear(settings.BackColor); TextRenderer.DrawText(g, c.ToString(), settings.Font, new Rectangle(settings.OffsetX, settings.OffsetY, settings.BlockWidth, settings.BlockHeight), settings.Forecolor, settings.BackColor, settings.TextFormatFlags); }
/// <summary> /// return a bitmap which drawed a char /// </summary> /// <param name="settings"></param> /// <param name="charPos"></param> /// <returns></returns> public static Bitmap DrawChar(FontSettings settings, int charPos) { Bitmap bmp = new Bitmap(settings.BlockWidth, settings.BlockHeight); using (Graphics g = Graphics.FromImage(bmp)) { DrawChar(g, settings, settings.Chars[charPos]); } return bmp; }
/// <summary> /// Get user choice and save to fontSettings /// </summary> /// <returns>if validation failured return false</returns> private bool getSettings() { if (fontSettings != null) { fontSettings.Dispose(); vScrollBar1.Value = 0; } try { fontSettings = new FontSettings(); fontSettings.SetForeColor(textBoxForecolor.Text); fontSettings.SetBackColor(textBoxBackgroundColor.Text); FontStyle fntStyle = radioButton1.Checked ? FontStyle.Regular : radioButton2.Checked ? FontStyle.Italic : radioButton3.Checked ? FontStyle.Bold : FontStyle.Bold | FontStyle.Italic; fontSettings.SetFont(listBoxSystemFont.SelectedItem, fntStyle, listBoxSize.SelectedItem); fontSettings.RenderingHint = radioButton5.Checked ? System.Drawing.Text.TextRenderingHint.SingleBitPerPixel : radioButton6.Checked ? System.Drawing.Text.TextRenderingHint.AntiAliasGridFit : radioButton7.Checked ? System.Drawing.Text.TextRenderingHint.ClearTypeGridFit : System.Drawing.Text.TextRenderingHint.SystemDefault; fontSettings.TextFormatFlags = ButtonAlignChecked == button1 ? TextFormatFlags.Left | TextFormatFlags.Top : ButtonAlignChecked == button2 ? TextFormatFlags.HorizontalCenter | TextFormatFlags.Top : ButtonAlignChecked == button3 ? TextFormatFlags.Right | TextFormatFlags.Top : ButtonAlignChecked == button4 ? TextFormatFlags.Left | TextFormatFlags.VerticalCenter : ButtonAlignChecked == button6 ? TextFormatFlags.Right | TextFormatFlags.VerticalCenter : ButtonAlignChecked == button7 ? TextFormatFlags.Left | TextFormatFlags.Bottom : ButtonAlignChecked == button8 ? TextFormatFlags.HorizontalCenter | TextFormatFlags.Bottom : ButtonAlignChecked == button9 ? TextFormatFlags.Right | TextFormatFlags.Bottom : TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; fontSettings.SetBlockSize(numericUpDownBlockWidth.Value, numericUpDownBlockHeight.Value); fontSettings.SetOffset(numericUpDownOffsetX.Value, numericUpDownOffsetY.Value); fontSettings.Chars = richTextBoxCharInput.Text; fontSettings.processChars(checkBoxIgnoreNewLine.Checked, checkBoxInsertNewLine.Checked, checkBoxSortChars.Checked, checkBoxFilterChars.Checked); // clear zoom image if (pictureBoxZoom.Image != null) { pictureBoxZoom.Image.Dispose(); pictureBoxZoom.Image = null; pictureBoxZoom.Refresh(); labelCurrent.Text = ""; } } catch (Exception ex) { Misc.MsgErr(ex.Message); return false; } return true; }