C# WinForm主畫面基本程式

直接看Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
 
        /// <summary>
        /// 應用程式的主進入點。
        /// </summary>
        [STAThread]
        
static void Main()
        {
            CustomExceptionHandler eh = 
new CustomExceptionHandler();
            Application.ThreadException += 
new System.Threading.ThreadExceptionEventHandler(eh.OnThreadException);

            Process instance = RunningInstance();
            
if (instance == null)
            {
                Application.EnableVisualStyles();
                Application.DoEvents();
                Application.Run(
new frmMain());
            }
            
else
            {
                MessageBox.Show(
"目前已有啟動!  ""請確認", MessageBoxButtons.OK, MessageBoxIcon.Information);
                HandleRunningInstance(instance);
                Application.Exit();
            }
        }

        
internal class CustomExceptionHandler
        {
            
public void OnThreadException(object sender, System.Threading.ThreadExceptionEventArgs t)
            {
                MessageBox.Show(
"無法預期的錯誤發生,錯誤訊息如下: \n \n" + t.Exception.Message + t.Exception.StackTrace);
                Application.Exit();
            }
        }
        
        
#region 檢查是否已啟動程式
        
private static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);

            
//Loop through  the running processes in with the same name      
            foreach (Process process in processes)
            {
                
//Ignore   the   current   process     
                if (process.Id != current.Id)
                {
                    
//Make sure that the process is running from the exe file.      
                    if (Assembly.GetExecutingAssembly().Location.Replace("/""\\") == current.MainModule.FileName)
                    {
                        
//Return   the   other   process   instance.     
                        return process;
                    }
                }
            }
            
//No other instance was found, return null.    
            return null;
        }

        
private static void HandleRunningInstance(Process instance)
        {
            
//Make sure the window is not minimized or maximized    
            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
            
//Set the real intance to foreground window   
            SetForegroundWindow(instance.MainWindowHandle);
        }

        [DllImport(
"User32.dll")]
        
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        [DllImport(
"User32.dll")]
        
private static extern bool SetForegroundWindow(IntPtr hWnd);
        
//1:normal   2:minimized   3:maximized   
        private const int WS_SHOWNORMAL = 3;
        
#endregion
        
        
private void frmMain_Load(object sender, System.EventArgs e)
        {
            
//登入的子視窗
            m_loginFrm = (Form)LoginUIFactory.getLoginInstance(APP_NAME);
            m_loginFrm.ShowDialog();

            
if (m_loginFrm.Tag == null || m_loginFrm.Tag.ToString().IndexOf(":") == -1)
            {
                Application.Exit();
                
return;
            }
            userID = m_loginFrm.Tag.ToString().Split(
":".ToCharArray()[0])[1];
            tslLoginID.Text = 
"登入身份:" + userID;
            
this.toolStripStatusLabel1.Text = "就緒";
        }
        
        
private void frmMain_MdiChildActivate(object sender, EventArgs e)
        {
            
if (this.ActiveMdiChild != null)
                
this.toolStripStatusLabel1.Text = this.ActiveMdiChild.Text;
            
else
                
this.toolStripStatusLabel1.Text = "就緒";
        }
        
        
private void MenuItem1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
if (checkOpenSubWin("MenuItem1subForm") == true)
                
return;

            MenuItem1subForm frm = 
new MenuItem1subForm();
            frm.MdiParent = 
this;
            frm.Show();
            
this.toolStripStatusLabel1.Text = frm.Text;  
        }
        
        
#region 視窗基本功能
        
private void 重疊顯示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
this.LayoutMdi(System.Windows.Forms.MdiLayout.Cascade);
        }

        
private void 水平並排ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
this.LayoutMdi(System.Windows.Forms.MdiLayout.TileHorizontal);
        }

        
private void 垂直並排ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
this.LayoutMdi(System.Windows.Forms.MdiLayout.TileVertical);
        }

        
private void 全部最大化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form[] charr = 
this.MdiChildren;
            
foreach (Form chform in charr)
            {
                
if (chform.MaximizeBox)
                    chform.WindowState = FormWindowState.Maximized;
                
else
                    chform.WindowState = FormWindowState.Normal;
            }
        }

        
private void 全部最小化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form[] charr = 
this.MdiChildren;
            
foreach (Form chform in charr)
                chform.WindowState = FormWindowState.Minimized;
        }

        
private void 全部關閉ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form[] charr = 
this.MdiChildren;

            
foreach (Form chform in charr)
                chform.Close();
        }

        
private void myItem_Click(object sender, EventArgs e)
        {
            
this.checkOpenSubWin(((ToolStripMenuItem)sender).Tag.ToString());
        }

        
private void menuStrip1_MenuActivate(object sender, EventArgs e)
        {
            
// 讓視窗選單列出開啟中的子視窗,可在此切換點選已開啟的子視窗
            if (this.視窗ToolStripMenuItem.DropDownItems.Count > 10)
            {
                
for (int i = this.視窗ToolStripMenuItem.DropDownItems.Count - 1; i > 9; i--)
                    
this.視窗ToolStripMenuItem.DropDownItems.RemoveAt(i);
            }

            
if (this.MdiChildren.Length > 0)
                
this.視窗ToolStripMenuItem.DropDownItems.Add(new ToolStripSeparator());

            
foreach (Form frm in this.MdiChildren)
            {
                ToolStripMenuItem myItem = 
new ToolStripMenuItem(frm.Text);
                myItem.Tag = frm.Name;
                myItem.Click += 
new EventHandler(myItem_Click);
                
if (this.ActiveMdiChild.Name == frm.Name)
                    myItem.CheckState = CheckState.Checked;

                
this.視窗ToolStripMenuItem.DropDownItems.Add(myItem);
            }
        }

        
public bool checkOpenSubWin(string chkName)
        {
            
foreach (Form frm in this.MdiChildren)
            {
                
if (frm.Name == chkName)
                {
                    frm.Activate();
                    
if (frm.MaximizeBox)
                        frm.WindowState = FormWindowState.Maximized;
                    
else
                        frm.WindowState = FormWindowState.Normal;
                    
return true;
                }
            }
            
return false;
        }

        
public void CloseSubWin(string chkName)
        {
            
foreach (Form frm in this.MdiChildren)
            {
                
if (frm.Name == chkName)
                {       
                    frm.Close();
                    
return;
                }
            }
        }

        
private void 關於ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
new About().ShowDialog();
        }

        
private void 離開ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form[] charr = 
this.MdiChildren;

            
foreach (Form chform in charr)
                chform.Close();

            Application.Exit();
        }
        
#endregion
posted @ 2013-06-07 11:36  Jimmych  阅读(973)  评论(0)    收藏  举报