c#用GDI+绘制文字(DrawString),实现选择其中一个字符功能
如图:
大家看看怎么实现?不用TextBox等控件。

解决方案 »

  1.   

    本帖最后由 bdmh 于 2012-05-03 16:47:40 编辑
      

  2.   

    写了一个代码,但是有些偏差。
            private void SelectText(string text,int selectindex)
            {            if (string.IsNullOrEmpty(text))
                {
                    new ArgumentNullException("text", "不能为空");
                }            Bitmap parent = (Bitmap)Image.FromFile("1.jpg");
                Graphics parentgp = Graphics.FromImage(parent);
                parentgp.PageUnit = GraphicsUnit.Pixel;            Font f = new Font("宋体", 300);            SizeF sizesingle = parentgp.MeasureString(text[0].ToString (), f);            SizeF sizesub = parentgp.MeasureString(text.Substring(0, selectindex), f);            float width = sizesingle.Width * ( 2.0F /3.0F );
                float height = sizesingle.Height;            float x = (sizesub.Width - sizesingle.Width ) + ((sizesingle.Width - width) / 2.0f);
                //float x = sizesingle.Width * (selectindex - 1) + ((sizesingle.Width - width) / 2.0f); ;            float y = 0.0f;            parentgp.FillRectangle(new SolidBrush(Color.Blue), new RectangleF(x, y, width, height));            parentgp.DrawString(text, f, new SolidBrush(Color.Black), 0.0F, 0.0F);            this.pictureBox1.Image = parent;
            }