TONT 40303 为什么对话框最初创建时是隐藏的?

客户只要结果,所以我们把过程藏起来了。

原文链接:https://blogs.msdn.microsoft.com/oldnewthing/20040311-00/?p=40303

You may not have noticed it until you looked closely, but dialog boxes are actually created hidden initially, even if you specify WS_VISIBLE in the template. The reason for this is historical.

不注意看的话可能都注意不到,不过对话框刚刚创建时是整体隐藏的,就算在窗体模板里指定WS_VISIBLE也一样。这么做有其历史原因。

Rewind back to the old days (we’re talking Windows 1.0), graphics cards are slow and CPUs are slow and memory is slow. You can pick a menu option that displays a dialog and wait a second or two for the dialog to get loaded off the floppy disk. (Hard drives are for the rich kids.) And then you have to wait for the dialog box to paint.

回到很久很久以前(这里指的是Windows 1.0的时候)(译注:1985年),显卡(译注:那时候最多叫显示适配器,与现在的显卡不完全是一个概念,例如并没有任何3D能力可言)速度很慢,CPU速度很慢,内存的速度也很慢。点一下会弹出对话框的菜单项,需要等个几秒钟来让这个对话框(的数据)从软盘上加载出来。(那时候硬盘是土豪才玩得起的东西。)接下来还得等这个对话框在屏幕上描绘出来。

To save valuable seconds, dialog boxes are created initially hidden and all typeahead is processed while the dialog stays hidden. Only after the typeahead is finished is the dialog box finally shown. And if you typed far ahead enough and hit Enter, you might even have been able to finish the entire dialog box without it ever being shown! Now that’s efficiency.

为了节省这宝贵的几秒钟,对话框在刚刚创建时是隐藏的,并且所有在它显示出来之前的输入都会被接受并处理,只有在这些『提前输入』结束后,对话框才会最终显示出来。如果你速度够快,把要输入的东西都搞定了然后敲了个回车键,甚至还有可能在这个对话框显示出来之前就把所有的事搞定呢!这可真是太有效率了。

Of course, nowadays, programs are stored on hard drives and you can’t (normally) out-type a hard drive, so this optimization is largely wasted, but the behavior remains for compatibility reasons.

当然现如今,程序都存储在硬盘上(译注:本文写于2004年,那时候固态硬盘还没有普及),(通常情况下)用户的操作是没有办法比从硬盘加载的速度还快的,所以这项优化的意义就不大了,但出于兼容性的目的仍旧保留着。

Actually this behavior still serves a useful purpose: If the dialog were initially created visible, then the user would be able to see all the controls being created into it, and watch as WM_INITDIALOG ran (changing default values, hiding and showing controls, moving controls around…) This is both ugly and distracting. (“How come the box comes up checked, then suddenly unchecks itself before I can click on it?”)

实际上,这项设计仍旧具有其积极意义:如果一个对话框在刚刚创建时就可见,那么用户就会眼见着窗体里的控件一个一个创建出来,然后又看着WM_INITDIALOG的过程运行(修改默认值、隐藏或显示控件、移动控件的位置等等),这个过程既不美观也令人分心。(『那个复选框刚出来的时候是选中的,怎么我还没来得及点它就又取消选中啦?』)

Comments

  1. 现在Web开发就是,很多框架不尊重历史,先渲染对话框,再渲染内容(当年是软盘,现在是网络),导致提示文本经常跑到对话框外边。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

 剩余字数 ( Characters available )

Your comment will be available after auditing.
您的评论将在通过审核后显示。

Please DO NOT add any links in your comment, otherwise it would be identified as SPAM automatically and never be audited.
请不要在评论中插入任何链接,否则将被自动归类为垃圾评论,且永远不会被提交给博主进行复审。

*