private void FontPreviewSurface_MouseMove(object sender, MouseEventArgs e)
        {
            var position = GetRelativeMouseLocation(e.Location, FontPreviewSurface);

            var needToInvalidate = false;

            foreach (var letterBox in m_letters)
            {
                if (letterBox.Rect.Contains(position))
                {
                    if (!letterBox.IsPreSelected)
                    {
                        letterBox.IsPreSelected = true;
                        needToInvalidate        = true;
                    }
                }
                else
                {
                    if (letterBox.IsPreSelected)
                    {
                        needToInvalidate = true;
                    }
                    letterBox.IsPreSelected = false;
                }
            }

            if (needToInvalidate)
            {
                FontPreviewSurface.Invalidate();
            }
        }
        private void FontPreviewSurface_MouseDown(object sender, MouseEventArgs e)
        {
            var position = GetRelativeMouseLocation(e.Location, FontPreviewSurface);

            var selectedLetterBox = (LetterBox)null;

            foreach (var letterBox in m_letters)
            {
                if (letterBox.Rect.Contains(position))
                {
                    if (!letterBox.IsSelected)
                    {
                        letterBox.IsSelected = true;
                        selectedLetterBox    = letterBox;
                    }
                    else
                    {
                        letterBox.IsSelected = false;
                    }
                }
                else
                {
                    letterBox.IsSelected = false;
                }
            }
            m_selectedLetterBox = selectedLetterBox;
            FontPreviewSurface.Invalidate();
        }
        private void LettersTextBox_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(LettersTextBox.Text))
            {
                m_letters.ForEach(x => x.Letter = string.Empty);
                OkButton.Enabled = false;
            }
            else
            {
                var letters  = LettersTextBox.Text;
                var minCount = Math.Min(m_letters.Count, letters.Length);
                for (var i = 0; i < minCount; i++)
                {
                    m_letters[i].Letter = letters[i].ToString();
                }

                if (letters.Length < m_letters.Count)
                {
                    m_letters.Skip(letters.Length).ForEach(x => x.Letter = string.Empty);
                }

                OkButton.Enabled = true;
            }
            FontPreviewSurface.Invalidate();
        }
        private void LoadFontButton_Click(object sender, EventArgs e)
        {
            string fileName;

            using (var op = new OpenFileDialog {
                Title = @"Select font file...", Filter = Consts.FontImportFilter
            })
            {
                if (op.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                fileName = op.FileName;
            }

            try
            {
                var pfc       = new PrivateFontCollection();
                var fontBytes = File.ReadAllBytes(fileName);
                unsafe
                {
                    fixed(byte *pFontData = fontBytes)
                    {
                        pfc.AddMemoryFont((IntPtr)pFontData, fontBytes.Length);
                    }
                }
                m_externalFontFamily = pfc.Families.First(x => x.IsStyleAvailable(FontStyle.Regular));
                m_font = CreateFont();
                FontPreviewSurface.Invalidate();
            }
            catch (Exception ex)
            {
                InfoBox.Show("An error occured during loading font file.\n" + ex.Message);
            }
        }
Esempio n. 5
0
        private void InitializeControls()
        {
            foreach (var font in FontFamily.Families)
            {
                FontComboBox.Items.Add(font.Name);
            }
            var tahomaIndex = FontComboBox.Items.IndexOf("Tahoma");

            if (tahomaIndex != -1)
            {
                FontComboBox.SelectedIndex = tahomaIndex;
            }
            else if (FontComboBox.Items.Count > 0)
            {
                FontComboBox.SelectedIndex = 0;
            }

            FontSizeUpDown.ValueChanged += (s, e) =>
            {
                m_fontSize = (float)FontSizeUpDown.Value;
                m_font     = CreateFont();
                FontPreviewSurface.Invalidate();
            };
            FontComboBox.TextChanged += (s, e) =>
            {
                m_fontName           = FontComboBox.Text;
                m_externalFontFamily = null;
                m_font = CreateFont();
                FontPreviewSurface.Invalidate();
            };
            LettersTextBox.TextChanged += LettersTextBox_TextChanged;

            LoadFontButton.Click += LoadFontButton_Click;

            BoldButton.CheckedChanged      += FontStyleButton_Click;
            ItalicButton.CheckedChanged    += FontStyleButton_Click;
            UnderlineButton.CheckedChanged += FontStyleButton_Click;

            ShiftUpButton.Click    += AdjustFontPosition;
            ShiftDownButton.Click  += AdjustFontPosition;
            ShiftLeftButton.Click  += AdjustFontPosition;
            ShiftRightButton.Click += AdjustFontPosition;

            ZeroNineButton.Click += (s, e) => LettersTextBox.Text = GetLetters('0', '9');
            LowerAZButton.Click  += (s, e) => LettersTextBox.Text = GetLetters('a', 'z');
            UpperAZButton.Click  += (s, e) => LettersTextBox.Text = GetLetters('A', 'Z');

            OkButton.Click               += OkButton_Click;
            FontPreviewSurface.Resize    += (s, e) => FontPreviewSurface.Invalidate();
            FontPreviewSurface.MouseDown += FontPreviewSurface_MouseDown;
            FontPreviewSurface.MouseMove += FontPreviewSurface_MouseMove;
            FontPreviewSurface.Paint     += FontPreviewSurface_Paint;

            m_fontName = FontComboBox.Text;
            m_fontSize = (float)FontSizeUpDown.Value;
            m_font     = CreateFont();
        }
 private void AdjustFontPosition(object sender, EventArgs e)
 {
     if (m_selectedLetterBox == null)
     {
         if (sender == ShiftUpButton)
         {
             m_letters.ForEach(x => x.LetterOffsetY--);
         }
         if (sender == ShiftDownButton)
         {
             m_letters.ForEach(x => x.LetterOffsetY++);
         }
         if (sender == ShiftLeftButton)
         {
             m_letters.ForEach(x => x.LetterOffsetX--);
         }
         if (sender == ShiftRightButton)
         {
             m_letters.ForEach(x => x.LetterOffsetX++);
         }
     }
     else
     {
         if (sender == ShiftUpButton)
         {
             m_selectedLetterBox.LetterOffsetY--;
         }
         if (sender == ShiftDownButton)
         {
             m_selectedLetterBox.LetterOffsetY++;
         }
         if (sender == ShiftLeftButton)
         {
             m_selectedLetterBox.LetterOffsetX--;
         }
         if (sender == ShiftRightButton)
         {
             m_selectedLetterBox.LetterOffsetX++;
         }
     }
     FontPreviewSurface.Invalidate();
 }
        private void FontStyleButton_Click(object sender, EventArgs eventArgs)
        {
            var newStyle = FontStyle.Regular;

            if (BoldButton.Checked)
            {
                newStyle |= FontStyle.Bold;
            }
            if (ItalicButton.Checked)
            {
                newStyle |= FontStyle.Italic;
            }
            if (UnderlineButton.Checked)
            {
                newStyle |= FontStyle.Underline;
            }

            m_fontStyle = newStyle;
            m_font      = CreateFont();
            FontPreviewSurface.Invalidate();
        }