当前位置:网站首页>对话框管理器第三章:创建控件
对话框管理器第三章:创建控件
2022-07-27 13:56:00 【拓扑梅尔-漫漫开发路】
和上一篇创建框架窗口相比,创建控件的工作要轻松很多,信不信由你。
在对话框模板中定义的每一个控件,其对应的子窗口将会被创建。控件的大小和位置在模板中是以DLU为单位来描述的,所以,理所当然的,我们需要将它们转换为以像素单位,代码如下:
int x = XDLU2Pix(ItemTemplate.x);
int y = YDLU2Pix(ItemTemplate.y);
int cx = XDLU2Pix(ItemTemplate.cx);
int cy = YDLU2Pix(ItemTemplate.cy);
控件类名和标题也来自于模板。还有一些可选的额外字节pExtra没有人使用,但由于历史原因保留在模板定义中。 一旦收集了这些信息,就该制作”甜甜圈”了。
HWND hwndChild = CreateWindowEx(
ItemTemplate.dwExStyle | WS_EX_NOPARENTNOTIFY,
pszClass, pwzCaption, ItemTemplate.dwStyle,
x, y, cx, cy, hdlg, ItemTemplate.dwId,
hinst, pExtra);
请注意,对于对话框控件,WS_EX_NOPARENTNOTIFY 样式是默认启用的。
下一部分经常让人栽跟头。 “当我尝试创建我的对话框时,它失败了,我不知道为什么。” 这可能是因为无法创建对话框上的某个控件,通常是因为你忘记为该控件注册窗口类。 (例如,您忘记调用 InitCommonControlsEx 函数或忘记 LoadLibrary 适当版本的 RichEdit 控件。)
if (!hwndChild) {
DestroyWindow(hdlg);
return NULL;
}
DS_NOFAILCREATE 样式禁止了上述失败检查。
但是,如果确实创建了控件,则需要对其进行初始化。
SetWindowContextHelpId(hwndChild, ItemTemplate.dwHelpID);
SetWindowFont(hwndChild, hf, FALSE);
对模板中的每个控件重复上面的步骤后,你现在拥有一个包含所有子控件的对话框。 告诉对话过程它可以初始化它的子窗口,如果我们在构建框架时延迟了 WS_VISIBLE 位,则显示(现在准备好的)对话框,并将对话框返回给我们的调用者,此时对话框已准备就绪了。
// The default focus is the first item that is a valid tab-stop.
HWND hwndDefaultFocus = GetNextDlgTabItem(hdlg, NULL, FALSE);
if (SendMessage(hdlg, WM_INITDIALOG, hwndDefaultFocus, lParam)) {
SetDialogFocus(hwndDefaultFocus);
}
if (fWasVisible) ShowWindow(hdlg);
return hdlg;
}
关于SetDialogFocus,我们之前讲过。
至此,你应该明白了对话框是如何创建了。
(实际上,现实情况要复杂得多,因为我跳过了所有应用程序兼容性技巧!例如,有一个程序依赖于细节的设置和WS_BORDER样式的缺失来决定控件是组合框还是列表框 . 我猜GetClassName函数工作量太大了?)
我希望这可以帮助你更好地理解对话模板是如何工作的。
总结
描述性语言,是程序开发者和操作系统之间的一种十分友好的沟通方式。
就好比COM时代的IDL(接口描述语言)。
最后
Raymond Chen的《The Old New Thing》是我非常喜欢的博客之一,里面有很多关于Windows的小知识,对于广大Windows平台开发者来说,确实十分有帮助。
本文来自:《The dialog manager, part 3: Creating the controls》

边栏推荐
- [ManageEngine] what is Siem
- C language layered understanding (C language array)
- Docker实践经验:Docker 上部署 mysql8 主从复制
- What if win11 wallpaper turns black? The solution of win11 wallpaper blackening
- STM32F103C8T6在Arduino框架下驱动SH1106 1.3“ IIC OLED显示
- Construction of knowledge map of financial securities and discovery of related stocks from the perspective of knowledge relevance
- Redis
- TXT把换行 替换为空格或者取消换行
- 在Oracle VirtualBox中导入Kali Linux官方制作的虚拟机
- STM32 - capacitive touch button experiment
猜你喜欢

Airport cloud business sign analysis
Database storage series (1) column storage

视觉系统设计实例(halcon-winform)-9.文字显示

动态规划——股票买卖5

C语言基础知识梳理总结

Understand JS execution context in an article

Docker practical experience: deploy mysql8 master-slave replication on docker

大家最想要的,最全的C语言知识点总结,还不赶紧学习

Graphic SQL of giant image

Navicate reports an error access violation at address 00000000
随机推荐
线程知识总结
【WORK】关于技术架构
股票买卖4
Chinese character style transfer --- antagonistic discriminative domain adaptation (L1)
Regular expressions: mailbox matching
SkyWalking分布式系统应用程序性能监控工具-中
c语言分层理解(c语言数组)
2022 Niuke multi School II_ E I
DXGI acquisition process
Unityui aspect processing (induction and accumulation)
Tencent two sides: @bean and @component are used in the same class, what will happen?
Construction of knowledge map of financial securities and discovery of related stocks from the perspective of knowledge relevance
codeforces 1708E - DFS Trees
MySQL save data prompt: out of range value for column error
Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]
lc marathon 7.26
STM32——电容触摸按键实验
What if win11 wallpaper turns black? The solution of win11 wallpaper blackening
NEFU118 n! How many zeros are there after [basic theorem of arithmetic]
炒黄金开户平台有没有正规,安全的