How to create a non active dropdown with keybord focus - c#

I want to create a dropdown control which should be inactive and have keyboard input focus. So I created a control as below.
public class DropDownEdit : UserControl
{
[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private const int WS_EX_TOOLWINDOW = 0x00000080;
private const int WS_EX_TOPMOST = 0x00000008;
private const int WS_EX_NOACTIVATE = 0x08000000;
private const int WS_CHILD = 0x40000000;
private const int WS_POPUP = unchecked((int)0x80000000);
private TextBox text = new TextBox();
public DropDownEdit()
{
this.BackColor = Color.FromArgb(44, 68, 107);
this.Controls.Add(text);
this.Margin = Padding.Empty;
this.Padding = new Padding(0);
text.Multiline = true;
text.ScrollBars = ScrollBars.Both;
text.Size = new Size(this.Width, this.Height);
}
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.Style &= ~WS_CHILD;
createParams.Style |= WS_POPUP;
createParams.ExStyle |= WS_EX_TOOLWINDOW;
createParams.ExStyle |= WS_EX_TOPMOST;
createParams.ExStyle |= WS_EX_NOACTIVATE;
return createParams;
}
}
public void ShowWindow(Point point)
{
text.Focus();
this.Capture = true;
SetParent(this.Handle, IntPtr.Zero);
this.Location = point;
Show();
}
protected override void OnMouseCaptureChanged(EventArgs e)
{
base.OnMouseCaptureChanged(e);
this.Hide();
}
}
And when I am displaying the above dropdown window as below,
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Point point = this.PointToScreen(button1.Location);
DropDownEdit window = new DropDownEdit();
window.ShowWindow(new Point(point.X, point.Y + 20));
}
}
The Form1 has a flickering while displaying DropDownEdit. I think DropDownEdit get activated and Form1 loses its activation. How can I avoid this flickering in Form1?
NB:- I need input focus on TextBox in the dropdown control.

I found a solution.
While displaying my dropdown window it will receive activation and Windows will deactivate the main window. The fix for this is to send a WM_NCACTIVATE message to the parent to update its visual appearance without changing its activation status.
The below code is updated in DropDownEdit class to solve my issue.
private const int WM_NCACTIVATE = 0x86;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
protected override void WndProc(ref Message m)
{
// The popup needs to be activated for the user to interact with it,
// but we want to keep the owner window's appearance the same.
if ((m.Msg == WM_NCACTIVATE) && !_activating && (m.WParam != IntPtr.Zero))
{
// The popup is being activated, ensure parent keeps activated appearance
_activating = true;
SendMessage(this.Owner.Handle, WM_NCACTIVATE, (IntPtr) 1, IntPtr.Zero);
_activating = false;
// Call base.WndProc here if you want the appearance of the popup to change
}
else
{
base.WndProc(ref m);
}
}

Related

in winforms, how can i make a control not accept mouse events

I have a button and upon mouseenter, a little form pops up, and upon mouseleave of the button, the little form disappears. I am needing this form to not accept any mouse events, in other words, be "invisible" to the mouse.
The problem is, the form pops up under the mouse, which triggers the mouseleave event for the button. I know there are other ways to get around this, but i'm needing the form to hide when the mouse leaves the original button that triggered the form, and I also need the form to appear underneath the mouse.
So how can I make the little pop-up form invisible to mouse-events, so that it doesn't cause the "mouse leave" event to trigger for the button?
The popup is of type "Form". Here is the mouseEnter and mouseLeave code that triggers showing and hiding the form:
private void btnPatientSearch_MouseEnter(object sender, EventArgs e)
{
_currentPatientInfo = new PatientInfo()
{
MdiParent = this.MdiParent
};
_currentPatientInfo.Show();
_currentPatientInfo.Location = new Point(181, 9);
}
}
private void btnPatientSearch_MouseLeave(object sender, EventArgs e)
{
if (_currentPatientInfo == null) return;
_currentPatientInfo.Hide();
_currentPatientInfo = null;
}
Inherit your popup form from the following form class. This code is using some p/invokes and not tested, but it should work.
public class PopupForm : Form
{
private const int WS_BORDER = 0x00800000;
private const int WS_POPUP = unchecked((int)0x80000000);
private const int WS_EX_TOPMOST = 0x00000008;
private const int WS_EX_NOACTIVATE = 0x08000000;
private const int WM_MOUSEACTIVATE = 0x0021;
private const int MA_NOACTIVATEANDEAT = 4;
private static readonly IntPtr HWND_TOPMOST = (IntPtr)(-1);
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOACTIVATE = 0x0010;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int X, int Y, int cx, int cy, int uFlags);
public PopupForm()
{
SetStyle(ControlStyles.Selectable, false);
FormBorderStyle = FormBorderStyle.None;
StartPosition = FormStartPosition.Manual;
ShowInTaskbar = false;
Visible = false;
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= WS_POPUP | WS_BORDER;
cp.ExStyle |= WS_EX_TOPMOST | WS_EX_NOACTIVATE;
return cp;
}
}
protected override bool ShowWithoutActivation
{
get { return true; }
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEACTIVATE)
{
OnClick(EventArgs.Empty);
m.Result = (IntPtr)MA_NOACTIVATEANDEAT;
}
else
base.WndProc(ref m);
}
public new void Show()
{
Windows.SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE);
base.Show();
}
}

Remove the Icon Double-click close feature in WinForms?

I would like to keep the icon on my program, but would like to remove the double-click close
feature. Is there any way of doing this?
I have not found anything about this on google tho.
You can do this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Size iconSize = new Size(32,32);
Rectangle R = new Rectangle(this.Location, iconSize);
if (R.Contains(Cursor.Position) && e.CloseReason == CloseReason.UserClosing)
e.Cancel = true;
}
Here are two options. The side effect is that the window's top right X (close button) is disabled on Windows 8. I believe on windows XP it is removed completely.
Option 1: Override the Form's OnHandleCreated:
[DllImport("user32.dll")]
public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
public static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("user32.dll")]
private static extern bool RemoveMenu(IntPtr hMenu, int uPosition, uint uFlags);
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
const uint MF_BYPOSITION = 0x00000400;
IntPtr hMenu = GetSystemMenu(this.Handle, false);
int n = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, n-1, MF_BYPOSITION); // remove last (always close?)
}
Option 2: override the Form's CreateParams. The advantage of this one is that it does the work of also removing the separator.
// this code will hide the close X button, since there is no CloseBox Form property
protected override CreateParams CreateParams {
get {
const int CS_NOCLOSE = 0x0200;
CreateParams param = base.CreateParams;
param.ClassStyle = param.ClassStyle | CS_NOCLOSE;
return param;
}
}

Moving Form without title bar

I have a windows form without title bar. I want to drag it by mouse. After searching on internet, I found this code for moving form:
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x84:
base.WndProc(ref m);
if ((int)m.Result == 0x1)
m.Result = (IntPtr)0x2;
return;
}
base.WndProc(ref m);
}
But it has a problem: It operates only on form regions which are not covered by any control. For example if I use label or group box, I can't move form by clicking on them.
How can I solve this problem?
One way is to implement IMessageFilter like this.
public class MyForm : Form, IMessageFilter
{
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
public const int WM_LBUTTONDOWN = 0x0201;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private HashSet<Control> controlsToMove = new HashSet<Control>();
public MyForm()
{
Application.AddMessageFilter(this);
controlsToMove.Add(this);
controlsToMove.Add(this.myLabel);//Add whatever controls here you want to move the form when it is clicked and dragged
}
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN &&
controlsToMove.Contains(Control.FromHandle(m.HWnd)))
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
return true;
}
return false;
}
}
This is basically what you are looking to do:
Make a borderless form movable?
You might be able to add the same code to the mouse down event of other controls on your form to accomplish the same thing.
Make a borderless form movable?
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
When the user presses the mouse down over the lblMoveForm Label in the form, the following event handler executes.
// On left button, let the user drag the form.
private void lblMoveForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Release the mouse capture started by the mouse down.
lblMoveForm.Capture = false; //select control
// Create and send a WM_NCLBUTTONDOWN message.
const int WM_NCLBUTTONDOWN = 0x00A1;
const int HTCAPTION = 2;
Message msg =
Message.Create(this.Handle, WM_NCLBUTTONDOWN,
new IntPtr(HTCAPTION), IntPtr.Zero);
this.DefWndProc(ref msg);
}
}
//For base form moving
private const int HT_CAPTION = 0x2;
private const int WM_NCHITTEST = 0x84;
private const int HT_CLIENT = 0x1;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
m.Result = (IntPtr)(HT_CAPTION);
}
//For any component you also want use to move form
private bool arrastando= false;
private Point pontoinicial= new Point(0,0);
private Point meu_offset;
//use this method for component event called MouseDown
private void dragMouseDown(object sender, MouseEventArgs e)
{
arrastando = true;
pontoinicial = new Point(e.X, e.Y);
}
//use this method for component event called MouseUp
private void dragMouseUp(object sender, MouseEventArgs e)
{
arrastando=false;
}
//use this method for component event called MouseMove
private void dragMouseMove(object sender, MouseEventArgs e)
{
if (arrastando)
{
Point p = PointToScreen(e.Location);
ActiveForm.Location = new Point(p.X - this.pontoinicial.X,
p.Y - this.pontoinicial.Y);
}
}

Application stuck in full screen?

To reproduce my problem please do the following:
Create a new Windows Form Application in C#.
In the Properties window of Form1 set FormBorderStyle to None.
Launch program and press Windows+Up.
Now you are stuck in full screen.
In the default FormBorderStyle setting the MaximizeBox property to false will disable the Windows+Up fullscreen shortcut.
If the FormBorderStyle is set to None Microsoft decided it would be a good idea to disable all the Windows+Arrow key shortcuts except for the up arrow and then disable the disabling of the MaximizeBox property.
Is this a glitch? Any simple way to disable this shortcut function the selfsame way it is disabled on all the other FormBorderStyles?
Windows does this by calling SetWindowPos() to change the position and size of the window. A window can be notified about this by listening for the WM_WINDOWPOSCHANGING message and override the settings. Lots of things you can do, like still giving the operation a meaning by adjusting the size and position to your liking. You completely prevent it by turning on the NOSIZE and NOMOVE flags.
Paste this code into your form:
private bool AllowWindowChange;
private struct WINDOWPOS {
public IntPtr hwnd, hwndInsertAfter;
public int x, y, cx, cy;
public int flags;
}
protected override void WndProc(ref Message m) {
// Trap WM_WINDOWPOSCHANGING
if (m.Msg == 0x46 && !AllowWindowChange) {
var wpos = (WINDOWPOS)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS));
wpos.flags |= 0x03; // Turn on SWP_NOSIZE | SWP_NOMOVE
System.Runtime.InteropServices.Marshal.StructureToPtr(wpos, m.LParam, false);
}
base.WndProc(ref m);
}
When you want to change the window yourself, simply set the AllowWindowChange field temporarily to true.
Trap the WM_GETMINMAXINFO message which will allow you to specify the maximized size and location of your form. Technically your form will still change state to Maximized, but it will appear the same since we specify the maximized size/position to be the same as the normal state of the form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public struct POINTAPI
{
public Int32 X;
public Int32 Y;
}
public struct MINMAXINFO
{
public POINTAPI ptReserved;
public POINTAPI ptMaxSize;
public POINTAPI ptMaxPosition;
public POINTAPI ptMinTrackSize;
public POINTAPI ptMaxTrackSize;
}
public const Int32 WM_GETMINMAXINFO = 0x24;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_GETMINMAXINFO:
MINMAXINFO mmi = (MINMAXINFO)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
mmi.ptMaxSize.X = this.Width;
mmi.ptMaxSize.Y = this.Height;
mmi.ptMaxPosition.X = this.Location.X;
mmi.ptMaxPosition.Y = this.Location.Y;
System.Runtime.InteropServices.Marshal.StructureToPtr(mmi, m.LParam, true);
break;
}
base.WndProc(ref m);
}
}
Overriding the ProcessCmdKey (protected method in Form) explicitly allow us to apply custom hook and can be used in your scenario. This essentially allow us to override built-in keystroke handling.
Note: Following example demonstrate the idea of how to handle different keystroke or combination of it. Now, you probably need to fine tune the following code to work inline with your scenario. Eg: Ideally changing the FormBorderStyle or Form Size when user press the LWin+Up arrow.
public partial class Form1 : Form
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.LWin | Keys.Up))//Left windows key + up arrow
{
FormBorderStyle = FormBorderStyle.FixedDialog;
return true;
}
if (keyData == Keys.Escape) //Form will call its close method when we click Escape.
Close();
return base.ProcessCmdKey(ref msg, keyData);
}
}
Updated on How to disable windows Key in your case Lwin or RWin
public partial class Form1 : Form
{
// Structure contain information about low-level keyboard input event
[StructLayout(LayoutKind.Sequential)]
private struct KBDLLHOOKSTRUCT
{
public Keys key;
public int scanCode;
public int flags;
public int time;
public IntPtr extra;
}
//System level functions to be used for hook and unhook keyboard input
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hook);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern short GetAsyncKeyState(Keys key);
//Declaring Global objects
private IntPtr ptrHook;
private LowLevelKeyboardProc objKeyboardProcess;
public Form1()
{
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
InitializeComponent();
}
private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
{
if (nCode >= 0)
{
KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));
if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin) // Disabling Windows keys
{
return (IntPtr)1;
}
}
return CallNextHookEx(ptrHook, nCode, wp, lp);
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}
}
Check this solution - it removes Maximize/Minimize/Titlebar/Border by API calls.
public partial class Form1 : Form
{
// import necessary API functions to get and set Windows styles for P/Invoke
[DllImport("user32.dll")]
internal extern static int SetWindowLong(IntPtr hwnd, int index, int value);
[DllImport("user32.dll")]
internal extern static int GetWindowLong(IntPtr hwnd, int index);
// define constants like they are named in SDK in order to make source more readable
const int GWL_STYLE = -16;
const int GWL_EXSTYLE = -20;
const int WS_MINIMIZEBOX = 0x00020000;
const int WS_MAXIMIZEBOX = 0x00010000;
const int WS_CAPTION = 0x00C00000;
const int WS_THICKFRAME = 0x00040000;
const int WS_EX_DLGMODALFRAME = 0x00000001;
const int WS_EX_CLIENTEDGE = 0x00000200;
const int WS_EX_STATICEDGE = 0x00020000;
// this replaces MinimizeBox=false and MaximizeBox=false
void HideMinimizeAndMaximizeButtons()
{
// read current style
int style = GetWindowLong(Handle, GWL_STYLE);
Debug.WriteLine("0x{0:X}", style);
// update style - remove flags for MinimizeBox and MaximizeBox
style = style & ~WS_MINIMIZEBOX & ~WS_MAXIMIZEBOX;
Debug.WriteLine("0x{0:X}", style);
SetWindowLong(Handle, GWL_STYLE, style);
}
// part of removing the whole border
void HideTitleBar()
{
// read current style
int style = GetWindowLong(Handle, GWL_STYLE);
Debug.WriteLine("0x{0:X}", style);
// update style - remove flag for caption
style = style & ~WS_CAPTION;
Debug.WriteLine("0x{0:X}", style);
SetWindowLong(Handle, GWL_STYLE, style);
}
// hide the border
void HideBorder()
{
// read current style
int style = GetWindowLong(Handle, GWL_STYLE);
Debug.WriteLine("0x{0:X}", style);
// update style - remove flag for border (could use WS_SIZEBOX which is the very same flag (see MSDN)
style = style & ~WS_THICKFRAME;
Debug.WriteLine("0x{0:X}", style);
SetWindowLong(Handle, GWL_STYLE, style);
// read current extended style
style = GetWindowLong(Handle, GWL_EXSTYLE);
Debug.WriteLine("0x{0:X}", style);
// update style by removing some additional border styles -
// may not be necessary, when current border style is not something exotic,
// i.e. as long as it "normal"
style = style & ~WS_EX_DLGMODALFRAME & ~WS_EX_CLIENTEDGE & ~WS_EX_STATICEDGE;
Debug.WriteLine("0x{0:X}", style);
SetWindowLong(Handle, GWL_EXSTYLE, style);
}
public Form1()
{
InitializeComponent();
// hide those unwanted properties - you can try to leave out one or another to see what it does
HideMinimizeAndMaximizeButtons();
HideTitleBar();
HideBorder();
}
}
This works as intended. Maximizing/minimizing by setting WindowState works as well.
One could analyze in sources what the framework does and what it does "wrong" (or not quite correct).
Edit: I added debug output of the style values. Please try this sequence of commands in Form1 constructor:
MaximizeBox = false;
FormBorderStyle = FormBorderStyle.Sizable;
HideMinimizeAndMaximizeButtons();
FormBorderStyle = FormBorderStyle.None;
MaximizeBox = true;
MaximizeBox = false;
HideMinimizeAndMaximizeButtons();
FormBorderStyle = FormBorderStyle.None;
HideMinimizeAndMaximizeButtons();
You'll see, that setting FormBorderStyle.None enables the WS_MAXIMIZEBOX style. This cannot be "corrected" by another MaximizeBox = false. It seems it's necessary to call API functions.

Opening a WinForms Form with TopMost = true but not having it steal focus?

I have a form that pops up on a user's screen and has TopMost=true, but it steals the focus. How can I get it to not steal focus when it first appears?
This is what worked for me. It provides TopMost but without focus-stealing.
protected override bool ShowWithoutActivation
{
get { return true; }
}
private const int WS_EX_TOPMOST = 0x00000008;
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= WS_EX_TOPMOST;
return createParams;
}
}
Remember to omit setting TopMost in Visual Studio designer, or elsewhere.
This is stolen, err, borrowed, from here (click on Workarounds):
https://connect.microsoft.com/VisualStudio/feedback/details/401311/showwithoutactivation-is-not-supported-with-topmost
Paste this code in your form:
protected override bool ShowWithoutActivation
{
get { return true; }
}
You can set:
this.TopMost = True;
on Load event of that form.
It's OK with me!
You can do it like this:
private const int SW_SHOWNOACTIVATE = 4;
private const int HWND_TOPMOST = -1;
private const uint SWP_NOACTIVATE = 0x0010;
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(
int hWnd, // Window handle
int hWndInsertAfter, // Placement-order handle
int X, // Horizontal position
int Y, // Vertical position
int cx, // Width
int cy, // Height
uint uFlags); // Window positioning flags
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow);
public static void ShowInactiveTopmost(System.Windows.Forms.Form frm)
{
try
{
ShowWindow(frm.Handle, SW_SHOWNOACTIVATE);
SetWindowPos(frm.Handle.ToInt32(), HWND_TOPMOST,
frm.Left, frm.Top, frm.Width, frm.Height,
SWP_NOACTIVATE);
}
catch (System.Exception ex)
{
// error handling
}
}
I tested the below code using a timer on form1 to instantiate and show form2 with form1 as owner.
In form2's Shown event I then set focus to the owner, which is the current active form.
I have a textbox on form1 and was able to continuesly write in the textbox without loosing focus during this process.
My timer code in form1:
private void timer1_Tick(object sender, EventArgs e)
{
Form2 popup = new Form2();
popup.TopMost = true;
popup.Show(this);
timer1.Enabled = false;
}
My code in the Shown event of form2:
private void Form2_Shown(object sender, EventArgs e)
{
this.Owner.Focus();
}
You can do this or simply set TopMost to false and use the override of ShowWithoutActivation as Hans Passant stated.
Edit: (Or use p/invoke as seen in Hans Passant's additional comment I missed while I wrote this)
I came across the same problem. I'm not using C# but C++. I figure this could be useful anyways:
Using windows.h:
BOOL WINAPI SetWindowPos(
__in HWND hWnd,
__in_opt HWND hWndInsertAfter,
__in int X,
__in int Y,
__in int cx,
__in int cy,
__in UINT uFlags
);
Passing the flag SWP_NOACTIVATE to the uFlags argument worked for me.
Instead of writing .setfocus()in _activated event; write it to .shown event of the form.

Categories