我希望在有大量窗口和控件的Win32项目中改进我的代码和文件结构。目前,我倾向于为整个窗口或对话框的实现使用一个头文件和一个源文件。这对于小型项目来说效果很好,但现在这些实现已经达到了1000-2000行,浏览起来非常繁琐。
我的典型源文件如下:
static LRESULT CALLBACK on_create(const HWND hwnd, WPARAM wp, LPARAM lp) {
setup_menu(hwnd);
setup_list(hwnd);
setup_context_menu(hwnd);
/* clip */
return 0;
}
static LRESULT CALLBACK on_notify(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
const NMHDR* header = (const NMHDR*)lp;
/* At this point I feel that the control's event handlers doesn't
* necessarily belong in the same source file. Perhaps I could move
* each control's creation code and event handlers into a separate
* source file? Good practice or cause of confusion? */
switch (header->idFrom) {
case IDC_WINDOW_LIST:
switch (header->code) {
case NM_RCLICK:
return on_window_list_right_click(hwnd, wp, lp);
/* clip */
}
}
}
static LRESULT CALLBACK wndmain_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg) {
case WM_CREATE:
return on_create(hwnd, wp, lp);
case WM_CLOSE:
return on_close(hwnd, wp, lp);
case WM_NOTIFY:
return on_notify(hwnd, wp, lp);
/* It doesn't matter much how the window proc looks as it just forwards
* events to the appropriate handler. */
/* clip */
default:
return DefWindowProc(hwnd, msg, wp, lp);
}
}
但现在窗口有更多的控件,这些控件又有它们自己的消息处理程序,还有菜单点击处理程序等等...我感到很困惑,我真的需要关于如何以良好和合理的方式组织这个混乱的建议。
我尝试找到良好的开源Win32代码结构示例,但我只是变得更加困惑,因为有数百个文件,而在这些与GUI相关的文件中,Win32 GUI代码似乎被封装得很远。当我最终找到一个CreateWindowEx语句时,窗口过程无处可寻。
任何有关如何组织所有代码并保持清醒的建议都将不胜感激。
谢谢!
我不希望使用任何库或框架,因为我认为Win32 API非常有趣且有价值,可以用来学习。
你如何组织自己的GUI代码的任何见解都可能会成为灵感来源。