0

I want to make a small software which can provide the windows running applications to "always on top functionality" as available in Linux terminal, VLC media player etc. I have found some application related to this on internet. but i want to create my own always on top utility.

It will be better if u can suggest c# .net code. and IDE: i will prefer Visual Studio

My goal is to make the application as shown here:

http://www.pcworld.com/article/218511/Windows.html

yogeshkmrsoni002
  • 379
  • 3
  • 6
  • 11
  • Depending upon your platform (winforms or wpf) it's either checking the property or including the attribute in your xaml. – Gayot Fow Jan 31 '14 at 15:03

4 Answers4

4

For making it generic to any process, you have to overload two methods of the User32.dll which is a part of Win32 API.

Just use the code given below and specify your process name without its extension, say for vlc - specify

processName = "vlc"; and NOT LIKE "vlc.exe"

using System.Runtime.InteropServices;
using System.Diagnostics;

public class ProcessManager
{
    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, uint windowStyle);

    [DllImport("user32.dll")]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

    ProcessManager()
    {
      string processName = "vlc"; /* Your process name here */
      SearchProcessAndModifyState(processName);
    }

    void SearchProcessAndModifyState(string targetProcessName)
    {
        Process specifiedProcess = null;
        Process[] processes = Process.GetProcesses();
        for (int i = 0; i < processes.Length; i++)
        {
            Process process = processes[i];
            if (process.ProcessName == targetProcessName)
            {
                specifiedProcess = process;
                break;
            }
        }
        if (specifiedProcess != null)
        {
          ProcessManager.ShowWindow(specifiedProcess.MainWindowHandle, 1u);
          ProcessManager.SetWindowPos(specifiedProcess.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, 3u);
        }
    }
}
Mayur Dhingra
  • 1,527
  • 10
  • 27
  • Seems nice, I'll try. thanx – yogeshkmrsoni002 Jan 31 '14 at 10:24
  • I've tried adding this class beneath my "public partial class Form1 : Form". I added the missing semicolon, added the Interop using statement, and the diagnostics using statement to rid of the errors. Can you please tell me the proper way to implement this functionality so that my program will run above all other programs? – DavidG Jul 20 '17 at 06:37
3

Try to add this in your code, preferably on load

myTopForm.TopMost = true;
Dieter B
  • 1,142
  • 11
  • 20
  • But i want to make the utility which can provide this always on top service to other software. see this article : http://www.pcworld.com/article/218511/Windows.html – yogeshkmrsoni002 Jan 31 '14 at 08:55
  • 1
    Then I suggest that you take a look to [SetWindowsPos function](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545%28v=vs.85%29.aspx) – Dieter B Jan 31 '14 at 09:05
  • Please go through the question again and explain that since we are not programming VLC media player. So how can we set VLC to be always on top. – Mayur Dhingra Jan 31 '14 at 09:19
0

Try to use this.TopMost = true; on your initialization.

Anyway it could be good if your read this.

Community
  • 1
  • 1
Saleh Parsa
  • 1,415
  • 13
  • 22
  • Please go through the question again and explain that since we are not programming VLC media player. So how can we set VLC to be always on top. – Mayur Dhingra Jan 31 '14 at 09:21
  • You should read the question more I think. "I want to make a small software which can provide the windows running applications to "always on top functionality" as available in Linux terminal, VLC media player etc" VLC is an example there – Saleh Parsa Jan 31 '14 at 09:23
  • Get a break man. I have read it well. Please go to the link and download the software that the author has provided and you will come to know. – Mayur Dhingra Jan 31 '14 at 09:28
0

Go to the Form Property and set TopMost property to 'True' like this

enter image description here

You can also set the value of the TopMost property of Form on code behind:

public Form1() { this.TopMost=true; }

Or on load
private void Form1_Load(object sender, EventArgs e) { this.TopMost=true; }

Ravi Verma
  • 182
  • 1
  • 3
  • 12
  • Please go through the question again and explain that since we are not programming VLC media player. So how can we set VLC to be always on top. – Mayur Dhingra Jan 31 '14 at 09:19
  • As per this question, this question is a part of Visual Studio 2010 tag. And in the context of VS you can set the form position only according to this type. Actually this question is mixing two different-different things. – Ravi Verma Jan 31 '14 at 09:31
  • Yes, I agree, but please go to the link and download the application which the author has given in the question. – Mayur Dhingra Jan 31 '14 at 09:49