Boblysan

Boblysan

  • NA
  • 66
  • 0

AlwaysOnTop, take 2!!!

Jan 19 2005 10:06 AM
Ok, I have created a very simple applicaation that when pressing one button should make the application always TOPMOST and pressing the other will turn that function off. Can someone please tell me why this isn't working and point me in some direction which may assist? The code I am using.. ---------------------------------------------------------------------------------------------------------- using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; namespace OnTop { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btnYes; private System.Windows.Forms.Button btnNo; private System.Windows.Forms.Label lblAlwaysOnTop; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } #region API for always on top public class Win32 { [DllImport("user32.dll", EntryPoint="SetWindowPos")] protected static extern int SetWindowPos( int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); public enum WindowPlacement { HWD_NOTOPMOST = -2, HWD_TOPMOST = -1, HWD_TOP = 0, HWD_BOTTOM = 1 } [Flags]public enum WindowPosition { SWP_NOSIZE = 0x1, SWP_NOMOVE = 0x2, SWP_SHOWWINDOW = 0x40 } public static void SetWindowPosition(System.Windows.Forms.Form form, WindowPlacement windowPlacement) { Win32.SetWindowPosition(form, windowPlacement, WindowPosition.SWP_NOMOVE | WindowPosition.SWP_NOSIZE | WindowPosition.SWP_SHOWWINDOW); } public static void SetWindowPosition(System.Windows.Forms.Form form, WindowPlacement windowPlacement, WindowPosition positionFlags) { Win32.SetWindowPos((int)form.Handle, (int)windowPlacement, 0, 0, 0, 0, (int)positionFlags); } } #endregion /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.btnYes = new System.Windows.Forms.Button(); this.btnNo = new System.Windows.Forms.Button(); this.lblAlwaysOnTop = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btnYes // this.btnYes.Location = new System.Drawing.Point(8, 8); this.btnYes.Name = "btnYes"; this.btnYes.TabIndex = 0; this.btnYes.Text = "Yes"; this.btnYes.Click += new System.EventHandler(this.btnYes_Click); // // btnNo // this.btnNo.Location = new System.Drawing.Point(200, 8); this.btnNo.Name = "btnNo"; this.btnNo.TabIndex = 1; this.btnNo.Text = "No"; this.btnNo.Click += new System.EventHandler(this.btnNo_Click); // // lblAlwaysOnTop // this.lblAlwaysOnTop.Location = new System.Drawing.Point(88, 8); this.lblAlwaysOnTop.Name = "lblAlwaysOnTop"; this.lblAlwaysOnTop.TabIndex = 2; this.lblAlwaysOnTop.Text = "Always On Top?"; this.lblAlwaysOnTop.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(280, 37); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.lblAlwaysOnTop, this.btnNo, this.btnYes}); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void btnYes_Click(object sender, System.EventArgs e) { Win32.SetWindowPosition(this, Win32.WindowPlacement.HWD_TOP); } private void btnNo_Click(object sender, System.EventArgs e) { Win32.SetWindowPosition(this, Win32.WindowPlacement.HWD_NOTOPMOST); } } }

Answers (3)