Sunday, September 5, 2010

Always on Top Window Form in C#

//This is the application in which we create a form  which is always shows top most window on the all opened windows in the Window O/S. So if u want to show a form always on top then follow this code given below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace AlwaysOnTopWindowApplication
    public partial class Form1 : Form
        //Write the below given code inside the Form class
        //using System.Runtime.InteropServices with this project;

        static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
        static readonly IntPtr HWND_TOP = new IntPtr(0);
        static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
        const UInt32 SWP_NOSIZE = 0x0001;
        const UInt32 SWP_NOMOVE = 0x0002;

        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        // apply this code in the constructor of the form
        public Form1()
            SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

No comments:

Post a Comment