private void KeyboardControl_Paint(object sender, PaintEventArgs e) { KeyboardLetterList list = keyboardData.letterList; // Get max frequency foreach (string key in list.Keys) { if (list.ContainsKey(key)) { KeyboardUiLetter letter = keyboardData.letterList[key]; if (letter.frequency > maxFreq) { maxFreq = letter.frequency; } } } foreach (string key in list.Keys) { if (list.ContainsKey(key)) { KeyboardUiLetter letter = keyboardData.letterList[key]; if (!letter.sup) { DrawKey(letter, e); } } } foreach (string key in list.Keys) { if (list.ContainsKey(key)) { KeyboardUiLetter letter = keyboardData.letterList[key]; if (letter.sup) { DrawKey(letter, e); } } } //SolidBrush freqBrush = new SolidBrush(Color.FromArgb(120, Color.Red)); if (lowFreq > 0) { float f = (lowFreq / 1024f).Clamp(0.01f, 1.0f); SizeF freqSize = new SizeF(f * (this.Width / 4), this.Height); RectangleF freqRect = new RectangleF(new PointF(0, 0), freqSize); LinearGradientBrush lgrad = new LinearGradientBrush(freqRect, Color.Red, Color.Transparent, LinearGradientMode.Horizontal); e.Graphics.FillRectangle(lgrad, freqRect); } if (highFreq > 0) { float f = (highFreq / 1024f).Clamp(0.01f, 1.0f); SizeF freqSize = new SizeF(f * (this.Width / 4), this.Height); RectangleF freqRect = new RectangleF(new PointF(this.Width - freqSize.Width, 0), freqSize); LinearGradientBrush lgrad = new LinearGradientBrush(freqRect, Color.Transparent, Color.Red, LinearGradientMode.Horizontal); e.Graphics.FillRectangle(lgrad, freqRect); } RectangleF rect = new RectangleF(0, 0, this.Width, this.Height); if (this.Enabled) { Pen rectPen = new Pen(Color.Black); e.Graphics.DrawRectangle(rectPen, new Rectangle(0, 0, this.Width - 1, this.Height - 1)); } if (true) { // Fade out if it's disabled or text is showing if (!string.IsNullOrEmpty(this.Text) || !this.Enabled) { SolidBrush disabledBrush = new SolidBrush(Color.FromArgb(120, Color.White)); e.Graphics.FillRectangle(disabledBrush, rect); } if (!string.IsNullOrEmpty(overrideText)) { PointF center = new PointF(rect.X + rect.Width / 2, rect.Y + rect.Height / 2); SizeF textSize = e.Graphics.MeasureString(this.overrideText, textLargeFont); PointF textPos = new PointF(center.X - textSize.Width / 2, center.Y - textSize.Height / 2); e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(80, 30, 30)), new RectangleF(textPos, textSize)); e.Graphics.DrawString(this.overrideText, textLargeFont, new SolidBrush(Color.White), this.Width / 2, this.Height / 2, centerTextFormat); } // Show custom message else if (!string.IsNullOrEmpty(this.Text)) { PointF center = new PointF(rect.X + rect.Width / 2, rect.Y + rect.Height / 2); SizeF textSize = e.Graphics.MeasureString(this.Text, textLargeFont); PointF textPos = new PointF(center.X - textSize.Width / 2, center.Y - textSize.Height / 2); e.Graphics.FillRectangle(new SolidBrush(Color.Black), new RectangleF(textPos, textSize)); e.Graphics.DrawString(this.Text, textLargeFont, new SolidBrush(Color.White), this.Width / 2, this.Height / 2, centerTextFormat); } } }
private void DrawKey(KeyboardUiLetter letter, PaintEventArgs e) { string noteKey = string.Empty; if (keyboardData.letterList.ContainsValue(letter)) { foreach (string k in keyboardData.letterList.Keys) { if (keyboardData.letterList[k].Equals(letter)) { noteKey = k; continue; } } } if (string.IsNullOrEmpty(noteKey)) { return; } string drawKey = letter.key; // k = letter.key // Determine colors SolidBrush keyBrush = new SolidBrush(Color.Transparent); if (letter.sup) { keyBrush.Color = this.Enabled ? this.ForeColor : Color.FromArgb(50, 50, 50); } else { keyBrush.Color = this.Enabled ? this.BackColor : Color.FromArgb(180, 180, 180); } // Determine region float ww = this.Width / (float)((letter.sup ? 26 : 22) - 1); SizeF keySize = new SizeF(ww, (letter.y * this.Height)); PointF keyPosition = new PointF((letter.x * this.Width) - (keySize.Width / 2f), 0f); RectangleF keyRect = new RectangleF(keyPosition, keySize); // Draw color if (letter.sup) { e.Graphics.FillRectangle(keyBrush, keyRect); } // Set clipping and such Region clip = e.Graphics.Clip; e.Graphics.SetClip(keyRect, System.Drawing.Drawing2D.CombineMode.Replace); // Draw key and frequency float keyX = keyRect.X + keyRect.Width / 2f; float keyY = keyRect.Y + keyRect.Height - 8f; if (letter.frequency > 0) { int freq = letter.frequency > 0 ? letter.frequency + 60 : 0; float size = (freq / 260f); if (size > 1f) { size = 1f; } Color freqCol = letter.sup ? Color.Yellow : Color.Red; SolidBrush freqBrush = new SolidBrush(Color.FromArgb(50 + (int)(size * 150), freqCol)); e.Graphics.FillCircle(freqBrush, keyX, (letter.sup ? keyRect.Y : keyRect.Y + keyRect.Height), size * this.Height); } // Write note if (true) { SizeF noteKeySize = e.Graphics.MeasureString(drawKey, textFont); PointF noteKeyPos = new PointF(keyX - noteKeySize.Width / 2, keyY - noteKeySize.Height / 2); e.Graphics.FillRectangle(bgContrastBrush, new RectangleF(noteKeyPos, noteKeySize)); e.Graphics.DrawString(drawKey, textFont, fgContrastBrush, keyX, keyY, centerTextFormat); } // Draw line if (!letter.sup && !noteKey.Equals("C+2")) { PointF p1 = new PointF(keyRect.X + keyRect.Width - 2, keyRect.Y); PointF p2 = new PointF(keyRect.X + keyRect.Width - 2, keyRect.Y + keyRect.Height); e.Graphics.DrawLine(new Pen(fgContrastBrush), p1, p2); } e.Graphics.SetClip(clip, System.Drawing.Drawing2D.CombineMode.Replace); if (letter.sup) { e.Graphics.DrawRectangle(new Pen(fgContrastBrush), Rectangle.Round(keyRect)); } }