I'm trying to use the youtube embedded player ( axshockwaveflash) and make a nice program out of it.
Thing is i'm trying to implement a button that'll replay/next/previous the current video.
what I have atm is:
private void btReplay_Click(object sender, EventArgs e)
{
if (!youtubePlayer.Focus())
{
youtubePlayer.Focus();
SendKeys.Send("0");
}
else
{
SendKeys.Send("0");
}
this.BringToFront();
}
The '0' key press makes the video replay from the start. Only it also makes the form dissappear between other open windows.
As you see i've tried using bringtofront but it won't work.
Any thoughts?
Also if anyone has any experience with this, i also want to play how to enable auto play the next video when using the 'END' key. I know about the autoplay=1 function but it won't seem to work when pressing the END key.
EDIT: Using WinForms btw
You didn't specify whether you use winForms or WPF. This snippet is for winforms.
Here I am giving you a static method that forces any given Form to front:
public static void bringWindowToFront(System.Windows.Forms.Form form)
{
uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), System.IntPtr.Zero);
uint appThread = GetCurrentThreadId();
const uint SW_SHOW = 5;
if (foreThread != appThread)
{
AttachThreadInput(foreThread, appThread, true);
BringWindowToTop(form.Handle);
ShowWindow(form.Handle, SW_SHOW);
AttachThreadInput(foreThread, appThread, false);
}
else
{
BringWindowToTop(form.Handle);
ShowWindow(form.Handle, SW_SHOW);
}
form.Activate();
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern bool BringWindowToTop(System.IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern System.IntPtr GetForegroundWindow();
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(System.IntPtr hWnd, System.IntPtr ProcessId);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowWindow(System.IntPtr hWnd, uint nCmdShow);
this.BringToFront();
this.TopMost = true;
worked for me, silly i didnt think of this.
Related
I'm intrested in disabling the classic windows effect-such as the fade in effect when you open a window.. etc.
I've seen this, i've tried to call the SystemParametersInfo in order to disable the effect,tried the suggestion in the solution.. but still.. i dont see any change...
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(
uint uiAction,
uint uiParam,
bool pvParam,
uint fWinIni
);
public const uint SPI_SETANIMATION = 0x73;
private unsafe void Form1_Load(object sender, EventArgs e)
{
bool enabled = false;
SystemParametersInfo(SPI_SETANIMATION, 0, enabled, 0);
}
I'm using windows 10.
I'll appreciate any incoming help.Thanks!
I want to change the Caret in a Textbox in C# that it appears wider like in does in old DOS applications.
What i have:
Example what i want:
I tried:
using System.Runtime.InteropServices;
...
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
...
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
CreateCaret(textBox1.Handle, IntPtr.Zero, 20, textBox1.Height);
ShowCaret(textBox1.Handle);
}
}
But it still looks the same. Any help would be nice. Thanks in advance!
Edit:
This was just an example. My real code looks like:
TextBox textbox = new TextBox();
textbox.MaxLength = fieldLength;
textbox.Width = fieldLength*24;
textbox.MaxLength = maxChars;
this.Controls.Add(textbox);
CreateCaret(textbox.Handle, IntPtr.Zero, 20, textbox.Height);
ShowCaret(textbox.Handle);
The code gets called but doesnt change anything.
Edit2:
I tried the example and it works fine but my problem still exisits:
I cant change the Caret when creating the textbox. Its only possible for a textbox created with the form.
You didn't link the event correctly, you should change to :
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
this.Shown += Form1_Shown;
}
private void Form1_Shown(object sender, EventArgs e)
{
CreateCaret(textBox1.Handle, IntPtr.Zero, 20, textBox1.Height);
ShowCaret(textBox1.Handle);
}
}
What is going on it seems is that the shown event will only initially work. When you leave the textbox by tabbing away and back to the control the caret will be reset by underlying code.
Take a look the answer in this thread.
I took their DrawDaret Method and changed it a bit. Calling DrawCaret on the textbox1.Enter event doesn't work. possibly the textbox implementation will notify the Enter event and then change the caret. This would undo a change done in the Enter event handler regarding to the caret.
EDIT
But the control also has a GotFocus event which you can use.
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
DrawCaret(textBox1);
}
public void DrawCaret(Control ctrl)
{
var nHeight = 0;
var nWidth = 10;
nHeight = Font.Height;
CreateCaret(ctrl.Handle, IntPtr.Zero, nWidth, nHeight);
ShowCaret(ctrl.Handle);
}
}
I have sent my user out to the browser with Application.OpenURL. And now I want to programatically bring unity back to the foreground.
Is there any way to do it without a plugin?
Thanks.
Use GetActiveWindow to get the window's handle before you send the user away, then use SetForegroundWindow using that handle. Before you use SetForegroundWindow, you can try simulating an Alt keypress to bring up a menu to abide by certain limitations of SetForegroundWindow:
private IntPtr unityWindow;
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
const int ALT = 0xA4;
const int EXTENDEDKEY = 0x1;
const int KEYUP = 0x2;
private void SendUser()
{
unityWindow = GetActiveWindow();
Application.OpenURL("http://example.com");
StartCoroutine(RefocusWindow(30f));
}
private IEnumerator RefocusWindow(float waitSeconds) {
// wait for new window to appear
yield return new WaitWhile(() => unityWindow == GetActiveWindow());
yield return new WaitForSeconds(waitSeconds);
// Simulate alt press
keybd_event((byte)ALT, 0x45, EXTENDEDKEY | 0, 0);
// Simulate alt release
keybd_event((byte)ALT, 0x45, EXTENDEDKEY | KEYUP, 0);
SetForegroundWindow(unityWindow);
}
if you are using Unity3D in Windows, try below code after calling Application.OpenURL(...) :
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
var prc = Process.GetProcessesByName("..."); //Get Unity's process, or
var prc = Process.GetCurrentProcess();
if (prc.Length > 0)
SetForegroundWindow(prc[0].MainWindowHandle);
This worked for me on Unity 5.0.1 / Windows 8.1:
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class ForeGrounder : MonoBehaviour {
private const uint LOCK = 1;
private const uint UNLOCK = 2;
private IntPtr window;
void Start() {
LockSetForegroundWindow(LOCK);
window = GetActiveWindow();
StartCoroutine(Checker());
}
IEnumerator Checker() {
while (true) {
yield return new WaitForSeconds(1);
IntPtr newWindow = GetActiveWindow();
if (window != newWindow) {
Debug.Log("Set to foreground");
SwitchToThisWindow(window, true);
}
}
}
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
static extern bool LockSetForegroundWindow(uint uLockCode);
[DllImport("user32.dll")]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
}
For Mac Standalone we can try
public void OpenURLInDefaultBrowser(string URL)
{
#if UNITY_STANDALONE_OSX
Screen.fullScreenMode = FullScreenMode.Windowed;
#endif
Application.OpenURL(URL);
StartCoroutine(ReFocusUnity());
}
private IEnumerator ReFocusUnity()
{
//You can wait for some seconds or wait for any callback you are expecting
yield return new WaitForSeconds(5f);
#if UNITY_STANDALONE_OSX
Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
#endif
}
How can I prevent WPF window from minimizing when users clicks on show desktop button?
This link will help you : Get the minimize box click of a WPF window
you need to catch the event and handle it yourself.
Edit : This method will alert you once the state is changed, so it might not be the "best" solution but it could work.
Windows are not minimized when "Show Desktop" is issued. Instead the "WorkerW" and "Desktop" windows are brought to the foreground.
I ended up developing my own solution.
I scoured the internet for weeks trying to find an answer so I'm kind of proud of this one.
So what we do is use pinvoke to create a hook for the EVENT_SYSTEM_FOREGROUND window event.
This event triggers whenever the foreground window is changed.
Now what I noticed is when the "Show Desktop" command is issued, the WorkerW window class becomes foreground.
Note this WorkerW window is not the desktop and I confirmed the hwnd of this WorkerW window is not the Desktop hwnd.
So what we do is whenever the WorkerW window becomes the foreground, we set our "WPF Gadget Window" to be topmost!
Whenever a window other the WorkerW window becomes the foreground, we remove topmost from our "WPF Gadget Window".
If you want to take it a step further, you can uncomment out the part where I check if the new foreground window is also "PROGMAN", which is the Desktop window.
However, this will lead to your window becoming topmost if the user clicks their desktop on a different monitor. In my case, I did not want this behavior, but I figured some of you might.
Confirmed to work in Windows 10. Should work in older versions of Windows.
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
namespace YourNamespace
{
internal static class NativeMethods
{
[DllImport("user32.dll")]
internal static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, ShowDesktop.WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
[DllImport("user32.dll")]
internal static extern bool UnhookWinEvent(IntPtr hWinEventHook);
[DllImport("user32.dll")]
internal static extern int GetClassName(IntPtr hwnd, StringBuilder name, int count);
}
public static class ShowDesktop
{
private const uint WINEVENT_OUTOFCONTEXT = 0u;
private const uint EVENT_SYSTEM_FOREGROUND = 3u;
private const string WORKERW = "WorkerW";
private const string PROGMAN = "Progman";
public static void AddHook(Window window)
{
if (IsHooked)
{
return;
}
IsHooked = true;
_delegate = new WinEventDelegate(WinEventHook);
_hookIntPtr = NativeMethods.SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _delegate, 0, 0, WINEVENT_OUTOFCONTEXT);
_window = window;
}
public static void RemoveHook()
{
if (!IsHooked)
{
return;
}
IsHooked = false;
NativeMethods.UnhookWinEvent(_hookIntPtr.Value);
_delegate = null;
_hookIntPtr = null;
_window = null;
}
private static string GetWindowClass(IntPtr hwnd)
{
StringBuilder _sb = new StringBuilder(32);
NativeMethods.GetClassName(hwnd, _sb, _sb.Capacity);
return _sb.ToString();
}
internal delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
private static void WinEventHook(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
if (eventType == EVENT_SYSTEM_FOREGROUND)
{
string _class = GetWindowClass(hwnd);
if (string.Equals(_class, WORKERW, StringComparison.Ordinal) /*|| string.Equals(_class, PROGMAN, StringComparison.Ordinal)*/ )
{
_window.Topmost = true;
}
else
{
_window.Topmost = false;
}
}
}
public static bool IsHooked { get; private set; } = false;
private static IntPtr? _hookIntPtr { get; set; }
private static WinEventDelegate _delegate { get; set; }
private static Window _window { get; set; }
}
}
You can change your window's parent to not be affected by Show Desktop. (as stated here: Window "on desktop")
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindowLoaded;
}
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private void MainWindowLoaded(object sender, RoutedEventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
var ProgmanHwnd = FindWindowEx(FindWindowEx(FindWindow("Progman", "Program Manager"), IntPtr.Zero, "SHELLDLL_DefView",""), IntPtr.Zero,"SysListView32", "FolderView");
SetParent(hwnd, ProgmanHwnd);
}
}
Hopefully this is a simple one, but can anyone provide some simple c# code that will launch the currently configured screensaver?
Here is a good site showing how to work with all aspects of the screensaver. See the comments at the end for the code to start the screensaver.
http://www.codeproject.com/KB/cs/ScreenSaverControl.aspx
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
//...
private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;
//...
public static void SetScreenSaverRunning()
{
SendMessage
(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}