Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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# 如何制作浮动控件_C#_.net_Winforms - Fatal编程技术网

C# 如何制作浮动控件

C# 如何制作浮动控件,c#,.net,winforms,C#,.net,Winforms,我想创建一个控件,使其浮动(可能)超出其包含表单的边界。这可能吗?我该怎么做 这与上下文菜单的功能非常相似,只是我需要能够向其添加其他控件,如按钮和图像。它需要是一个单独的窗口(与上下文菜单的实际功能非常相似)——您可以将其包装为一个控件,以显示无模式窗体(如果您真的愿意,甚至可以选择非矩形窗口)。由于您可以从父窗体的不可见控件创建窗口,因此您可以维护对子窗体的引用以处理窗体间的通信。查看源代码并采用该技术。有可能,TopLevel属性控制此操作。但是,设计器不支持它们,很难控制l控制在设计时也

我想创建一个控件,使其浮动(可能)超出其包含表单的边界。这可能吗?我该怎么做


这与上下文菜单的功能非常相似,只是我需要能够向其添加其他控件,如按钮和图像。

它需要是一个单独的窗口(与上下文菜单的实际功能非常相似)——您可以将其包装为一个控件,以显示无模式窗体(如果您真的愿意,甚至可以选择非矩形窗口)。由于您可以从父窗体的不可见控件创建窗口,因此您可以维护对子窗体的引用以处理窗体间的通信。

查看源代码并采用该技术。

有可能,TopLevel属性控制此操作。但是,设计器不支持它们,很难控制l控制在设计时也是顶级窗口的控件


除了ToolTip和ContextMenuStrip等组件之外,还有一个设计为顶级的类,即Form类。将其FormBorderStyle设置为None,将ControlBox设置为False,以创建一个基本的顶级窗口,您可以使用其他控件来填充该窗口。

如果希望表单的FormBorderStyle设置为None,则希望表单的FormBorderStyle设置为None行为类似于上下文菜单,然后您需要将其显示绑定到主窗体中的相应事件处理程序。下面是设置位置并从鼠标单击事件处理程序调用show的简单示例

MyForm form = new MyForm();
form.Location = PointToScreen(new Point(e.X, e.Y));
form.Show();

让您的
UserControl
覆盖
CreateParams
。例如:

[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();

protected override CreateParams CreateParams
{
    get
    {
        var cp = base.CreateParams;
        cp.ExStyle &= 0x00080000; // WS_EX_LAYERED
        cp.Style = 0x40000000 | 0x4000000; // WS_CHILD | WS_CLIPSIBLINGS
        cp.Parent = GetDesktopWindow(); 
        return cp;
    }
}
这可能会产生意想不到的效果(包括与Designer的配合不好)。我选择遵循上述模式之一,但我认为值得在此提及。查找
CreateParams
,查看其用途。(此选项是从中收集的。)

这对我很有效

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
using LollipopUIControls.UIManagers;

namespace Gamasis.Apps.Controls
{
public class FloatingButton : Button
{
    public FloatingButton()
    {
        SetStyle((ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint), true);
        DoubleBuffered = true;

        Size = new Size(50, 50);
        BackColor = Color.Transparent;

        SF.Alignment = StringAlignment.Center;
        SF.LineAlignment = StringAlignment.Center;

        AnimationTimer.Tick += new EventHandler(AnimationTick);
    }

    #region  Variables

    Timer AnimationTimer = new Timer { Interval = 1 };

    FontManager font = new FontManager();
    StringFormat SF = new StringFormat();
    Rectangle StringRectangle;

    bool Focus = false;
    int margintop = 0, marginleft = 0, marginright = 0, marginBottom = 0;
    int xx;
    int yy;

    float SizeAnimation = 0;
    float SizeIncNum;

    string fontcolor = "#FAFAFA";
    string Backcolor = "#039BE5";

    Color EnabledBGColor;
    Color EnabledBorderColor;
    Color StringColor;

    Color DisabledBGColor = ColorTranslator.FromHtml("#B0BEC5");
    Color DisabledStringColor = ColorTranslator.FromHtml("#FAFAFA");
    Color NonColor = ColorTranslator.FromHtml("#e3e5e7");

    Image bGImage = null;


    #endregion

    #region  Properties
    [Category("Custom")]
    public string BGColor
    {
        get { return Backcolor; }
        set
        {
            Backcolor = value;
            Invalidate();
        }
    }
    [Category("Custom")]
    public string FontColor
    {
        get { return fontcolor; }
        set
        {
            fontcolor = value;
            Invalidate();
        }
    }

    [Browsable(false)]
    public Font Font
    {
        get { return base.Font; }
        set { base.Font = value; }
    }

    [Browsable(false)]
    public Color ForeColor
    {
        get { return base.ForeColor; }
        set { base.ForeColor = value; }
    }

    [Category("Custom")]
    public Image BGImage
    {
        get { return bGImage; }
        set { bGImage = value; }
    }

    ImageSizeLevel bGimgSize = ImageSizeLevel.peque2;

    public ImageSizeLevel BGimgSize
    {
        get { return bGimgSize; }
        set { bGimgSize = value; }
    }
    #endregion

    #region Events
    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);

        EnabledBGColor = Color.FromArgb(30, ColorTranslator.FromHtml(BGColor));//StringColor);
        EnabledBorderColor = Color.FromArgb(20, ColorTranslator.FromHtml(BGColor));//StringColor);
        Refresh();
    }
    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);

        EnabledBGColor = ColorTranslator.FromHtml(BGColor);
        EnabledBorderColor = ColorTranslator.FromHtml(BGColor);
        Refresh();
    }
    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);

        EnabledBGColor = Color.FromArgb(30, StringColor);
        Refresh();

        xx = e.X;
        yy = e.Y;

        Focus = true;
        AnimationTimer.Start();
        Invalidate();
    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        Focus = false;
        AnimationTimer.Start();
        Invalidate();
    }

    protected override void OnTextChanged(System.EventArgs e)
    {
        base.OnTextChanged(e);
        Invalidate();
    }
    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        //StringRectangle = new Rectangle(3, 0, Width - 6, Height - 6);
    }
    #endregion

    protected override void OnResize(System.EventArgs e)
    {
        base.OnResize(e);
        //SizeIncNum = Width / 34;
        SizeIncNum = Width / 10;

    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        var G = e.Graphics;


        #region Default rectangle
        //G.SmoothingMode = SmoothingMode.HighQuality | SmoothingMode.AntiAlias;
        //G.Clear(Parent.BackColor);

        //StringColor = ColorTranslator.FromHtml(fontcolor);

        //var BG = DrawHelper.CreateRoundRect(1, 1, Width - 3, Height - 3, 1);
        //Region region = new Region(BG);

        //G.FillPath(new SolidBrush(Enabled ? EnabledBGColor : Color.White), BG);
        //G.DrawPath(new Pen(Enabled ? EnabledBorderColor : Color.White), BG);

        //G.SetClip(region, CombineMode.Replace);

        ////The Ripple Effect
        //G.FillEllipse(new SolidBrush(Color.FromArgb(30, StringColor)), xx - (SizeAnimation / 2), yy - (SizeAnimation / 2), SizeAnimation, SizeAnimation);

        //G.DrawString(Text, font.Roboto_Medium10, new SolidBrush(Enabled ? StringColor : DisabledStringColor), R, SF);

        #endregion

        #region Circle

        //G.SmoothingMode = SmoothingMode.AntiAlias;
        //G.Clear(BackColor);

        //GraphicsPath bgbtn = new GraphicsPath();
        //bgbtn.AddEllipse(0, 0, Width - 5, Height - 5);

        //GraphicsPath bgShadow = new GraphicsPath();
        //bgShadow.AddEllipse(0, 0, Width - 2, Height - 2);

        //G.FillPath(new SolidBrush(NonColor), bgShadow);
        //G.DrawPath(new Pen(NonColor), bgShadow);

        //G.FillPath(new SolidBrush(Color.DeepSkyBlue), bgbtn);            
        //G.DrawPath(new Pen(Color.DeepSkyBlue), bgbtn);

        #endregion


        ///----------------------------
        G.SmoothingMode = SmoothingMode.AntiAlias;
        G.Clear(Parent.BackColor);

        StringColor = ColorTranslator.FromHtml(fontcolor);

        //var BG = DrawHelper.CreateRoundRect(1, 1, Width - 3, Height - 3, 1);
        //Círculo principal
        GraphicsPath bgbtn = new GraphicsPath();
        bgbtn.AddEllipse(2, 0, Width - 6, Height - 6);

        //Círculo para la sombra
        GraphicsPath bgShadow = new GraphicsPath();
        bgShadow.AddEllipse(2, 4, Width - 6, Height - 6);

        // se dibuja la sombra
        G.FillPath(new SolidBrush(NonColor), bgShadow);
        G.DrawPath(new Pen(NonColor), bgShadow);

        //sedibuja el círculo principal sobre la sombra
        G.FillPath(new SolidBrush(Enabled ? ColorTranslator.FromHtml(BGColor) : DisabledBGColor), bgbtn);
        G.DrawPath(new Pen(Enabled ? ColorTranslator.FromHtml(BGColor) : DisabledBGColor), bgbtn);

        // Se da a la región forma de círculo/elipse
        Region region = new Region(bgbtn);//BG);
        G.SetClip(region, CombineMode.Replace);

        //The Ripple Effect
        if (Enabled)
            G.FillEllipse(new SolidBrush(Color.FromArgb(30, EnabledBGColor)), xx - (SizeAnimation / 2), yy - (SizeAnimation / 2), SizeAnimation, SizeAnimation);
        StringRectangle = new Rectangle((int)bgbtn.GetBounds().Location.X, (int)bgbtn.GetBounds().Location.Y,
            (int)bgbtn.GetBounds().Size.Width, (int)bgbtn.GetBounds().Size.Height);
        G.DrawString(Text, font.Roboto_Medium15, new SolidBrush(Enabled ? StringColor : DisabledStringColor), StringRectangle, SF);
        if (bGImage != null)
        {
            float imgX = 0, imgY = 0;
            imgY = (bgbtn.GetBounds().Size.Height - (int)bGimgSize) / 2;
            imgX = ((bgbtn.GetBounds().Size.Width - (int)bGimgSize) + 2) / 2;
            G.DrawImage(bGImage, imgX, imgY, (float)bGimgSize, (float)bGimgSize);
        }
    }

    protected void AnimationTick(object sender, EventArgs e)
    {
        if (Focus)
        {
            if (SizeAnimation < Width + 250)
            {
                SizeAnimation += SizeIncNum;
                this.Invalidate();
            }
        }
        else
        {
            if (SizeAnimation > 0)
            {
                SizeAnimation = 0;
                this.Invalidate();
            }
        }
    }

    public enum ImageSizeLevel
    {
        peque = 12, peque1 = 24, peque2 = 32,
        maso = 48, maso1 = 56, maso2 = 64,
        grande = 72, grande1 = 86, grande2 = 96,
        monstruo = 128, monstruo1 = 256, monstruo2 = 512
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统组件模型;
使用系统图;
使用System.Drawing.Drawing2D;
使用System.Drawing.Text;
使用System.Windows.Forms;
使用LollipopUIControls.ui管理器;
命名空间Gamasis.Apps.Controls
{
公共类浮动按钮:按钮
{
公共浮动按钮()
{
设置样式((ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDubleBuffer | ControlStyles.ResizerDraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint),true);
双缓冲=真;
尺寸=新尺寸(50,50);
背景色=颜色。透明;
SF.Alignment=StringAlignment.Center;
SF.LineAlignment=StringAlignment.Center;
AnimationTimer.Tick+=新事件处理程序(AnimationTick);
}
#区域变量
计时器动画计时器=新计时器{Interval=1};
FontManager字体=新FontManager();
StringFormat SF=新的StringFormat();
矩形串矩形;
bool Focus=false;
int margintop=0,marginleft=0,marginright=0,marginBottom=0;
int xx;
int-yy;
float SizeAnimation=0;
浮点数;
字符串fontcolor=“#FAFAFA”;
字符串Backcolor=“#039BE5”;
颜色使能的颜色;
启用颜色的OrderColor;
颜色;
Color DISABLEDBGCLOR=ColorTranslator.FromHtml(“#B0BEC5”);
Color DisabledStringColor=ColorTranslator.FromHtml(“#fafafafa”);
Color NONCLOR=ColorTranslator.FromHtml(“#e3e5e7”);
图像bGImage=null;
#端区
#区域属性
[类别(“海关”)]
公共字符串颜色
{
获取{return Backcolor;}
设置
{
背景色=值;
使无效();
}
}
[类别(“海关”)]
公共字符串字体颜色
{
获取{return fontcolor;}
设置
{
fontcolor=值;
使无效();
}
}
[可浏览(错误)]
公共字体
{
获取{return base.Font;}
设置{base.Font=value;}
}
[可浏览(错误)]
公共颜色前景色
{
获取{return base.ForeColor;}
设置{base.ForeColor=value;}
}
[类别(“海关”)]
公众形象
{
获取{返回bGImage;}
设置{bGImage=value;}
}
ImageSizeLevel bGimgSize=ImageSizeLevel.peque2;
公共图像SizeLevel BGimgSize
{
获取{return bGimgSize;}
设置{bGimgSize=value;}
}
#端区
#地区活动
MouseCenter上受保护的覆盖无效(事件参数e)
{
base.onmouseinter(e);
EnabledBGColor=Color.FromArgb(30,ColorTranslator.FromHtml(BGColor));//StringColor);
EnabledBorderColor=Color.FromArgb(20,ColorTranslator.FromHtml(BGColor));//StringColor);
刷新();
}
MouseLeave上的受保护覆盖无效(事件参数e)
{
基地,离港(e);;
EnabledBGColor=ColorTranslator.FromHtml(BGColor);
EnabledBorderColor=ColorTranslator.FromHtml(BGColor);
刷新();
}
MouseEventArgs e上的受保护覆盖无效(MouseEventArgs e)
{
base.OnMouseDown(e);
EnabledBGColor=Color.FromArgb(30,StringColor);
刷新();
xx=e.X;
yy=e.Y;
焦点=真;
AnimationTimer.Start();
使无效();
}
MouseUp上的受保护覆盖无效(MouseEventArgs e)
{
base.OnMouseUp(e);
焦点=错误;
AnimationTimer.Start();
使无效();
}
受保护的覆盖void OnTextChanged(System.EventArgs e)
{
base.OnTextChanged(e);
使无效();
}
IzeChanged上的受保护覆盖无效(EventArgs e)
{
基地.OnSizeChanged(e);
//StringRectangle=新矩形(3,0,宽度-6,高度-6);
}
#端区
受保护的覆盖void OnResize(System.EventArgs e)
{
基数(e);
//SizeIncNum=宽度/34;
SizeIncNum=宽度/10;
    private void Panel_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Panel.Left += e.X - PanelMouseDownLocation.X;
            Panel.Top += e.Y - PanelMouseDownLocation.Y;
        }
    }
    private void Panel_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left) PanelMouseDownLocation = e.Location;
    }


public Point PanelMouseDownLocation { get; set; }