天天看點

C# WindForm Note-I

--------------------------------------------

★ 通過重寫視窗過程函數(WndProc)函數,截取窗體消息,如最大.最小化等

        const int WM_SYSCOMMAND = 0x112;

        const int SC_CLOSE = 0xF060;

        const int SC_MINIMIZE = 0xF020;

        const int SC_MAXIMIZE = 0xF030;

        protected override void WndProc(ref Message m)

        {

            if (m.Msg == WM_SYSCOMMAND)

            {

                if (m.WParam.ToInt32() == SC_MINIMIZE)

                {

                    MessageBox.Show("最小化!1");

                }

                if (m.WParam.ToInt32() == SC_MAXIMIZE)

                {

                    MessageBox.Show("最大化!1");

                }

                if (m.WParam.ToInt32() ==0xF120)

                {

                    MessageBox.Show("還原!1");

                }

            }

            base.WndProc(ref m);

        }

--------------------------------------------

★C#實作MDI 多文檔界面 應用程式:

主窗體Form_Main:将其IsMdiContainer設定成true,這樣MDI主窗體就建立了.

子窗體Form_Child屬性:

設定Form_Main它的MDI父窗體:Form_Child.MdiParent=this;

--------------------------------------------

★設定窗體滑鼠-形狀

this.Cursor = Cursors.屬性

★監視滑鼠-狀态

Button.MouseButtons == MouseButtons.屬性

----------------------

★設定控件的透明色

Control.Parent=ParentControl(父控件);

Control.BackColor=Color.Transparent;

----------------------

★字型設定(listViewEx為例)

//FontStyle Fs=FontStyle.Regular;GraphicsUnit Gh=GraphicsUnit.Point;

   listViewEx1.Font = new Font("宋體", 9, FontStyle.Regular,

GraphicsUnit.Point);//Font.Unit

----------------------

★合理的管理控件(釋放/建立)

control.IsDisposed  控件是否釋放

control.Created 控件是否建立并show()

判斷窗體是否已經打開:

if (client_Fm == null || client_Fm.IsDisposed)//Client_Form:窗體

            {

                client_Fm = new Client_Form();

                client_Fm.Show();

            }

            else

                client_Fm.Activate();

--------------------------------------------

★繪制UI

e.Graphics.DrawString()字

e.Graphics.DrawLines() 線

e.Graphics.DrawRectangle()方

e.Graphics.DrawImage() image

PaintEventArgs 類: 為 Paint 事件提供資料。

Rectangle 類:  繪制一個矩形。

PaintEventArgs.Graphics.DrawImage: 在指定位置并且按指定大小繪制指定的

Image 的指定部分。

PaintEventArgs..::.ClipRectangle:  擷取要在其中進行繪畫的矩形

--------------------------------------------

★Form添加背景圖檔

this.BackgroundImage = imageList1.Images[Index];

再把BackgroundImageLayout屬性改為:Stretch

這樣在界面上顯示的圖檔就不是小圖示了,是大圖檔

--------------------------------------------

★窗體全屏顯示:另外就是調用API函數

WindowState= FormWindowState.Maximized;

FormBorderStyle=FormBorderStyle.None;//此并非真正的全屏

--------------------------------------------

★擷取COMBOX指定索引  cb_Name.SelectedIndex = n;//适合用于其它datagridview等

控件

--------------------------------------------

★設定控件焦點:control.fouts();

--------------------------------------------

★設定窗體不讓使用者改變大小

把窗體的FormBorderStyle屬性設為FormBorderStyle.FixedSingle;

--------------------------------------------

★設定Form窗體的特征“

 i , StartPosition 窗體第一次出現的位置:如CenterScreen目前視窗居中,

CenterParent為父視窗中心位置。

 ii ,FormBorderStyle 訓示窗體的邊框和标題欄的外觀和行為:如None 為無标題欄格

式。

 iii,屬性Font設定顯示控件中的文本的字型;屬性CaptionFont 設定标題欄字型,

--------------------------------------------

★frm.ShowDialog()//有模式窗體

  frmAbout.Show()//無模式窗體

--------------------------------------------

★鍵盤,滑鼠事件

滑鼠事件:"MouseHover"、"MouseLeave"、"MouseEnter"、"MouseMove"、"MouseDown"

和"MouseUp"。

例--MouseDown 滑鼠按鈕按下事件

e.Button == MouseButtons.Left  左按下 

e.Button == MouseButtons.Right 右按下

鍵盤事件:"KeyDown"、"KeyUp"和"KeyPress"。

 先 聲明:  "元件名稱"."事件名稱"+= new System.EventHandler("事件函數名稱"

);

 再 定義:   事件函數····

--------------------------------------------

★Enter控件擷取焦點事件中,設定文本全選

textBox1.SelectAll();///textBox1文本全選

textBox1.SelectionStart = 1;/"1"代表光标在第1個字元之後

form.ActiveControl==control1//判斷控件control1是否被選中

--------------------------------------------

★控件(以主窗體Form為例)大小變化時,控件内的子控件安裝相應比例縮放

一般使用Anchor/Dock 就可以控制窗體位置和大小,但是這個不能實作控件随窗體縮放比例

自動調整自身大小和位置。是以隻能跟蹤Resize事件用代碼做出相應的調整。

方法代碼:

定義2個全局實型變量 float X,Y;

在窗體 load裡面初始化 X,Y

FormMain_Load(object sender, EventArgs e)

{

X = this.Width;

Y = this.Height;

}

FormMain_Resize(object sender, EventArgs e)

{

float newx = ((float)this.Width) / X;//記錄變化後窗體長和寬大小

//* 按照需要是否減去狀态欄、窗體邊框 等的長寬大小

float newy = (float)this.Height /Y;

setControls(newx, newy, this);

/*如果要過濾一些控件,可以在setControls方法中排除要過濾的方法,

*/

}

//記載控件長寬資訊

private void setTag(Control cons)

{

foreach (Control con in cons.Controls)

{

con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" +

con.Top + ":" + con.Font.Size;

if (con.Controls.Count > 0)

setTag(con);

}

}

//控件大小調整真正實作部分

private void setControls(float newx, float newy, Control cons)

{

foreach (Control con in cons.Controls)

{

if (con.Name == "panel5" || con.Name == "panel7" || con.Name ==

"pictureBox1" || con.Name == "buttonX3")

{

continue;

}

string[] mytag = con.Tag.ToString().Split(new char[] { ':' });

float a = Convert.ToSingle(mytag[0]) * newx;

con.Width = (int)a;

a = Convert.ToSingle(mytag[1]) * newy;

con.Height = (int)(a);

a = Convert.ToSingle(mytag[2]) * newx;

con.Left = (int)(a);

a = Convert.ToSingle(mytag[3]) * newy;

con.Top = (int)(a);

Single currentSize = Convert.ToSingle(mytag[4]) * newy;

GraphicsUnit f = con.Font.Unit;

con.Font = new Font(con.Font.Name, currentSize,

con.Font.Style, con.Font.Unit);

if (con.Controls.Count > 0)

{

setControls(newx, newy, con);

}

}

}

--------------------------------------------

★  windows 開始按鈕效果的 Button

方法一:

ToolStrip Test = new ToolStrip();

Test.Dock = DockStyle.None;

//放置位置(注意位置不要超過其父控件範圍)

// Test.Left = 150;

// Test.Top = 100;

Test.Parent = panel1;//設定他父控件

ToolStripDropDownButton TestButton = new ToolStripDropDownButton("Test");

//設定TestButton的事件

。。。。。。

TestButton.MouseEnter +=..._MouseEnter;

TestButton.MouseLeave += ..._MouseLeave;

TestButton.DisplayStyle = ToolStripItemDisplayStyle.Text;

//給下拉子項添加事件

TestButton.DropDown.Items[0].MouseDown += Too_MouseEnter;

Test.Items.Add(TestButton);

方法二:contextMenuStrip1

private void buttonX_Click(object sender, EventArgs e)

{

int lf = 0;

int tp = 0;

//control

Control cont = buttonX4;

while (cont.Parent != this) //如果buttonX被嵌套在多層容器裡

{

lf += cont.Left;

tp += cont.Top;

cont = cont.Parent;

}

lf += this.Left;

tp += this.Top - buttonX4.Height;

contextMenuStrip1.Show(lf, tp);

}

--------------------------------------------

★将Form添加到主窗體進行顯示

不能直接把frm.Parent =父容器,會報出無法将頂級控件添加到控件的錯誤,解決方案:

因為Form預設是頂級窗體,是以加不上去,可以設定其TopLevel屬性為False,代碼如下:

frmMain frm = new frmMain();

frm.FormBorderStyle = FormBorderStyle.None;

frm.TopLevel = false;

frm.Parent = panel1;

frm.Show();

--------------------------------------------

--------------------------------------------

--------------------------------------------

--------------------------------------------

--------------------------------------------

繼續閱讀