Solved

An issue with TopMost property

Posted on 2007-11-23
8
531 Views
Last Modified: 2013-12-16
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.
0
Comment
Question by:EEssam
  • 3
  • 3
  • 2
8 Comments
 
LVL 11

Assisted Solution

by:Agarici
Agarici earned 400 total points
ID: 20340001
i dont think you can do that. not using topmost property.
0
 
LVL 11

Assisted Solution

by:Mihai Stancescu
Mihai Stancescu earned 100 total points
ID: 20365481
Try instead of TopMost = true SetForegroundWindow API method.

This link will show you how to declare it, and how to use it.
http://pinvoke.net/default.aspx/user32/SetForegroundWindow.html

Hope it helps!

Regards,
Mishu
0
 

Author Comment

by:EEssam
ID: 20367089
Mishu007,

The code is broken. Please test it.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 20367516
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

0
 

Author Comment

by:EEssam
ID: 20376944
How can I activate the code in Form1_Load for example?

Thanks.
0
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 20380194
You just simply add the method call : CheckAutFocus(this.Handle);

Regards,
Mishu
0
 
LVL 11

Accepted Solution

by:
Agarici earned 400 total points
ID: 20380616
unfortunatelly this will not be the same as topmost property
it will only be in effect until the user clicks on an other window or does alt-tab

i think what you can do is the folowing:
overwrite the WndProc method, and do some trick when you receive WM_WINDOWPOSCHANGING
http://msdn2.microsoft.com/en-us/library/ms632653.aspx
maybe this can help:
http://www.codeproject.com/vb/net/SnapForm.asp?df=100&forumid=335272&exp=0&select=2310934

hth,
A
0
 

Author Comment

by:EEssam
ID: 20389577
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

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question