winform視窗只允許最大化最小化 並隨解析度大小而改變

來源:互聯網
上載者:User

遇到一個需求就是 一個視窗只允許最大化和最小化 並且視窗的下面的狀態列不能被遮住

      開始以為很簡單的設定幾個屬性就好了.結果弄了好幾個小時才解決這個問題.

首先是 不運行改變視窗大小的問題  需要屏蔽最大化和還原的按鈕  只允許最大化和關閉.

通過設定程式已最大化啟動  並在Form_Load中擷取表單大小 並設定最大值和最小值來實現不允許改變表單大小

發現沒用. 試了好幾種方式都會出現視窗最下面的狀態列被windows功能表列遮住, 最後採用屏蔽表單訊息來實現這個功能

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.ComponentModel;
   4:  using System.Data;
   5:  using System.Drawing;
   6:   
   7:  using System.Text;
   8:  using System.Windows.Forms;
   9:   
  10:  namespace WinFormDemo
  11:  {
  12:      public partial class Form1 : Form
  13:      {
  14:          public Form1()
  15:          {
  16:              InitializeComponent();
  17:          } 
  18:   
  19:          private void Form1_Load(object sender, EventArgs e)
  20:          { 
  21:              this.MaximizeBox = false;
  22:              IsLoad = true;
  23:          }
  24:   
  25:          public bool IsLoad { get; set; }
  26:       
  27:   
  28:          public const int WM_SYSCOMMAND = 0x112;
  29:   
  30:          public const int SC_MINIMIZE = 0xF020;
  31:          public const int SC_CLOSE = 0xF060; 
  32:          public const int SC_RESTORE = 0xF120;
  33:   
  34:          protected override void WndProc(ref   Message m)
  35:          {
  36:              if (m.Msg == WM_SYSCOMMAND)
  37:              {
  38:   
  39:                  int wParam = (int)m.WParam;
  40:                  //攔截 除最小化 恢複 關閉 以外的訊息
  41:                  if (wParam != SC_MINIMIZE && wParam != SC_CLOSE && wParam != SC_RESTORE)
  42:                  {
  43:                      return;
  44:                  } 
  45:              } 
  46:              base.WndProc(ref   m);
  47:          }
  48:   
  49:          
  50:   
  51:          private void Form1_SizeChanged(object sender, EventArgs e)
  52:          {
  53:   
  54:                
  55:              if (WindowState == FormWindowState.Maximized)
  56:              {
  57:                  if (IsLoad)
  58:                  {
  59:                      //最大化 時禁用 最大化按鈕
  60:                      this.MaximizeBox = false;
  61:                  }
  62:                  
  63:              }
  64:              else if(WindowState == FormWindowState.Minimized)
  65:              {
  66:                  // 最小化時啟用 最大化按鈕 
  67:                  //最大化 禁用時 恢複原始大小 不會最大化
  68:                  this.MaximizeBox = true;
  69:              }
  70:          }
  71:   
  72:          
  73:         
  74:      }
  75:  }

 

具體的訊息類型的值 http://msdn.microsoft.com/en-us/library/ms646360(v=vs.85).aspx  msdn有具體說明

通過屏蔽視窗功能表列 最小化,恢複,關閉 以外的訊息來禁用實現無法改變表單大小的功能

感覺應該有別的方法解決這個問題, 不知道大家有沒遇到這種需求.  如果有好的解決方案 請告訴我.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.