How do I mute audio from a WPF application or window? - c#

I have a Web Browser in my WPF application which plays a Flash, but the Flash has audio and I don't want the users to hear it. I'm assuming that there has to be some way to mute either the Web Browser, the Window or the entire Application. I've tried stuff like the following, but nothing has worked. Could I get some assistance, please?
private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int WM_APPCOMMAND = 0x319;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam,
IntPtr lParam);
public EditTags() {
IntPtr windowHandle = new WindowInteropHelper(this).Handle;
SendMessageW(windowHandle, WM_APPCOMMAND, windowHandle, (IntPtr)
APPCOMMAND_VOLUME_MUTE);
}
EDIT:
A solution has been found by Jimi.
Link for those who want it: Webbrowser disable all audio output - from online radio to youtube.

Related

How to start a process from a Windows Forms (C#) without creating a taskbar icon

I have a Windows Forms application with a button that's supposed to open an exe file.
The default Windows behavior is that the exe file, once opened, will have its own icon appear in the taskbar, however the requirement is for it not to have the icon, and for the window to appear "nested" inside the icon of the Windows Form application from which it originates, so to hide the location of the exe file from users.
I have looked around for a solution, and my understanding is that in order to achieve this, I would have to use the user32.dll library.
I have found some code online that attempts something similar (no window, no taskbar icon), and I am trying to tweak it so that it fits my needs, but so far I am stuck.
This is what I have:
private int GWL_STYLE = -16;
private int GWL_EXSTYLE = -20;
private int WS_EX_APPWINDOW = 262144;
private UInt32 WS_POPUP = 0x80000000;
private UInt32 WS_CHILD = 0x40000000;
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
public Process HideProcess(string executablesPath, System.Windows.Forms.Form parentForm) {
Process valueToReturn = null;
if (executablesPath != null && executablesPath.Equals(String.Empty) == false && File.Exists(executablesPath) == true) {
ProcessStartInfo startInfo = new ProcessStartInfo(executablesPath);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
valueToReturn = Process.Start(startInfo);
int style = GetWindowLong(valueToReturn.MainWindowHandle, GWL_EXSTYLE) & ~WS_EX_APPWINDOW;
SetWindowLong(valueToReturn.MainWindowHandle, GWL_EXSTYLE, style);
SetParent(valueToReturn.MainWindowHandle, parentForm.Handle);
}
return valueToReturn;
}
I have tried several different variations of this method, but I always get close but not exactly where I'd like to be.
The documentation I have found seems a little bit confusing, so I am a little bit stuck and would appreciate some help.

How do I set the focus to the Desktop from within my C# application

Winforms App. .Net 3.5.
I need to set the focus from my C# application to the user's desktop (almost like simulating a mouse click on the desktop).
Can someone please show me how to do this with C#? I just want to set focus on the desktop so the focus is no longer on my application but I want to do this from within my application.
Edit: An answer below works by setting the focus to the desktop, but it minimizes all the open windows on the user's desktop.
Is there a way I can maybe set the focus to the next open window on the desktop instead? I just want to get the focus off of my application (without minimizing my application or hiding it). I just want to move focus to somewhere else. Maybe the desktop was not the best choice if it will minimize all the user's open windows/applications.
This should do it for you.
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1 {
class Program {
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
const int WM_COMMAND = 0x111;
const int MIN_ALL = 419;
const int MIN_ALL_UNDO = 416;
static void Main(string[] args) {
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero);
System.Threading.Thread.Sleep(2000);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
}
}
}
Get Next Window
I don't have a code example ready for these two but I'm going to give you the links to both. The first think you need to do is call GetWindow. After doing that you'll want to call SwitchToThisWindow passing in the pointer you received from GetWindow.
You can add this COM object in your project:
Microsoft Shell Controls And Automation
And then just call:
Shell32.ShellClass shell = new Shell32.ShellClass();
shell.MinimizeAll();
This will minimize all the windows and then focus the desktop. Otherwise, if you have your window non-full screen then you can simulate the mouse click using:
//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
You can calculate coordinates by looking at your window startup location plus height/width and select a available space (that will be the desktop indeed).

Detecting a modal dialog box of another process

I want to detect whether another process say process.exe is currently displaying a dialog box ?
Is there a way to do that in C# ?
To see if I could get the handle of the dialog box. I have tried Spy++ 's find window tool, when I try to drag the finder on top of the dialog box, it does not highlight the dialogbox but populates the details and
mentions AppCustomDialogBox and mentions the handle number
Please advise how can I programatically detect that ..
Thanks,
When an application shows a dialog box, the (for me quietly annoying) behaviour of Windows Operating System is to show the newly created window on top of all other. So if I assume that You know which process to watch, a way to detect a new window is to set up a windows hook:
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
[DllImport("user32.dll")]
public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr
hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
uint idThread, uint dwFlags);
[DllImport("user32.dll")]
public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
// Constants from winuser.h
public const uint EVENT_SYSTEM_FOREGROUND = 3;
public const uint WINEVENT_OUTOFCONTEXT = 0;
//The GetForegroundWindow function returns a handle to the foreground window.
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
// For example, in Main() function
// Listen for foreground window changes across all processes/threads on current desktop
IntPtr hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero,
new WinEventDelegate(WinEventProc), 0, 0, WINEVENT_OUTOFCONTEXT);
void WinEventProc(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
IntPtr foregroundWinHandle = GetForegroundWindow();
//Do something (f.e check if that is the needed window)
}
//When you Close Your application, remove the hook:
UnhookWinEvent(hhook);
I did not try that code explicitely for dialog boxes, but for separate processes it works well. Please remember that that code cannot work in a windows service or a console application as it requires a message pump (Windows applications have that). You'll have to create an own.
Hope this helps
As modal dialogs normally disable the parent window(s), you can enumerate all top level windows for a process and see if they're enabled using the IsWindowEnabled() function.

How do I tell if the master volume is muted?

I am using the following to mute/unmute the master audio on my computer. Now, I am looking for a way to determine the mute state. Is there a just as easy way to do this in C#?
private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int WM_APPCOMMAND = 0x319;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
Hi just stumbled accross this old topic, but wa shaving the exact same issue.
I solved using the following:
using System.Runtime.InteropServices;
...
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int APPCOMMAND_VOLUME_UP = 0xA0000;
private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
private const int WM_APPCOMMAND = 0x319;
...
// mute (toggle)
SendMessage(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_MUTE);
// unmute
SendMessage(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_UP);
SendMessage(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_DOWN);
Mute will not always mute the audio, it's a toggle - but if you make sure to call "unmute" first you should be golden.
Best regards
Kurt
Checkout the following tutorial. I've never played with the Mixer in C# ( or any other language ) so I'm assuming they are correct in P/Invoking the Win32 APIs and that they aren't reinventing the wheel. You can download the example and I think the method GetMixer() will do what you want.
Credit to RRUZ in comments above. See stackoverflow.com/questions/294292. The only problem is that you need compatibility mode.

Capture Highlighted Text from any window using C#

How to read the highlighted/Selected Text from any window using c#.
i tried 2 approaches.
Send "^c" whenever user selects some thing. But in this case my clipboard is flooded with lots of unnecessary data. Sometime it copied passwords also.
so i switched my approach to 2nd method, send message method.
see this sample code
[DllImport("user32.dll")]
static extern int GetFocus();
[DllImport("user32.dll")]
static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);
[DllImport("user32.dll") ]
static extern int GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);
// second overload of SendMessage
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, uint Msg, out int wParam, out int lParam);
const int WM_SETTEXT = 12;
const int WM_GETTEXT = 13;
private string PerformCopy()
{
try
{
//Wait 5 seconds to give us a chance to give focus to some edit window,
//notepad for example
System.Threading.Thread.Sleep(5000);
StringBuilder builder = new StringBuilder(500);
int foregroundWindowHandle = GetForegroundWindow();
uint remoteThreadId = GetWindowThreadProcessId(foregroundWindowHandle, 0);
uint currentThreadId = GetCurrentThreadId();
//AttachTrheadInput is needed so we can get the handle of a focused window in another app
AttachThreadInput(remoteThreadId, currentThreadId, true);
//Get the handle of a focused window
int focused = GetFocus();
//Now detach since we got the focused handle
AttachThreadInput(remoteThreadId, currentThreadId, false);
//Get the text from the active window into the stringbuilder
SendMessage(focused, WM_GETTEXT, builder.Capacity, builder);
return builder.ToString();
}
catch (System.Exception oException)
{
throw oException;
}
}
this code working fine in Notepad. But if i try to capture from another applications like Mozilla firefox, or Visual Studio IDE, it's not returning the text.
Can anybody please help me, where i am doing wrong? First of all, i have chosen the right approach?
That's because both Firefox and Visual Studio don't use the built-in Win32 controls for displaying/editing text.
It is not possible in general to be able to get the value of "any" selected text, because of the fact that programs can re-implement their own version of the Win32 controls any way they see fit, and your program cannot possibly expect to work with all of them.
However, you can use the UI Automation APIs which will allow you to interact with the majority of 3rd-party controls (at least, all the good ones - such as Visual Studio and Firefox - will likely work with the UI Automation APIs since it's a requirement for accessibility)

Categories