期间 {@ 1}}的构造函数触及特定属性(即Form
属性)时,它会立即调用Width
。我只在运行Windows Embedded 7的设备和随附的.NET 3.5上注意到这种行为。
以下是我用来重现问题的代码:
OnActivated()
(请注意,我不是static class Program
{
[MTAThread]
static void Main()
{
new MyForm();
}
}
public class MyForm : Form
{
public MyForm()
{
Width = 100;
}
protected override void OnActivated(EventArgs e)
{
MessageBox.Show("Activated!");
}
}
表单,也不是Show()
)
在我的桌面或WinCE 5设备上运行上述代码不会显示Application.Run()
。在我的Windows Embedded 7设备上,出现MessageBox
,点击“确定”后再次显示,因为再次调用MessageBox
。
设备制造商告诉我这是一个特定于操作系统的问题,任何人都可以确认这是Windows Embedded 7应该如何“对待他的表单”吗?
答案 0 :(得分:2)
我在使用.NET CF 3.5的Windows CE 5.0设备上尝试过此操作,看看我是否遇到了与您相同的行为。使用完全相同的代码,我没有OnActivated得到调用。即使使用Application.Run(),设置Width也不会导致调用OnActivated。事实上,似乎必须显示Form以允许Activate触发,因为我尝试显式调用Activate()。
因此,它似乎是Windows Embedded 7与以前版本的Windows CE相比所做的事情。我还尝试使用Reflector查看CompactFramework .NET .dll,但是我没有在.NET中找到任何代码,当它的宽度发生变化时会激活一个Form。 Width不是Form的属性(它是Control的属性),并且Form似乎无法以任何方式处理resize事件,从而允许它调用其Activate方法。
我确实有一个建议,作为一种可能的解决方法。即使我明确调用了Activate();
,以下代码也阻止了Activate完全出现尝试一下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DisableActivate();
Activate(); // No call is made to OnActivated
EnableActivate();
}
protected override void OnActivated(EventArgs e)
{
Debug.WriteLine("Activated");
}
private void DisableActivate()
{
WindowStyles style = GetWindowLong(Handle, GWL_EX_STYLE);
style |= WindowStyles.WS_EX_NOACTIVATE;
SetWindowLong(Handle, GWL_EX_STYLE, style);
}
private void EnableActivate()
{
WindowStyles style = GetWindowLong(Handle, GWL_EX_STYLE);
style &= ~WindowStyles.WS_EX_NOACTIVATE;
SetWindowLong(Handle, GWL_EX_STYLE, style);
}
public const int GWL_EX_STYLE = -20;
[DllImport("coredll.dll", SetLastError = true)]
public static extern WindowStyles GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("Coredll.dll", SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, WindowStyles dwNewLong);
[Flags]
public enum WindowStyles : uint
{
WS_CHILD = 0x40000000,
WS_VISIBLE = 0x10000000,
WS_DISABLED = 0x08000000,
WS_GROUP = 0x00020000,
WS_TABSTOP = 0x00010000,
WS_OVERLAPPED = WS_BORDER | WS_CAPTION,
WS_CLIPSIBLINGS = 0x04000000,
WS_CLIPCHILDREN = 0x02000000,
WS_CAPTION = 0x00C00000, /* WS_BORDER | WS_DLGFRAME */
WS_BORDER = 0x00800000,
WS_DLGFRAME = 0x00400000,
WS_VSCROLL = 0x00200000,
WS_HSCROLL = 0x00100000,
WS_SYSMENU = 0x00080000,
WS_THICKFRAME = 0x00040000,
WS_MAXIMIZEBOX = 0x00020000,
WS_MINIMIZEBOX = 0x00010000,
WS_SIZEBOX = WS_THICKFRAME,
WS_POPUP = 0x80000000,
WS_EX_NOACTIVATE = 0x08000000,
WS_EX_DLGMODALFRAME = 0x00000001,
WS_EX_TOPMOST = 0x00000008,
WS_EX_TOOLWINDOW = 0x00000080,
WS_EX_WINDOWEDGE = 0x00000100,
WS_EX_CLIENTEDGE = 0x00000200,
WS_EX_CONTEXTHELP = 0x00000400,
WS_EX_RIGHT = 0x00001000,
WS_EX_RTLREADING = 0x00002000,
WS_EX_LEFTSCROLLBAR = 0x00004000,
WS_EX_STATICEDGE = 0x00020000,
WS_EX_NOINHERITLAYOUT = 0x00100000, // Disable inheritence of mirroring by children
WS_EX_LAYOUTRTL = 0x00400000, // Right to left mirroring
WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE),
WS_EX_CAPTIONOKBTN = 0x80000000,
WS_EX_NODRAG = 0x40000000,
WS_EX_ABOVESTARTUP = 0x20000000,
WS_EX_INK = 0x10000000,
WS_EX_NOANIMATION = 0x04000000
}
}
所以基本上,用你的代码做:
public MyForm()
{
DisableActivate();
Width = 100;
EnableActivate();
}