我开发的是影院的票务系统,显示剧场的座位情况是用一个按钮控件来表示的。现在的问题是,座位有很多,就会创建很多很多的按钮,结果,在几天门票的视图切换时,就会出现创建窗口句柄的错误提示。请问有什么方法可以解决吗?

解决方案 »

  1.   

    為什么要用Button,你用Button代表座位?不看代碼不知道你說的是什么情況。或者說詳細點
      

  2.   

    window的窗口句柄数量是由限制的,最好用图片来模拟
      

  3.   


    这个限制可以忽略..我记得win95时代好像是65535或者更多,记不清,后来的windows几乎没有什么限制,应该是你代码的问题
      

  4.   

    我用的是Button. 因为Button有些事件处理。不用自己的处理这些事件。
    ************** 异常文本 **************
    System.ComponentModel.Win32Exception: 创建窗口句柄时出错。
       在 System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
       在 System.Windows.Forms.Control.CreateHandle()
       在 System.Windows.Forms.Control.get_Handle()
       在 System.Windows.Forms.Control.CreateGraphicsInternal()
       在 System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
       在 System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t)
       在 System.Windows.Forms.Control.WndProcException(Exception e)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e)
       在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       在 System.Windows.Forms.Form.ShowDialog()
       在 AuwaTicket.Client.SellManagement.Business.frmBookingOrderDetail.btnSelTicket_Click(Object sender, EventArgs e) 位置 F:\DavidLv\AuwaTicket\AuwaTicket.Client.SellManagement\Business\frmBookingOrderDetail.cs:行号 521
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 DevExpress.XtraEditors.BaseButton.OnClick(EventArgs e)
       在 DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    ************** 已加载的程序集 **************
    mscorlib
        程序集版本: 2.0.0.0
        Win32 版本: 2.0.50727.832 (QFE.050727-8300)
        基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    AuwaTicket.Client.SellManagement
        程序集版本: 1.0.0.0
        Win32 版本: 1.0.0.0
        基本代码: file:///F:/DavidLv/AuwaTicket/AuwaTicket.Client.SellManagement/bin/Debug/AuwaTicket.Client.SellManagement.exe
    ----------------------------------------
    System.Windows.Forms
        程序集版本: 2.0.0.0
        Win32 版本: 2.0.50727.832 (QFE.050727-8300)
        基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
      

  5.   

    不好意思,更正一下楼上的说法。
    可以在注册表上查HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\
         CurrentVersion\Windows\USERProcessHandleQuota
    win2k,XP默认是10000个,2003没查。
    最大支持应该是65535个,但是MSDN上又说该注册表项的设置范围为200至18000个。
      

  6.   

    System.ComponentModel.Win32Exception: 创建窗口句柄时出错。 
       在 System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp) 
    ------------------------------------
    不过楼主的问题,从理论上来说,10000个应该是足够了。
    至于出错,出错消息是.net内部代码的问题,而非用户代码,我想问题主要是LZ的程序中使用了过多的控件资源。
    可以考虑从以下几个方法来解决:
    1. 改用GDI绘图代替Button.
    2. 即使一定要使用Button,也可以在程序动态加载Button并及时释放资源,或者创建一定数量的Button重复利用,实话说,一个窗体同一时间能够显示多少个Button?
    有一千个座位,就要创建1000个Button?创建1000个Button肯定是不能一起显示的,大多数的Button都是在无谓浪费消耗大量系统资源,如内存的分配和系统的消息处理。即使操作系统没有限制句柄数量,也不应该这样做。
      

  7.   

    就好像win32可以访问4G的内存,但你不能把你的程序写成4g大小
      

  8.   

    谢谢。也许是太浪费内存了。呵呵。
    但GDI方式太麻烦了。要处理很多事情,本来只是一个项目用,我看还是重复使用算了。优化一下算法。
    因为剧场最多也只是3千多座位。
      

  9.   

    窗口句柄过多.
    建议不要使用Button来表示座位,而应该使用自己绘图的方法来处理。
      

  10.   

    不知道用Dispose,手动将不用的Button清除可行否?