Created
February 10, 2018 14:23
-
-
Save 27Cobalter/3f666f4e04d1279ac31d4ac7788af3b8 to your computer and use it in GitHub Desktop.
渡したパスのファイルをWindowsMediaPlayerで実行して閉じるやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Automation; | |
using System.Diagnostics; | |
namespace WMPhandler | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string file = "C:\\Windows\\media\\Windows Background.wav"; | |
string[] argument = System.Environment.GetCommandLineArgs(); | |
System.Diagnostics.Process process = new System.Diagnostics.Process(); | |
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); | |
if (argument.Length > 1) | |
{ | |
file = argument[1]; | |
} | |
for(int i = 1;i<argument.Length;i++) | |
{ | |
switch (argument[i]) | |
{ | |
case ("/hidden"): // /hiddenオプションを付けるとWMPを隠して実行 | |
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // サブプロセスのウィンドウを隠す | |
break; | |
default: | |
file = argument[i]; | |
break; | |
} | |
} | |
startInfo.FileName = "\"C:\\Program Files (x86)\\Windows Media Player\\wmplayer.exe\""; | |
startInfo.Arguments = "/play \"" + file + "\""; | |
process.StartInfo = startInfo; | |
process.Start(); | |
System.Threading.Thread.Sleep(1000); | |
CloseWnd(); | |
} | |
static void CloseWnd() | |
{ | |
IntPtr hWnd = GetWnd(); | |
if (hWnd == IntPtr.Zero) return; | |
AutomationElement ae = AutomationElement.FromHandle(hWnd); | |
TreeScope ts1 = TreeScope.Descendants | TreeScope.Element; | |
AutomationElement ae1 = ae.FindFirst(ts1, new PropertyCondition(AutomationElement.NameProperty, "候補ビュー")); | |
AutomationElement ae2 = ae1.FindFirst(ts1, new PropertyCondition(AutomationElement.NameProperty, "svSuggestions")); | |
while (ae2 == null) | |
{ | |
if (GetWnd() == IntPtr.Zero) return; | |
ae2 = ae1.FindFirst(ts1, new PropertyCondition(AutomationElement.NameProperty, "svSuggestions")); | |
} | |
AutomationElement title = ae.FindFirst(ts1, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "タイトル バー")); | |
InvokePattern close = title.FindFirst(ts1, new PropertyCondition(AutomationElement.NameProperty, "閉じる")).GetCurrentPattern(InvokePattern.Pattern) as InvokePattern; ; | |
close.Invoke(); | |
} | |
static IntPtr GetWnd() | |
{ | |
IntPtr hWnd = IntPtr.Zero; | |
string winTitle = "Windows Media Player"; | |
int RetlyCount = 3; | |
int RetryWaitms = 1000; | |
for (int i = 0; i < RetlyCount; i++) | |
{ | |
Process[] ps = Process.GetProcesses(); | |
foreach (Process pitem in ps) | |
{ | |
if (pitem.MainWindowTitle.Equals("")) continue; | |
if (pitem.MainWindowHandle != IntPtr.Zero && pitem.MainWindowTitle.Equals(winTitle)) | |
{ | |
hWnd = pitem.MainWindowHandle; | |
} | |
} | |
if (hWnd != IntPtr.Zero) break; | |
if (i < (RetlyCount - 1)) System.Threading.Thread.Sleep(RetryWaitms); | |
} | |
return hWnd; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment