首页 新闻 会员 周边 捐助

c#如何让Panel可以到windows10/11窗体外?

0
悬赏园豆:50 [已解决问题] 解决于 2024-06-10 14:49

 

 

 

如何做到这样,显示到外面,可以随意拖拽位置。。。

 

我的代码:

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Core;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using System.Reflection;

namespace WinClock
{
    public partial class WinForm : Form
    {
        /// <summary>
        /// 软件名称
        /// </summary>
        public string softName = "WindowsClock";
        /// <summary>
        /// 获取当前路径
        /// </summary>
        public string projectPath = System.Environment.CurrentDirectory;
        /// <summary>
        /// 获取服务器时间URL
        /// </summary>
        public string urlTime = "https://f.m.suning.com/api/ct.do";
        //鼠标按下坐标(control控件的相对坐标)
        Point mouseDownPoint = Point.Empty;
        //显示拖动效果的矩形
        Rectangle rect = Rectangle.Empty;
        //是否正在拖拽
        public bool isDrag = false;
        public WinForm()
        {
            InitializeComponent();
            // 窗口居中
            this.StartPosition = FormStartPosition.CenterScreen;
            // windows 分辨率大小
            int winWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            int winHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            // 设置WinForms窗口大小
            Size size = new Size(380, 235);
            this.Size = size;
            this.Text = softName;
            this.timerSend.Tick += new System.EventHandler(this.timerSend_Tick);
        }
        private void loadTime()
        {
            string j = HttpHelper.Get(this.urlTime);
            Object obj = JsonConvert.DeserializeObject(j);
            Newtonsoft.Json.Linq.JObject js = obj as Newtonsoft.Json.Linq.JObject;//把上面的obj转换为 Jobject对象
            string time = (Convert.ToInt64(js["currentTime"].ToString()) / 1000).ToString();
            DateTime dt = DateTimeHelper.GetTimeFromUint(time);
            this.lblTime.Text = dt.ToString("HH:mm:ss");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public Font ShowFont(float size)
        {
            Font font = null;
            System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
            string fontsPath = projectPath + @"\fonts\YournameD7HomeHalf.ttf";
            privateFonts.AddFontFile(fontsPath);
            font = new Font(privateFonts.Families[0], size);
            this.lblTime.Font = font;
            return font;
        }

        private void timerSend_Tick(object sender, EventArgs e)
        {
            ShowFont(60);
            loadTime();
        }

        private void cbShow_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)sender).CheckState.ToString() == "Checked")
            {
                // 复制
                Panel originalPanel = this.panelBox;
                Panel clonedPanel = CloneControl(originalPanel);
                isDrag = true;
            }
            else
            {
                isDrag = false;
            }
        }
        [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
        public static extern void ReleaseCapture();
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern void SendMessage(int hwnd, int wMsg, int wParam, int lParam);

        private void panelBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage((int)this.panelBox.Handle, 0xA1, 2, 0);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="controlToClone"></param>
        /// <returns></returns>
        public T CloneControl<T>(T controlToClone) where T : Control
        {
            PropertyInfo[] controlProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            T clonedControl = Activator.CreateInstance<T>();

            foreach (PropertyInfo prop in controlProperties)
            {
                if (prop.CanWrite && prop.Name != "WindowTarget")
                {
                    try { prop.SetValue(clonedControl, prop.GetValue(controlToClone, null), null); }
                    catch { }
                }
            }
            foreach (Control control in controlToClone.Controls)
            {
                // 检查是否为Label类型
                if (control is Label)
                {
                    // 创建一个新的Label实例
                    Label newLabel = new Label();
                    // 设置新Label的属性,使其看起来和原来的一样
                    newLabel.Text = control.Text;
                    newLabel.Location = control.Location;
                    // 添加到destinationPanel中
                    clonedControl.Controls.Add(newLabel);
                }
            }
            loadTime();
            return clonedControl;
        }
    }
}
复制代码

 

 

我的界面:

 

希望和最上面两张图片一样,应该是复制:Panel

目前也实现了复制,但是复制后的:Panel,时间没有动,字也很小。。。

快乐的langYa的主页 快乐的langYa | 菜鸟二级 | 园豆:280
提问于:2024-06-07 15:42
< >
分享
最佳答案
1

可以单独做个Form,Panel到不了父窗体之外

收获园豆:50
会长 | 专家六级 |园豆:12463 | 2024-06-07 17:05

嗯,最后是这样处理的。。。

快乐的langYa | 园豆:280 (菜鸟二级) | 2024-06-10 14:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册