Develop and Download Open Source Software

Browse Subversion Repository

Contents of /TransparentizeWindow/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (show annotations) (download)
Thu Feb 16 15:07:56 2012 UTC (12 years, 1 month ago) by yuuan
File size: 7980 byte(s)
- PuTTYを透過するかどうかの状態を保存するようにした
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 Form1 : 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 public class ApplicationSetting
45 {
46 [System.Xml.Serialization.XmlElement("alpha")]
47 public byte alphaValue = 210;
48 public bool puttysAreTransparent = false;
49 public bool notTransparentPuttySetting = false;
50 public string[] ngwords = new string[] {"sidebar", "Saezuri"};
51 }
52
53 private List<ProcessInfo> processList = new List<ProcessInfo>();
54 private ApplicationSetting setting = new ApplicationSetting();
55 private bool allowExit = false;
56
57 private void SaveSetting() {
58 try {
59 XmlSerializer sirializer = new XmlSerializer(typeof(ApplicationSetting));
60 FileStream stream = new FileStream("TraPutty.xml", FileMode.Create);
61 sirializer.Serialize(stream, this.setting);
62 stream.Close();
63 }
64 catch { }
65 }
66
67 private void LoadSettings() {
68 try {
69 XmlSerializer serializer = new XmlSerializer(typeof(ApplicationSetting));
70 FileStream stream = new FileStream("TraPutty.xml", FileMode.Open);
71 this.setting = (ApplicationSetting)serializer.Deserialize(stream);
72 }
73 catch {
74 this.setting = new ApplicationSetting();
75 }
76 }
77
78 private void ResetProcessesInfo() {
79 for (int i = 0; i < processList.Count; i++) {
80 processList[i].exists = false;
81 }
82
83 Process[] processes = Process.GetProcesses();
84 foreach (Process process in processes) {
85 if (process.MainWindowHandle != IntPtr.Zero) {
86 bool ng = false;
87 foreach (string ngword in this.setting.ngwords) {
88 if (process.ProcessName.Equals(ngword, StringComparison.CurrentCultureIgnoreCase)) ng = true;
89 }
90 if (!ng) {
91 bool exists = false;
92 for (int i = 0; i < processList.Count; i++) {
93 ProcessInfo processInfo = processList[i];
94 if (processInfo.hWnd == process.MainWindowHandle) {
95 int exStyle = Win32API.GetWindowLong(processInfo.hWnd, Win32API.GWL_EXSTYLE);
96 if ((exStyle & (int)Win32API.WS_EX.LAYERED) != 0) {
97 byte alpha;
98 Win32API.GetLayeredWindowAttributes(processInfo.hWnd, 0, out alpha, 0);
99 processInfo.transparent = (alpha != 255) ? true : false;
100 }
101 processInfo.Update(process);
102 exists = true;
103 break;
104 }
105 }
106 if (!exists) {
107 ProcessInfo pi = new ProcessInfo(process);
108 this.processList.Add(pi);
109
110 if (this.setting.puttysAreTransparent) {
111 SetTransparentForPutty(pi, true);
112 }
113 }
114 }
115 }
116 }
117
118 processList.Sort(delegate(ProcessInfo x, ProcessInfo y) {
119 return x.name.CompareTo(y.name);
120 });
121
122 for (int i = 0; i < processList.Count; i++ ) {
123 if (!processList[i].exists) {
124 processList.Remove(processList[i]);
125 }
126 }
127 }
128
129 private bool SetTransparent(IntPtr hWnd, bool transparent) {
130 if (transparent) {
131 int exStyle = Win32API.GetWindowLong(hWnd, Win32API.GWL_EXSTYLE);
132 Win32API.SetWindowLong(hWnd, Win32API.GWL_EXSTYLE, exStyle | (int)Win32API.WS_EX.LAYERED);
133
134 return Win32API.SetLayeredWindowAttributes(hWnd, 0, this.setting.alphaValue, Win32API.LWA_ALPHA);
135 }
136 else {
137 return Win32API.SetLayeredWindowAttributes(hWnd, 0, 255, Win32API.LWA_ALPHA);
138 }
139 }
140
141 private void SetTransparentForAllPutty() {
142 this.setting.puttysAreTransparent = !this.setting.puttysAreTransparent;
143 foreach (ProcessInfo process in processList) {
144 this.SetTransparentForPutty(process, this.setting.puttysAreTransparent);
145 }
146 }
147
148 private void SetTransparentForPutty(ProcessInfo process, bool transparent) {
149 if (process.name.Equals("putty", StringComparison.CurrentCultureIgnoreCase)) {
150 if (this.setting.notTransparentPuttySetting && process.title.Equals("PuTTY 設定"))
151 transparent = false;
152 SetTransparent(process.hWnd, transparent);
153 }
154 }
155
156 public Form1() {
157 InitializeComponent();
158 }
159
160 private void Form1_Load(object sender, EventArgs e) {
161 LoadSettings();
162 ResetProcessesInfo();
163 timer.Enabled = true;
164 }
165
166 private void Form1_Shown(object sender, EventArgs e) {
167 this.Hide();
168 }
169
170 private void contextMenuStrip_Opening(object sender, CancelEventArgs e) {
171 this.Cursor = Cursors.WaitCursor;
172
173 ToolStripSeparator menuSep1 = new ToolStripSeparator();
174 ToolStripSeparator menuSep2 = new ToolStripSeparator();
175 ToolStripMenuItem menuOption = new ToolStripMenuItem("設定(&S)");
176 menuOption.Click += new EventHandler(this.menuItemOption_Click);
177 ToolStripMenuItem menuExit = new ToolStripMenuItem("終了(&X)");
178 menuExit.Click += new EventHandler(this.menuItemExit_Click);
179
180 ResetProcessesInfo();
181 contextMenuStrip.Items.Clear();
182 contextMenuStrip.ImageList = this.imageList;
183
184 foreach (ProcessInfo process in this.processList) {
185 if (process.exists) {
186 ToolStripMenuItem item = new ToolStripMenuItem(process.name);
187 item.ToolTipText = process.title;
188 item.Tag = process;
189 item.ImageIndex = (process.transparent) ? 1 : 0;
190 item.Checked = process.transparent;
191 item.Text += (process.transparent) ? " *" : "";
192 item.Click += new EventHandler(this.menuItemProcess_Click);
193 contextMenuStrip.Items.Add(item);
194 }
195 }
196
197 contextMenuStrip.Items.Add(menuSep1);
198 contextMenuStrip.Items.Add(menuOption);
199 contextMenuStrip.Items.Add(menuSep2);
200 contextMenuStrip.Items.Add(menuExit);
201
202 this.Cursor = Cursors.Default;
203 }
204
205 private void menuItemOption_Click(object sender, EventArgs e) {
206 this.numTransparent.Value = (int)this.setting.alphaValue;
207 this.chkNotTransparentPuttySetting.Checked = this.setting.notTransparentPuttySetting;
208 this.Enabled = true;
209 this.Show();
210 this.Activate();
211 }
212
213 private void menuItemExit_Click(object sender, EventArgs e) {
214 SaveSetting();
215 this.allowExit = true;
216 this.Close();
217 }
218
219 private void menuItemProcess_Click(object sender, EventArgs e) {
220 ToolStripMenuItem item = (ToolStripMenuItem)sender;
221 ProcessInfo processInfo = (ProcessInfo)item.Tag;
222
223 bool transparent = !processInfo.transparent;
224 if (SetTransparent(processInfo.hWnd, transparent)) {
225 processInfo.transparent = transparent;
226 }
227 }
228
229 private void buttonCancel_Click(object sender, EventArgs e) {
230 this.Hide();
231 }
232
233 private void buttonOk_Click(object sender, EventArgs e) {
234 this.setting.alphaValue = (byte)numTransparent.Value;
235 this.setting.notTransparentPuttySetting = chkNotTransparentPuttySetting.Checked;
236 this.Hide();
237 }
238
239 private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
240 if (!this.allowExit) {
241 e.Cancel = true;
242 this.Hide();
243 }
244 }
245
246 private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) {
247 SetTransparentForAllPutty();
248 }
249
250 private void linkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
251 Process.Start("http://www.yuuan.net/blog/");
252 }
253
254 private void timer_Tick(object sender, EventArgs e) {
255 ResetProcessesInfo();
256 }
257 }
258 }

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