Popup Windows on Forms

G

Guest

Hi Folks

I'm trying to create a popup window on a Form by overriding the CreateParams property of a control.
I can get all the functionality of this to work in my application but for one little problem. Thi
is that when I select something on the popup my application deactivates (the title bar goes faint)
How do I stop this from happening. Any help with this would be much appreciated

Thanks in advanc

Jon Rushwort

A simplified example of the code is as follows

using System
using System.Drawing
using System.Windows.Forms

namespace PopupExampl

public enum WindowStyles : uin

WS_POPUP = 0x8000000


public enum WindowExStyle

WS_EX_TOPMOST = 0x00000008
WS_EX_TOOLWINDOW = 0x0000008


public enum MouseActivateFlag

MA_NOACTIVATE =

public enum Msg

WM_MOUSEACTIVATE = 0x002


public class PopupList : Contro

private ListBox listBox = new ListBox()

public PopupList(

listBox.Size=new Size(100,100)
listBox.Location=new Point(0,0)
listBox.Items.AddRange(new object[]{"Bashful", "Doc", "Sleepy", "Sneezy", "Happy", "Dopey", "Grumpy"})

Controls.Add(listBox)


protected override CreateParams CreateParams

ge

CreateParams createParams = new CreateParams()

// Define the screen position and siz
createParams.X = Location.X
createParams.Y = Location.Y
createParams.Height = Size.Height
createParams.Width = Size.Width

// As a top-level window it has no paren
createParams.Parent = IntPtr.Zero

// Appear as a pop-up windo
createParams.Style = unchecked((int)WindowStyles.WS_POPUP)

createParams.ExStyle = (int)WindowExStyles.WS_EX_TOPMOST +
(int)WindowExStyles.WS_EX_TOOLWINDOW

return createParams



protected override void WndProc(ref Message m

if (m.Msg == (int)Msgs.WM_MOUSEACTIVATE

m.Result = (IntPtr)MouseActivateFlags.MA_NOACTIVATE

els

base.WndProc(ref m)




public class frmMain : System.Windows.Forms.For

private PopupList popupList = new PopupList()

public frmMain(

popupList.CreateControl()
Text="Popup Example (Click the Form)"
Click+=new EventHandler(OnClick)


private void OnClick(object sender, System.EventArgs e

popupList.Location=Cursor.Position
popupList.Size=new Size(100,100)
popupList.Show()


[STAThread
static void Main()

Application.Run(new frmMain())
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Jon,
This is the way popup windows work. They have their own identity.
Here is the message flow when Windows changes the active window
1.Windows being deactivated receives WM_ACTIVATE with WA_INACTIVE in wParams
2.Windows, which is to be activated receives WM_ACTIVATE with wParams set
either to WA_ACTIVE or
WA_CLICKACTIVE
3 If wParams has WA_CLICKACTIVE set a WM_MOUSEACTIVATE message is send to
the window that should be acivated

This message flow is synchronous if both windows are created by the same
thread.
How you can see when a window receive WM_MOUSEACTIVATE the other window
already has been deactivated.

So, you should process WM_ACTIVATE instead and set back the old active
window active again.

[DllImport("user32.dll")]
private extern static IntPtr SetActiveWindow(IntPtr handle);
private const int WM_ACTIVATE = 6;
private const int WA_INACTIVE = 0;

protected override void WndProc(ref Message m)
{
if(m.Msg == WM_ACTIVATE)
{
if(((int)m.WParam & 0xFFFF) != WA_INACTIVE)
{
if(m.LParam != IntPtr.Zero) SetActiveWindow(m.LParam);
}
}
base.WndProc(ref m);

}

There might be a managed solution for setting the active window.
You do know that if your popup window cannot be activated it and its
children won't receive any input form the mouse or keyboard, don't you?


--
HTH
B\rgds
100 [C# MVP]

Jon Rushworth said:
Hi Folks,

I'm trying to create a popup window on a Form by overriding the
CreateParams property of a control.
I can get all the functionality of this to work in my application but for one little problem. This
is that when I select something on the popup my application deactivates (the title bar goes faint).
How do I stop this from happening. Any help with this would be much appreciated.

Thanks in advance

Jon Rushworth

A simplified example of the code is as follows:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace PopupExample
{
public enum WindowStyles : uint
{
WS_POPUP = 0x80000000
}

public enum WindowExStyles
{
WS_EX_TOPMOST = 0x00000008,
WS_EX_TOOLWINDOW = 0x00000080
}

public enum MouseActivateFlags
{
MA_NOACTIVATE = 3
}
public enum Msgs
{
WM_MOUSEACTIVATE = 0x0021
}

public class PopupList : Control
{
private ListBox listBox = new ListBox();

public PopupList()
{
listBox.Size=new Size(100,100);
listBox.Location=new Point(0,0);
listBox.Items.AddRange(new object[]{"Bashful", "Doc", "Sleepy", "Sneezy", "Happy", "Dopey", "Grumpy"});

Controls.Add(listBox);
}

protected override CreateParams CreateParams
{
get
{
CreateParams createParams = new CreateParams();

// Define the screen position and size
createParams.X = Location.X;
createParams.Y = Location.Y;
createParams.Height = Size.Height;
createParams.Width = Size.Width;

// As a top-level window it has no parent
createParams.Parent = IntPtr.Zero;

// Appear as a pop-up window
createParams.Style = unchecked((int)WindowStyles.WS_POPUP);

createParams.ExStyle = (int)WindowExStyles.WS_EX_TOPMOST +
(int)WindowExStyles.WS_EX_TOOLWINDOW;

return createParams;
}
}

protected override void WndProc(ref Message m)
{
if (m.Msg == (int)Msgs.WM_MOUSEACTIVATE)
{
m.Result = (IntPtr)MouseActivateFlags.MA_NOACTIVATE;
}
else
{
base.WndProc(ref m);
}
}
}

public class frmMain : System.Windows.Forms.Form
{
private PopupList popupList = new PopupList();

public frmMain()
{
popupList.CreateControl();
Text="Popup Example (Click the Form)";
Click+=new EventHandler(OnClick);
}

private void OnClick(object sender, System.EventArgs e)
{
popupList.Location=Cursor.Position;
popupList.Size=new Size(100,100);
popupList.Show();
}

[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
}
}
 
G

Guest

Hi Stoitcho

Thankyou very much for your reply. You wouldn'
believe the number of blind alleys i've been with this one

Regard
Jon Rushworth
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Jon,
If you tell us more of what you are trying to achieve we migh help you to
come with the right solution.
 
Top