English 中文(简体)
如何绘制控件,而不绘制控件。
原标题:How to draw a control, without drawing the control

我有一个 UserControl 对象列表;当点击菜单选项转到应用程序的另一个部分时,它会执行以下操作:

  1. Set the currently displayed usercontrol to visible
  2. Clear the main panel s Controls list
  3. New up the requested control (if not already created in the list)
  4. Add the new control to the main panels Controls list
  5. Dock the new control to fill
  6. Set the new control to visible

我这么做是因为之前应用程序的所有区域都不是用户控件,它们只是TabControl中的控件集合。底层代码非常庞大(frmMain中的代码超过13000行)。

它工作得很好。所有控件(之前每个选项卡页可能都有的)都管理自己的行为,每个控件都有着很小的代码占用。我可以拥有一个性感的菜单。一切都很好。

然而,显然存在问题。以前,选项卡控件会在启动时加载,并呈现所有选项卡(但没有被看到)。其中一个标签页上有一个调度控件,需要大约3-4秒钟才能绘制并调整其布局。现在,控件被加载并显示时,它们会根据需要显示,首次查看此特定控件时,您会看到它加载并调整其大小/格式等。这几乎不令人愉悦。

我的问题是,我该如何加载和呈现此控件,以便它能够呈现并找出其布局,但不实际向用户显示?

我试过一些简单的事情,比如:

ScheduleWindow scheduler = new ScheduleWindow() { Visible = false; }
mainPanel.Controls.Add(scheduler);
scheduler.Visible = true;

我尝试了在独立线程上创建和加载控件(遇到了很多困难),但仍然没有任何作用...

有没有办法实现我想要的东西?

最佳回答

尝试在主窗体上使用SuspendLayout()和ResumeLayout()方法?当您调用SuspendLayout()时,您可以对添加的控件进行任何需要的操作,例如停靠、调整大小等。当您调用ResumeLayout()时,所有更改将立即应用,并应该立即发生。

另一种做法是,一些控制办法有“开始”和“最后”方法,暂停进行这种控制,允许你从事繁重的工作(例如,在名单上添加数千件物品),而没有看到最新情况。 这还改善了业绩,因为每次改变控制时,都不会再进行重整,而是会改变控制,然后重整一次。

问题回答

Try calls scheduler.Handle.ToString() and/or scheduler.Refresh(.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签