定位窗口形式最底层

时间:2015-02-12 01:11:31

标签: c# winforms

我希望每次你开始我的窗体时,他都会设置在屏幕的底部(任务栏上方)

  public void goBottomWindow(Form targetForm)
  {

                targetForm.WindowState = FormWindowState.Maximized;
                targetForm.FormBorderStyle = FormBorderStyle.None;
                targetForm.TopMost = true;
                WinApi.SetWinFullScreen(targetForm.Handle);

     }
  public class WinApi
  {
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        public static extern int GetSystemMetrics(int which);

        [DllImport("user32.dll")]
        public static extern void
            SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                         int X, int Y, int width, int height, uint flags);

        private const int SM_CXSCREEN = 0;
        private const int SM_CYSCREEN = 1;
        private static IntPtr HWND_TOP = IntPtr.Zero;
        private const int SWP_SHOWWINDOW = 64; // 0x0040

        public static int ScreenX
        {
            get { return GetSystemMetrics(SM_CXSCREEN); }
        }

        public static int ScreenY
        {
            get {return 60;}
        }

        public static void SetWinFullScreen(IntPtr hwnd)
        {
            SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
        }
    }

使用此代码,它将窗口窗体保留在屏幕顶部....但我需要的是位于下方的窗体窗口。 有可能这样做吗?

对不起,我的英语非常糟糕:(

2 个答案:

答案 0 :(得分:1)

为什么targetForm.WindowState = FormWindowState.Maximized;? 是不是FormWindowState.Normal对你更好?

只需获取桌面/屏幕的尺寸

var rect = Screen.PrimaryScreen.WorkingArea;
targetForm.Witdh = rect.Width;
targetForm.Top = rect.Height - targetForm.Height;

答案 1 :(得分:0)

我在第一行看到targetForm.TopMost = true; 这意味着您的表单将 TOPMOST ,如果您希望表单 BottomMost ,则应将其更改为false;

希望这有帮助! - CCB