简体   繁体   中英

How to set a form to top most

What I am trying to do is just set my from, that has a picture box on it, to be the top most object on my computer and stay that way. I have tried to use f.topmost = true; but if when I click on something my form is no longer top most. I am running my form from a dll (code of dll is below). I have also tried to add in a on load even to my form, which also did nothing.

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

}
}

Your problem is that other windows set their 'TopMost' property to true as well. It's not the cleanest solution but it should work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM