Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(C#Winforms)在文本框上画一个圆_C#_Winforms - Fatal编程技术网

(C#Winforms)在文本框上画一个圆

(C#Winforms)在文本框上画一个圆,c#,winforms,C#,Winforms,对不起,我的英语很差 我有一个用户控件,其中包括两个文本框。我想在上面画个圈 我试着使用下面这样的透明面板。(此代码来自) 公共部分类表单1:表单 { 公共表格1() { 初始化组件(); } 专用空心画圈(整数x、整数y、整数透明度、图形) { 如果(透明度255) 透明度=255; 钢笔=新钢笔(颜色:红色,5) 图形学.抽屉(钢笔,新矩形(x,y,90,90)); pen.Dispose(); graphics.Dispose(); } 私有void TransparentPanel1_P

对不起,我的英语很差

我有一个用户控件,其中包括两个文本框。我想在上面画个圈

我试着使用下面这样的透明面板。(此代码来自)

公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
专用空心画圈(整数x、整数y、整数透明度、图形)
{
如果(透明度<0)
透明度=0;
否则如果(透明度>255)
透明度=255;
钢笔=新钢笔(颜色:红色,5)
图形学.抽屉(钢笔,新矩形(x,y,90,90));
pen.Dispose();
graphics.Dispose();
}
私有void TransparentPanel1_Paint(对象发送器,PaintEventArgs e)
{
画圈(10,10,255,e.Graphics);
}
私有void Form1\u加载(对象发送方、事件参数e)
{
transparentPanel1.Enabled=false;
transparentPanel1.Paint+=transparentPanel1_Paint;
透明面板1.BringToFront();
}
}
公共类透明面板:面板
{
受保护的重写CreateParams CreateParams
{
得到
{
CreateParams cp=base.CreateParams;
cp.ExStyle |=0x00000020;//WS_EX_透明
返回cp;
}
}
PaintBackground上受保护的覆盖无效(PaintEventArgs e)
{
//根据背景(e);
}
}
但是,它不起作用

当我使用普通面板而不是透明面板时,背景色会覆盖整个文本框,因此我看不到文本。我不想那样

当圆圈出现时,我不需要编辑文本,所以这个文本框可以替换为标签。(但当圆圈不存在时,我仍然需要编辑文本。)


如何在文本框上画一个圆?(圆圈可以替换为“圆圈图像文件”。但圆圈的背景仍然需要透明。)

我遇到了类似的问题,我想出的最佳解决方案是从父面板制作屏幕截图,并在另一个面板中显示图像。此面板变为可见,而包含所有控件的另一个面板变为不可见。我用它来显示加载屏幕,而不使用模态形式

我在一个旧的VB.NET应用程序中找到了该代码,希望翻译后的代码能够正常工作:

class NativeMethods
{
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);

    internal static Image PrintControl(Control ctrl)
    {
        using (Graphics controlGraphics = ctrl.CreateGraphics())
        {
            Bitmap bmp = new Bitmap(ctrl.Size.Width, ctrl.Size.Height, controlGraphics);

            using (Graphics bmpGraphics = Graphics.FromImage(bmp))
            {
                IntPtr dc = bmpGraphics.GetHdc();
                PrintWindow(ctrl.Handle, dc, 0);
                bmpGraphics.ReleaseHdc(dc);
                return bmp;
            }
        }
    }
}
如果您有
Panel1
,其中包含
TextBox
控件和
Panel2
作为覆盖(其中包含带红色圆圈的屏幕截图),您可以使用如下代码:

        private void ShowRedCircle()
        {
            Image bmp = NativeMethods.PrintControl(this.panel1);

            using (Graphics bmpGraphics = Graphics.FromImage(bmp))
            using (Pen sPen = new Pen(Color.Red))
            {
                bmpGraphics.DrawEllipse(sPen, new Rectangle(10, 10, 90, 90));
                this.panel2.BackgroundImage = bmp;
            }

            this.panel2.Visible = true;
            this.panel1.Visible = false;
        }

如果要删除圆,只需再次更改面板的可见性。在这种情况下,你应该考虑处理Panel2的背景值。

我遇到了一个类似的问题,我想出的最佳解决方案是从父面板上截图并在另一个面板中显示图像。此面板变为可见,而包含所有控件的另一个面板变为不可见。我用它来显示加载屏幕,而不使用模态形式

我在一个旧的VB.NET应用程序中找到了该代码,希望翻译后的代码能够正常工作:

class NativeMethods
{
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);

    internal static Image PrintControl(Control ctrl)
    {
        using (Graphics controlGraphics = ctrl.CreateGraphics())
        {
            Bitmap bmp = new Bitmap(ctrl.Size.Width, ctrl.Size.Height, controlGraphics);

            using (Graphics bmpGraphics = Graphics.FromImage(bmp))
            {
                IntPtr dc = bmpGraphics.GetHdc();
                PrintWindow(ctrl.Handle, dc, 0);
                bmpGraphics.ReleaseHdc(dc);
                return bmp;
            }
        }
    }
}
如果您有
Panel1
,其中包含
TextBox
控件和
Panel2
作为覆盖(其中包含带红色圆圈的屏幕截图),您可以使用如下代码:

        private void ShowRedCircle()
        {
            Image bmp = NativeMethods.PrintControl(this.panel1);

            using (Graphics bmpGraphics = Graphics.FromImage(bmp))
            using (Pen sPen = new Pen(Color.Red))
            {
                bmpGraphics.DrawEllipse(sPen, new Rectangle(10, 10, 90, 90));
                this.panel2.BackgroundImage = bmp;
            }

            this.panel2.Visible = true;
            this.panel1.Visible = false;
        }

如果要删除圆,只需再次更改面板的可见性。在这种情况下,您应该考虑处理PANEL2的后台值。

< P>可以通过将区域属性设置为环来实现这一点。

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace WinForm
{
    public partial class Form1 : Form
    {
        TextBox textBox1;
        TextBox textBox2;
        Panel circle;

        public Form1()
        {
            //InitializeComponent();
            textBox1 = new TextBox { Parent = this, Width = 100, Left = 20, Top = 20, Height = 80, AutoSize = false };
            textBox2 = new TextBox { Parent = this, Width = 100, Left = 20, Top = textBox1.Bottom };

            ShowCircle();
        }
        void ShowCircle()
        {
            circle = new Panel
            {
                Parent = this,
                BackColor = Color.Red,
                Top = textBox1.Top,
                Left = textBox1.Left,
                Width = textBox1.Width,
                Height = textBox1.Height + textBox2.Height
            };

            using (var path = new GraphicsPath())
            {
                var rect = new Rectangle(0, 0, circle.Width, circle.Height);
                path.AddEllipse(rect);
                rect.Inflate(-5, -5);
                path.AddEllipse(rect);

                circle.Region = new Region(path);
                circle.BringToFront();
            }
        }
    }
}

可以通过将区域属性设置为环来实现这一点

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace WinForm
{
    public partial class Form1 : Form
    {
        TextBox textBox1;
        TextBox textBox2;
        Panel circle;

        public Form1()
        {
            //InitializeComponent();
            textBox1 = new TextBox { Parent = this, Width = 100, Left = 20, Top = 20, Height = 80, AutoSize = false };
            textBox2 = new TextBox { Parent = this, Width = 100, Left = 20, Top = textBox1.Bottom };

            ShowCircle();
        }
        void ShowCircle()
        {
            circle = new Panel
            {
                Parent = this,
                BackColor = Color.Red,
                Top = textBox1.Top,
                Left = textBox1.Left,
                Width = textBox1.Width,
                Height = textBox1.Height + textBox2.Height
            };

            using (var path = new GraphicsPath())
            {
                var rect = new Rectangle(0, 0, circle.Width, circle.Height);
                path.AddEllipse(rect);
                rect.Inflate(-5, -5);
                path.AddEllipse(rect);

                circle.Region = new Region(path);
                circle.BringToFront();
            }
        }
    }
}

TextBox是一个旧控件,它不喜欢被利用。您可以在图像和背景中添加标签/面板等,但文本不会显示。你真的需要文本框吗?它们真的会收到用户输入吗?文本框是一个传统控件,不喜欢被利用。您可以在图像和背景中添加标签/面板等,但文本不会显示。你真的需要文本框吗?