0
点赞
收藏
分享

微信扫一扫

MongoDB文档的详细使用说明

洒在心头的阳光 2024-10-17 阅读 16

前文解释了思路,这里直接贴一下代码。 

c#透明悬浮球实现 从零开始用C#写一个桌面应用程序(三)_c# 悬浮窗-CSDN博客 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace testxuanfuqiu
{
    public partial class BallForm : Form
    {
        int dheight = 136;
        int dwidth = 136;
        private ball functionForm;

        public BallForm()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;

            this.TransparencyKey = this.BackColor;
            this.Paint += new PaintEventHandler(this.BallForm_Paint);
            functionForm = new ball();

            this.ClientSize = new Size(136, 136);

          


            this.MouseDown += new MouseEventHandler(BallForm_MouseDown);
            this.MouseMove += new MouseEventHandler(BallForm_MouseMove);
            this.MouseUp += new MouseEventHandler(BallForm_MouseUp);

            this.LocationChanged += new EventHandler(BallForm_LocationChanged);

            AddMouseHandlers(this);
        }

        private void BallForm_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            e.Graphics.FillEllipse(new SolidBrush(Color.Yellow), 0, 0, dwidth, dheight);
        }

        private void AddMouseHandlers(Control control)
        {
            control.MouseDown += new MouseEventHandler(BallForm_MouseDown);
            control.MouseMove += new MouseEventHandler(BallForm_MouseMove);
            control.MouseUp += new MouseEventHandler(BallForm_MouseUp);
            
            // 递归地为所有子控件添加事件处理器
            foreach (Control childControl in control.Controls)
            {
                AddMouseHandlers(childControl);
            }
        }


        private bool dragging = false;
        private bool isclick = false;
        private Point dragCursorPoint;
        private Point dragFormPoint;
        private const int DragThreshold = 5; // 你可以根据需要调整这个值


        private void BallForm_MouseDown(object sender, MouseEventArgs e)
        {
            isclick = true;
            dragging = true;
            dragCursorPoint = Cursor.Position;
            dragFormPoint = this.Location;
        }

        private void BallForm_MouseMove(object sender, MouseEventArgs e)
        {
            Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));

            // 检查鼠标是否移动了一定的距离
            if (Math.Abs(dif.X) > DragThreshold || Math.Abs(dif.Y) > DragThreshold)
            {
                
                isclick = false;
            }

            if (dragging)
            {
                
                Point newLocation = Point.Add(dragFormPoint, new Size(dif));

                // 检查新的位置是否会使窗体超出屏幕边界,如果会,那么就调整新的位置
                if (newLocation.X < 0)
                {
                    newLocation.X = 0;
                }
                else if (newLocation.X + this.Width > Screen.PrimaryScreen.WorkingArea.Width)
                {
                    newLocation.X = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
                }

                if (newLocation.Y < 0)
                {
                    newLocation.Y = 0;
                }
                else if (newLocation.Y + this.Height > Screen.PrimaryScreen.WorkingArea.Height)
                {
                    newLocation.Y = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
                }

                this.Location = newLocation;
            }
        }

        private void BallForm_MouseUp(object sender, MouseEventArgs e)
        {
            dragging = false;

            // 检查窗体是否接近屏幕边缘,如果接近,那么就将窗体的位置设置为屏幕边缘
            if (this.Left <= StickGap) // 窗体靠近屏幕的左边缘
            {
                this.Left = 0;
            }
            else if (this.Right >= Screen.PrimaryScreen.WorkingArea.Width - StickGap) // 窗体靠近屏幕的右边缘
            {
                this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width/2;
            }
            else if (this.Top <= StickGap) // 窗体靠近屏幕的上边缘
            {
                this.Top = 0;
            }
            else if (this.Bottom >= Screen.PrimaryScreen.WorkingArea.Height - StickGap) // 窗体靠近屏幕的下边缘
            {
                this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
            }
        }


        private const int StickGap = 20; // 窗体与屏幕边缘的距离,你可以根据需要调整这个值
        private bool isSticking = false; // 窗体是否正在靠边粘连

     

        private void BallForm_LocationChanged(object sender, EventArgs e)
        {
            if (isSticking) return; // 如果窗体正在靠边粘连,那么我们不需要再次检查其位置

            if (this.Left <= StickGap) // 窗体靠近屏幕的左边缘
            {
                isSticking = true;
                this.Left = -this.Width / 2;
            }
            else if (this.Right >= Screen.PrimaryScreen.Bounds.Width - StickGap) // 窗体靠近屏幕的右边缘
            {
                isSticking = true;
                this.Left = Screen.PrimaryScreen.Bounds.Width - this.Width / 2;
            }
            else if (this.Top <= StickGap) // 窗体靠近屏幕的上边缘
            {
                isSticking = true;
                this.Top = -this.Height / 2;
            }
            else if (this.Bottom >= Screen.PrimaryScreen.Bounds.Height - StickGap) // 窗体靠近屏幕的下边缘
            {
                isSticking = true;
                this.Top = Screen.PrimaryScreen.Bounds.Height - this.Height / 2;
            }
        }


        private void BallForm_MouseEnter(object sender, EventArgs e)
        {
            functionForm.Location = new Point(this.Location.X, this.Location.Y + this.Height);

            functionForm.Show();
        }

        private void BallForm_MouseLeave(object sender, EventArgs e)
        {
            functionForm.Hide();
        }

        private void BallForm_Load(object sender, EventArgs e)
        {
            lb_yizhu.Font = new Font(lb_yizhu.Font.FontFamily, 13);
            lb_yizhu.TextAlign = ContentAlignment.MiddleCenter;
            
            lb_yizhu.Width = 136;  // 设置为你需要的宽度
            lb_yizhu.Height = 136;  // 设置为你需要的高度
            
            lb_yizhu.BackColor = BallForm.DefaultBackColor;
            lb_yizhu.BackColor = this.BackColor;
            
            //lb_yizhu.Location = new Point(50,37);
            lb_yizhu.BackColor = Color.Transparent;
            //lb_yizhu.Location = new Point(35, 35);
        }
       


        private void BallForm_MouseClick(object sender, MouseEventArgs e)
        {
            // 如果不是点击操作,那么就不执行点击操作的代码
            if (isclick==false)
            {
                return;
            }

            // 检查是哪个鼠标按钮被点击
            if (e.Button == MouseButtons.Left)
            {
                // 如果 functionForm 窗体当前是显示状态,那么就隐藏它
                // 如果 functionForm 窗体当前是隐藏状态,那么就显示它
                if (functionForm.Visible)
                {
                    functionForm.Hide();
                }
                else
                {
                    // 计算 functionForm 窗体的位置
                    int x = this.Location.X + this.Width;
                    int y = this.Location.Y;

                    // 检查 functionForm 窗体是否会被屏幕右边缘遮挡,如果会,那么就调整它的 X 坐标
                    if (x + functionForm.Width > Screen.PrimaryScreen.WorkingArea.Width)
                    {
                        x = Screen.PrimaryScreen.WorkingArea.Width - functionForm.Width;
                    }

                    // 检查 functionForm 窗体是否会被屏幕下边缘遮挡,如果会,那么就调整它的 Y 坐标
                    if (y + functionForm.Height > Screen.PrimaryScreen.WorkingArea.Height)
                    {
                        y = Screen.PrimaryScreen.WorkingArea.Height - functionForm.Height;
                    }

                    // 设置 functionForm 窗体的位置并显示它
                    functionForm.Location = new Point(x, y);
                    functionForm.Show();
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                // 右键被点击
                // 在这里添加你的代码
            }
        }

        private void BallForm_Click(object sender, EventArgs e)
        {

        }
    }
}
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;

namespace testxuanfuqiu
{
    public partial class copyboardlist : Form
    {
        public copyboardlist()
        {
            InitializeComponent();
        }
        List<DataObject> history;

        private List<string> clipboardContents = new List<string>();

        public List<string> GetClipboardContents()
        {
            return new List<string>(clipboardContents);
        }

        private void btngetlist_Click(object sender, EventArgs e)
        {
            //string clipboardText = Clipboard.GetText();
            //if (!string.IsNullOrEmpty(clipboardText))
            //{
            //    clipboardContents.Add(clipboardText);
            //    cplist.Items.Add(clipboardText);
            //}

            
            foreach (DataObject data in history)
            {
                ListViewItem item = new ListViewItem();

                if (data.GetDataPresent(DataFormats.Text))
                {
                    string text = (string)data.GetData(DataFormats.Text);
                    item.Text = text;
                }
                else if (data.GetDataPresent(DataFormats.Bitmap))
                {
                    Bitmap bitmap = (Bitmap)data.GetData(DataFormats.Bitmap);
                    // 添加 bitmap 到 ImageList
                    cpimgls.Images.Add(bitmap);
                    item.ImageIndex = cpimgls.Images.Count - 1; // 设置图片索引
                }
                // ...处理其他类型的数据...

                cplist.Items.Add(item);
            }

        }

        private void copyboardlist_Load(object sender, EventArgs e)
        {
            ClipboardWatcher watcher = new ClipboardWatcher();
            history = watcher.GetClipboardHistory();
        }
    }
}
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Collections.Generic;

public class ClipboardWatcher : NativeWindow
{
    private const int WM_CLIPBOARDUPDATE = 0x031D;
    private List<DataObject> clipboardHistory = new List<DataObject>();

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool AddClipboardFormatListener(IntPtr hwnd);

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool RemoveClipboardFormatListener(IntPtr hwnd);

    public ClipboardWatcher()
    {
        CreateHandle(new CreateParams());
        AddClipboardFormatListener(Handle);
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_CLIPBOARDUPDATE)
        {
            try
            {
                DataObject clipboardData = Clipboard.GetDataObject() as DataObject;
                if (clipboardData != null)
                {
                    clipboardHistory.Add(clipboardData);
                }
            }
            catch (Exception ex)
            {
                // 处理错误
                Console.WriteLine("An error occurred while accessing the clipboard: " + ex.Message);
            }
        }

        base.WndProc(ref m);
    }

    public void Stop()
    {
        RemoveClipboardFormatListener(Handle);
    }
    public List<DataObject> GetClipboardHistory()
    {
        return clipboardHistory;
    }
}
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;

namespace testxuanfuqiu
{
    public partial class ball : Form
    {
        public ball()
        {
            InitializeComponent();
        }
        private copyboardlist copyboardlist = new copyboardlist();
        private void btncopyboard_Click(object sender, EventArgs e)
        {
            if (copyboardlist == null || copyboardlist.IsDisposed)
            {
                // 创建新的 copyboardlist 窗体
                copyboardlist = new copyboardlist();

                // 计算 copyboardlist 窗体的位置
                int x = Screen.PrimaryScreen.WorkingArea.Width - copyboardlist.Width;
                int y = 0;

                // 设置 copyboardlist 窗体的位置并显示它
                copyboardlist.Location = new Point(x, y);
                copyboardlist.Show();
            }
            else
            {
                // 如果 copyboardlist 窗体已经存在,那么就切换它的显示状态
                if (copyboardlist.Visible)
                {
                    copyboardlist.Hide();
                }
                else
                {
                    copyboardlist.Show();
                }
            }
        }
    }
}
举报

相关推荐

0 条评论