Develop and Download Open Source Software

Browse Subversion Repository

Contents of /TransparentizeWindow/MainForm.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (show annotations) (download)
Tue Apr 16 05:19:23 2013 UTC (10 years, 11 months ago) by yuuan
File size: 7350 byte(s)
- 設定が変更できないバグを修正
- 著作権表示のリンク先を変更
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Drawing;
7 using System.Linq;
8 using System.Text;
9 using System.Windows.Forms;
10 using System.Diagnostics;
11 using System.Xml.Serialization;
12 using System.IO;
13
14 namespace TransparentizePutty
15 {
16 public partial class MainForm : Form
17 {
18 private class ProcessInfo
19 {
20 public bool transparent = false;
21 public IntPtr hWnd = IntPtr.Zero;
22 public string name;
23 public string title;
24 public bool exists = true;
25
26 public ProcessInfo(IntPtr hWnd, string name, string title) {
27 this.hWnd = hWnd;
28 this.name = name;
29 this.title = title;
30 }
31
32 public ProcessInfo(Process process) {
33 Update(process);
34 }
35
36 public void Update(Process process) {
37 this.hWnd = process.MainWindowHandle;
38 this.name = process.ProcessName;
39 this.title = process.MainWindowTitle;
40 this.exists = true;
41 }
42 }
43
44 private List<ProcessInfo> processList = new List<ProcessInfo>();
45 private ApplicationSetting setting = new ApplicationSetting();
46
47 private void SaveSetting() {
48 try {
49 XmlSerializer sirializer = new XmlSerializer(typeof(ApplicationSetting));
50 FileStream stream = new FileStream("TraPutty.xml", FileMode.Create);
51 sirializer.Serialize(stream, this.setting);
52 stream.Close();
53 }
54 catch { }
55 }
56
57 private void LoadSettings() {
58 try {
59 XmlSerializer serializer = new XmlSerializer(typeof(ApplicationSetting));
60 FileStream stream = new FileStream("TraPutty.xml", FileMode.Open);
61 this.setting = (ApplicationSetting)serializer.Deserialize(stream);
62 }
63 catch {
64 this.setting = new ApplicationSetting();
65 }
66 }
67
68 private void ResetProcessesInfo() {
69 for (int i = 0; i < processList.Count; i++) {
70 processList[i].exists = false;
71 }
72
73 Process[] processes = Process.GetProcesses();
74 foreach (Process process in processes) {
75 if (process.MainWindowHandle != IntPtr.Zero) {
76 bool ng = false;
77 foreach (string ngword in this.setting.ngwords) {
78 if (process.ProcessName.Equals(ngword, StringComparison.CurrentCultureIgnoreCase)) ng = true;
79 }
80 if (!ng) {
81 bool exists = false;
82 for (int i = 0; i < processList.Count; i++) {
83 ProcessInfo processInfo = processList[i];
84 if (processInfo.hWnd == process.MainWindowHandle) {
85 int exStyle = Win32API.GetWindowLong(processInfo.hWnd, Win32API.GWL_EXSTYLE);
86 if ((exStyle & (int)Win32API.WS_EX.LAYERED) != 0) {
87 byte alpha;
88 Win32API.GetLayeredWindowAttributes(processInfo.hWnd, 0, out alpha, 0);
89 processInfo.transparent = (alpha != 255) ? true : false;
90 }
91 processInfo.Update(process);
92 exists = true;
93 break;
94 }
95 }
96 if (!exists) {
97 ProcessInfo pi = new ProcessInfo(process);
98 this.processList.Add(pi);
99
100 if (this.setting.puttysAreTransparent) {
101 SetTransparentForPutty(pi, true);
102 }
103 }
104 }
105 }
106 }
107
108 processList.Sort(delegate(ProcessInfo x, ProcessInfo y) {
109 return x.name.CompareTo(y.name);
110 });
111
112 for (int i = 0; i < processList.Count; i++ ) {
113 if (!processList[i].exists) {
114 processList.Remove(processList[i]);
115 }
116 }
117 }
118
119 private bool SetTransparent(IntPtr hWnd, bool transparent) {
120 if (transparent) {
121 int exStyle = Win32API.GetWindowLong(hWnd, Win32API.GWL_EXSTYLE);
122 Win32API.SetWindowLong(hWnd, Win32API.GWL_EXSTYLE, exStyle | (int)Win32API.WS_EX.LAYERED);
123
124 return Win32API.SetLayeredWindowAttributes(hWnd, 0, this.setting.alphaValue, Win32API.LWA_ALPHA);
125 }
126 else {
127 return Win32API.SetLayeredWindowAttributes(hWnd, 0, 255, Win32API.LWA_ALPHA);
128 }
129 }
130
131 private void SetTransparentForAllPutty() {
132 this.setting.puttysAreTransparent = !this.setting.puttysAreTransparent;
133 foreach (ProcessInfo process in processList) {
134 this.SetTransparentForPutty(process, this.setting.puttysAreTransparent);
135 }
136 }
137
138 private void SetTransparentForPutty(ProcessInfo process, bool transparent) {
139 if (process.name.Equals("putty", StringComparison.CurrentCultureIgnoreCase)) {
140 if (this.setting.notTransparentPuttySetting && IsSetting(process))
141 transparent = false;
142 SetTransparent(process.hWnd, transparent);
143 }
144 }
145
146 private bool IsSetting(ProcessInfo process) {
147 if (process.title.Equals("PuTTY 設定"))
148 return true;
149 if (process.title.Equals("PuTTY Configuration"))
150 return true;
151 return false;
152 }
153
154 public MainForm() {
155 InitializeComponent();
156 this.Enabled = false;
157 }
158
159 private void MainForm_Load(object sender, EventArgs e) {
160 LoadSettings();
161 ResetProcessesInfo();
162 timer.Enabled = true;
163 }
164
165 private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
166 SaveSetting();
167 }
168
169 private void contextMenuStrip_Opening(object sender, CancelEventArgs e) {
170 this.Cursor = Cursors.WaitCursor;
171
172 ToolStripSeparator menuSep1 = new ToolStripSeparator();
173 ToolStripSeparator menuSep2 = new ToolStripSeparator();
174 ToolStripMenuItem menuOption = new ToolStripMenuItem("設定(&S)...");
175 menuOption.Click += new EventHandler(this.menuItemOption_Click);
176 ToolStripMenuItem menuExit = new ToolStripMenuItem("終了(&X)");
177 menuExit.Click += new EventHandler(this.menuItemExit_Click);
178
179 ResetProcessesInfo();
180 contextMenuStrip.Items.Clear();
181 contextMenuStrip.ImageList = this.imageList;
182
183 foreach (ProcessInfo process in this.processList) {
184 if (process.exists) {
185 ToolStripMenuItem item = new ToolStripMenuItem(process.name);
186 item.ToolTipText = process.title;
187 item.Tag = process;
188 item.ImageIndex = (process.transparent) ? 1 : 0;
189 item.Checked = process.transparent;
190 item.Text += (process.transparent) ? " *" : "";
191 item.Click += new EventHandler(this.menuItemProcess_Click);
192 contextMenuStrip.Items.Add(item);
193 }
194 }
195
196 contextMenuStrip.Items.Add(menuSep1);
197 contextMenuStrip.Items.Add(menuOption);
198 contextMenuStrip.Items.Add(menuSep2);
199 contextMenuStrip.Items.Add(menuExit);
200
201 this.Cursor = Cursors.Default;
202 }
203
204 private void menuItemOption_Click(object sender, EventArgs e) {
205 SettingForm dialog = new SettingForm(this.setting);
206 DialogResult result = dialog.ShowDialog(this);
207 if (result == DialogResult.OK) {
208 this.setting = dialog.GetSetting();
209 SaveSetting();
210 }
211 }
212
213 private void menuItemExit_Click(object sender, EventArgs e) {
214 notifyIcon.Visible = false;
215 this.Close();
216 }
217
218 private void menuItemProcess_Click(object sender, EventArgs e) {
219 ToolStripMenuItem item = (ToolStripMenuItem)sender;
220 ProcessInfo processInfo = (ProcessInfo)item.Tag;
221
222 bool transparent = !processInfo.transparent;
223 if (SetTransparent(processInfo.hWnd, transparent)) {
224 processInfo.transparent = transparent;
225 }
226 }
227
228 private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) {
229 SetTransparentForAllPutty();
230 }
231
232 private void timer_Tick(object sender, EventArgs e) {
233 ResetProcessesInfo();
234 }
235
236 private void labelTitle_Click(object sender, EventArgs e) {
237 this.Hide();
238 }
239 }
240 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26