using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
-using System.Xml.Serialization;
-using System.IO;
namespace TraPutty
{
public partial class MainForm : Form
{
- private class ProcessInfo
- {
- public bool transparent = false;
- public IntPtr hWnd = IntPtr.Zero;
- public string name;
- public string title;
- public bool exists = true;
-
- public ProcessInfo(IntPtr hWnd, string name, string title) {
- this.hWnd = hWnd;
- this.name = name;
- this.title = title;
- }
-
- public ProcessInfo(Process process) {
- Update(process);
- }
-
- public void Update(Process process) {
- this.hWnd = process.MainWindowHandle;
- this.name = process.ProcessName;
- this.title = process.MainWindowTitle;
- this.exists = true;
- }
- }
-
- private List<ProcessInfo> processList = new List<ProcessInfo>();
- private ApplicationSetting setting = new ApplicationSetting();
+ /// <summary>
+ /// 設定
+ /// </summary>
+ private ApplicationSetting settings = new ApplicationSetting();
+
+ /// <summary>
+ /// 設定を変更するためのフォーム
+ /// </summary>
private SettingForm settingForm = new SettingForm();
- private void SaveSetting() {
- try {
- XmlSerializer sirializer = new XmlSerializer(typeof(ApplicationSetting));
- FileStream stream = new FileStream("TraPutty.xml", FileMode.Create);
- sirializer.Serialize(stream, this.setting);
- stream.Close();
- }
- catch { }
- }
-
- private void LoadSettings() {
- try {
- XmlSerializer serializer = new XmlSerializer(typeof(ApplicationSetting));
- FileStream stream = new FileStream("TraPutty.xml", FileMode.Open);
- this.setting = (ApplicationSetting)serializer.Deserialize(stream);
- }
- catch {
- this.setting = new ApplicationSetting();
- }
- }
-
- private void ResetProcessesInfo() {
- for (int i = 0; i < processList.Count; i++) {
- processList[i].exists = false;
- }
-
- Process[] processes = Process.GetProcesses();
- foreach (Process process in processes) {
- if (process.MainWindowHandle != IntPtr.Zero) {
- bool ng = false;
- foreach (string ngword in this.setting.ngwords) {
- if (process.ProcessName.Equals(ngword, StringComparison.CurrentCultureIgnoreCase)) ng = true;
- }
- if (!ng) {
- bool exists = false;
- for (int i = 0; i < processList.Count; i++) {
- ProcessInfo processInfo = processList[i];
- if (processInfo.hWnd == process.MainWindowHandle) {
- int exStyle = Win32API.GetWindowLong(processInfo.hWnd, Win32API.GWL_EXSTYLE);
- if ((exStyle & (int)Win32API.WS_EX.LAYERED) != 0) {
- byte alpha;
- Win32API.GetLayeredWindowAttributes(processInfo.hWnd, 0, out alpha, 0);
- processInfo.transparent = (alpha != 255) ? true : false;
- }
- processInfo.Update(process);
- exists = true;
- break;
- }
- }
- if (!exists) {
- ProcessInfo pi = new ProcessInfo(process);
- this.processList.Add(pi);
-
- if (this.setting.puttysAreTransparent) {
- SetTransparentForPutty(pi, true);
- }
- }
- }
- }
- }
-
- processList.Sort(delegate(ProcessInfo x, ProcessInfo y) {
- return x.name.CompareTo(y.name);
- });
-
- for (int i = 0; i < processList.Count; i++ ) {
- if (!processList[i].exists) {
- processList.Remove(processList[i]);
- }
- }
- }
-
- private bool SetTransparent(IntPtr hWnd, bool transparent) {
- if (transparent) {
- int exStyle = Win32API.GetWindowLong(hWnd, Win32API.GWL_EXSTYLE);
- Win32API.SetWindowLong(hWnd, Win32API.GWL_EXSTYLE, exStyle | (int)Win32API.WS_EX.LAYERED);
-
- return Win32API.SetLayeredWindowAttributes(hWnd, 0, this.setting.alphaValue, Win32API.LWA_ALPHA);
- }
- else {
- return Win32API.SetLayeredWindowAttributes(hWnd, 0, 255, Win32API.LWA_ALPHA);
- }
- }
+ /// <summary>
+ /// 透過する PuTTY ウィンドウのリスト
+ /// </summary>
+ private WindowList putties = new WindowList();
+
+ /// <summary>
+ /// PuTTY ウィンドウを探してターゲットに追加する
+ /// </summary>
+ private void SearchPutty() {
+ this.putties.RemoveAll(window => window.NotExists);
+
+ foreach (var process in Process.GetProcesses()) {
+ if (Window.IsPuttyProcess(process)) {
+ this.putties.FindOrCreate(process);
+ }
+ }
+ }
- private void SetTransparentForAllPutty() {
- this.setting.puttysAreTransparent = !this.setting.puttysAreTransparent;
- foreach (ProcessInfo process in processList) {
- this.SetTransparentForPutty(process, this.setting.puttysAreTransparent);
- }
- }
+ /// <summary>
+ /// PuTTY ウィンドウに透過状態を適用する
+ /// </summary>
+ private void TransparentPutty() {
+ foreach (var window in this.putties) {
+ if (! window.IsPuttyConfig || ! this.settings.notTransparentPuttySetting) {
+ window.Transparency = this.settings.puttysAreTransparent ? Window.NOT_TRANSPARENT : this.settings.alpha;
+ }
+ }
+ }
- private void SetTransparentForPutty(ProcessInfo process, bool transparent) {
- if (process.name.Equals("putty", StringComparison.CurrentCultureIgnoreCase)) {
- if (this.setting.notTransparentPuttySetting && IsSetting(process))
- transparent = false;
- SetTransparent(process.hWnd, transparent);
- }
- }
+ /// <summary>
+ /// PuTTY の透過状態を切り替える
+ /// </summary>
+ private void ToggleTransparencyOfPuttys() {
+ this.settings.puttysAreTransparent = ! this.settings.puttysAreTransparent;
- private bool IsSetting(ProcessInfo process) {
- if (process.title.Equals("PuTTY 設定"))
- return true;
- if (process.title.Equals("PuTTY Configuration"))
- return true;
- return false;
+ this.TransparentPutty();
}
+ /// <summary>
+ /// コンストラクタ
+ /// </summary>
public MainForm() {
InitializeComponent();
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
this.Enabled = false;
- LoadSettings();
- ResetProcessesInfo();
+ this.settings = ApplicationSetting.Load();
+
+ this.SearchPutty();
timer.Enabled = true;
}
+ /// <summary>
+ /// 起動時
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
private void MainForm_Load(object sender, EventArgs e) {
this.Hide();
}
+ /// <summary>
+ /// 終了前
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
- SaveSetting();
+ this.settings.Save();
}
+ /// <summary>
+ /// タスクトレイからメニューを開いたとき
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
private void contextMenuStrip_Opening(object sender, CancelEventArgs e) {
this.Cursor = Cursors.WaitCursor;
this.TopMost = false;
- ToolStripSeparator menuSep1 = new ToolStripSeparator();
- ToolStripSeparator menuSep2 = new ToolStripSeparator();
- ToolStripMenuItem menuOption = new ToolStripMenuItem("設定(&S)...");
+ var menuSep1 = new ToolStripSeparator();
+ var menuSep2 = new ToolStripSeparator();
+ var menuOption = new ToolStripMenuItem("設定(&S)...");
menuOption.Click += new EventHandler(this.menuItemOption_Click);
- ToolStripMenuItem menuExit = new ToolStripMenuItem("終了(&X)");
+ var menuExit = new ToolStripMenuItem("終了(&X)");
menuExit.Click += new EventHandler(this.menuItemExit_Click);
- ResetProcessesInfo();
contextMenuStrip.Items.Clear();
contextMenuStrip.ImageList = this.imageList;
- foreach (ProcessInfo process in this.processList) {
- if (process.exists) {
- ToolStripMenuItem item = new ToolStripMenuItem(process.name);
- item.ToolTipText = process.title;
- item.Tag = process;
- item.ImageIndex = (process.transparent) ? 1 : 0;
- item.Checked = process.transparent;
- item.Text += (process.transparent) ? " *" : "";
- item.Click += new EventHandler(this.menuItemProcess_Click);
- contextMenuStrip.Items.Add(item);
- }
+ foreach (var process in Process.GetProcesses().OrderBy(p => p.ProcessName)) {
+ if (process.MainWindowHandle != IntPtr.Zero) {
+ if (! this.settings.ngwords.Contains(process.ProcessName)) {
+ var window = new Window(process);
+
+ var item = new ToolStripMenuItem(process.ProcessName);
+ item.ToolTipText = process.MainWindowTitle;
+ item.Tag = process;
+ item.ImageIndex = window.IsTransparents ? 1 : 0;
+ item.Checked = window.IsTransparents;
+ item.Text += (window.IsTransparents) ? " *" : "";
+ item.Click += new EventHandler(this.menuItemProcess_Click);
+ contextMenuStrip.Items.Add(item);
+ }
+ }
}
contextMenuStrip.Items.Add(menuSep1);
this.Cursor = Cursors.Default;
}
+ /// <summary>
+ /// 設定メニュー
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
private void menuItemOption_Click(object sender, EventArgs e) {
- settingForm.SetSetting(this.setting);
+ settingForm.SetSetting(this.settings);
if (settingForm.Visible == false) {
- DialogResult result = settingForm.ShowDialog(this);
+ var result = settingForm.ShowDialog(this);
if (result == DialogResult.OK) {
- this.setting = settingForm.GetSetting();
- SaveSetting();
+ this.settings = settingForm.GetSetting();
+ this.settings.Save();
}
}
}
+ /// <summary>
+ /// 終了メニュー
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
private void menuItemExit_Click(object sender, EventArgs e) {
notifyIcon.Visible = false;
Application.Exit();
}
+ /// <summary>
+ /// メニューからプロセスを選択したとき
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
private void menuItemProcess_Click(object sender, EventArgs e) {
- ToolStripMenuItem item = (ToolStripMenuItem)sender;
- ProcessInfo processInfo = (ProcessInfo)item.Tag;
+ var item = (ToolStripMenuItem)sender;
+ var process = (Process)item.Tag;
+ var window = new Window(process);
- bool transparent = !processInfo.transparent;
- if (SetTransparent(processInfo.hWnd, transparent)) {
- processInfo.transparent = transparent;
- }
+ if (window.IsTransparents) {
+ window.Transparency = Window.NOT_TRANSPARENT;
+ }
+ else {
+ window.Transparency = this.settings.alpha;
+ }
}
+ /// <summary>
+ /// ただちに PuTTY の透過状態を切り替える
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) {
- SetTransparentForAllPutty();
+ this.ToggleTransparencyOfPuttys();
}
+ /// <summary>
+ /// 定期的に
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
private void timer_Tick(object sender, EventArgs e) {
- ResetProcessesInfo();
+ this.SearchPutty();
+ this.TransparentPutty();
}
+ /// <summary>
+ /// メインフォームのラベルをクリックしたら
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
private void labelTitle_Click(object sender, EventArgs e) {
this.Hide();
}
+ /// <summary>
+ /// メインフォームをクリックしたら
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
private void MainForm_Click(object sender, EventArgs e) {
this.Hide();
}
// buttonOk
//
this.buttonOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
- this.buttonOk.Location = new System.Drawing.Point(168, 128);
+ this.buttonOk.Location = new System.Drawing.Point(216, 149);
this.buttonOk.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonOk.Name = "buttonOk";
this.buttonOk.Size = new System.Drawing.Size(103, 32);
//
// label1
//
- this.label1.Location = new System.Drawing.Point(8, 24);
+ this.label1.Location = new System.Drawing.Point(18, 40);
+ this.label1.Margin = new System.Windows.Forms.Padding(0);
this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(121, 30);
+ this.label1.Size = new System.Drawing.Size(182, 30);
this.label1.TabIndex = 1;
this.label1.Text = "透過度 (0-254) : ";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
// numTransparent
//
this.numTransparent.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
- this.numTransparent.Location = new System.Drawing.Point(295, 24);
+ this.numTransparent.Location = new System.Drawing.Point(343, 40);
this.numTransparent.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.numTransparent.Maximum = new decimal(new int[] {
254,
0,
0});
this.numTransparent.Name = "numTransparent";
- this.numTransparent.Size = new System.Drawing.Size(65, 25);
+ this.numTransparent.Size = new System.Drawing.Size(65, 30);
this.numTransparent.TabIndex = 2;
this.numTransparent.Value = new decimal(new int[] {
210,
// chkNotTransparentPuttySetting
//
this.chkNotTransparentPuttySetting.AutoSize = true;
- this.chkNotTransparentPuttySetting.Location = new System.Drawing.Point(8, 72);
+ this.chkNotTransparentPuttySetting.Location = new System.Drawing.Point(20, 84);
this.chkNotTransparentPuttySetting.Name = "chkNotTransparentPuttySetting";
- this.chkNotTransparentPuttySetting.Size = new System.Drawing.Size(165, 22);
+ this.chkNotTransparentPuttySetting.Size = new System.Drawing.Size(204, 27);
this.chkNotTransparentPuttySetting.TabIndex = 3;
this.chkNotTransparentPuttySetting.Text = "PuTTY 設定を透過しない";
this.chkNotTransparentPuttySetting.UseVisualStyleBackColor = true;
this.groupBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
- this.groupBox1.Size = new System.Drawing.Size(376, 113);
+ this.groupBox1.Size = new System.Drawing.Size(424, 134);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "設定";
//
this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
- this.buttonCancel.Location = new System.Drawing.Point(280, 128);
+ this.buttonCancel.Location = new System.Drawing.Point(328, 149);
this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(103, 32);
//
this.linkLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.linkLabel.AutoSize = true;
- this.linkLabel.Location = new System.Drawing.Point(8, 136);
+ this.linkLabel.Location = new System.Drawing.Point(8, 157);
this.linkLabel.Name = "linkLabel";
- this.linkLabel.Size = new System.Drawing.Size(129, 18);
+ this.linkLabel.Size = new System.Drawing.Size(164, 23);
this.linkLabel.TabIndex = 9;
this.linkLabel.TabStop = true;
- this.linkLabel.Text = "© 2012-2014 yuuAn.";
+ this.linkLabel.Text = "© 2012-2017 yuuAn.";
this.linkLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel_LinkClicked);
//
// SettingForm
//
this.AcceptButton = this.buttonOk;
- this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 18F);
+ this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 23F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.buttonCancel;
- this.ClientSize = new System.Drawing.Size(394, 172);
+ this.ClientSize = new System.Drawing.Size(442, 193);
this.Controls.Add(this.linkLabel);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.groupBox1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SettingForm";
- this.Text = "とらぷてぃ。 v.1.7";
+ this.Text = "とらぷてぃ。 v.2.0";
this.Load += new System.EventHandler(this.SettingForm_Load);
((System.ComponentModel.ISupportInitialize)(this.numTransparent)).EndInit();
this.groupBox1.ResumeLayout(false);