Link to home
Start Free TrialLog in
Avatar of EEssam
EEssam

asked on

An issue with TopMost property

Hi,

I have the TopMost property set to true for a form, and it works... but I don't want the form to be "TopMost" when moved over the taskbar. I want it TopMost everything, except when touching the taskbar it should act as normal form and have the part that is touching the taskbar go behind it.

Please help. Thanks.
SOLUTION
Avatar of Agarici
Agarici
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EEssam
EEssam

ASKER

Mishu007,

The code is broken. Please test it.
Try this:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
 
public static void CheckAutFocus(IntPtr hWnd)
{
     if (GetForegroundWindow() != hWnd)
     {
         SetForegroundWindow(hWnd);
     }
}
// You can add this on the form shown event

Open in new window

Avatar of EEssam

ASKER

How can I activate the code in Form1_Load for example?

Thanks.
You just simply add the method call : CheckAutFocus(this.Handle);

Regards,
Mishu
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EEssam

ASKER

The code attached is not doing the trick. Please check it.

Agarici,

Thanks a lot for the magnet effect. I've been looking for it for longtime!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            CheckAutFocus(this.Handle);
        }
 
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();
 
        public static void CheckAutFocus(IntPtr hWnd)
        {
            if (GetForegroundWindow() != hWnd)
            {
                SetForegroundWindow(hWnd);
            }
        }
 
    }
}

Open in new window