如何设置窗体顶部最多

本文关键字:窗体 顶部 设置 何设置 | 更新日期: 2023-09-27 18:15:05

我要做的就是把我的from,上面有一个图片框,设置为我电脑上最上面的对象并保持这种状态。我试过使用f.t otopmost = true;但如果当我点击一些东西,我的形式不再是最上面。我从一个dll运行我的形式(dll的代码如下)。我也试过添加一个on load甚至到我的表单,这也没有做什么。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace pic
{
public class Class1
{
    [DllImport("user32.dll", SetLastError = true)]
    private static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
    [DllImport("user32.dll")]
    static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
    public const int GWL_EXSTYLE = -20;
    public const int WS_EX_LAYERED = 0x80000;
    public const int WS_EX_TRANSPARENT = 0x20;
    public const int LWA_ALPHA = 0x2;
    public const int LWA_COLORKEY = 0x1;
    Form f = new Form();
    public void t(int LocalX, int LocalY, string PicLocal, byte transparency)
    {
        f.Load += new EventHandler(ProgramViwer_Load);
        Bitmap bitmap;
       // Form f = new Form();
        f.BackColor = Color.White;
        f.FormBorderStyle = FormBorderStyle.None;
        f.Bounds = Screen.PrimaryScreen.Bounds;
        bitmap = new Bitmap(PicLocal);
        f.Size = new Size(bitmap.Size.Width, bitmap.Size.Height);
        f.StartPosition = FormStartPosition.Manual;
        f.SetDesktopLocation(LocalX, LocalY);
        Application.EnableVisualStyles();

        PictureBox PictureBox1 = new PictureBox();
        PictureBox1.Location = new System.Drawing.Point(0, 0);
        PictureBox1.Image = bitmap;
        PictureBox1.Size = new System.Drawing.Size(bitmap.Size.Width, bitmap.Size.Height);
        PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
        f.Controls.Add(PictureBox1);
        f.AllowTransparency = true;
        SetWindowLong(f.Handle, GWL_EXSTYLE,
        (IntPtr)(GetWindowLong(f.Handle, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT));
        // set transparency to 50% (128)
        SetLayeredWindowAttributes(f.Handle, 0, transparency, LWA_ALPHA);
        Color BackColor = Color.White;
        f.TransparencyKey = BackColor;
        f.Opacity = transparency / 255f;
        Application.Run(f);
    }
    private void ProgramViwer_Load(object sender, EventArgs e)
    {
        f.TopMost = true;
    }
}
}

如何设置窗体顶部最多

你的问题是,其他窗口设置他们的'TopMost'属性为真,以及。这不是最干净的解决方案,但它应该有效。

new Thread(() =>
                {
                    try
                    {
                        while (true)
                        {
                            this.TopMost = true;
                            Thread.Sleep(1);
                        }
                    }
                    catch (Exception ex) { }
                }).Start();