Hi Lance,
Yes, you're right. When a form's border style is set to None, the window
menu won't appear when you right click the form's taskbar button.
I use Spy++ to monitor the Windows messages that a form receives when I
right click the form's taskbar button. I notice that the form receives the
following messages in sequence:
WM_NCACTIVATE fActive:False
WM_KILLFOCUS
WM_NCACTIVATE fActive:True
WM_SETFOCUS
WM_ENTERMENULOOP
WM_INITMENU
WM_INITMENUPOPUP
So my initial thought is to set the form's FormBorderStyle property to
Sizable when the form receives the WM_NCACTIVATE Windows message(when the
fActive=false) and restore the FormBorderStyle property to None when the
form receives the WM_INITMENUPOPUP Windows message. The following is the
code:
public partial class Form1 : Form
{
int WM_NCACTIVATE = 0x0086;
int WM_INITMENUPOPUP = 0x0117;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCACTIVATE)
{
if (m.WParam.ToInt32() == 0)
{
this.FormBorderStyle = FormBorderStyle.Sizable;
}
}
else if (m.Msg == WM_INITMENUPOPUP)
{
this.FormBorderStyle = FormBorderStyle.None;
}
base.WndProc(ref m);
}
}
When I right click the form's taskbar button, the window menu does appear.
But the problem is that when I switch to another program running on my
computer, the default form border appears.
Actually, it's hard to distinguish whether it is the user right clicks the
form's taskbar button or switch to another program on the computer when the
form receives the WM_NCACTIVATE message.
Alternatively, I suggest that you create your own ContextMenuStrip to mimic
the window menu and show it when the form receives the WM_NCACTIVATE
message. The following is a sample.
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
[DllImport("user32.dll")]
private extern static bool GetCursorPos(ref Point lpPoint);
int WM_NCACTIVATE = 0x0086;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCACTIVATE)
{
if (m.WParam.ToInt32() == 0)
{
Point pt = new Point();
GetCursorPos(ref pt);
this.contextMenuStrip1.Show(pt);
}
}
base.WndProc(ref m);
}
}
As for displaying custom text in the form's taskbar button, you could set
the form's Text property to any text you want and the text will be shown in
the taskbar button.
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.