class WinSimpleClass { public: WinSimpleClass (char const * name, HINSTANCE hInst) : _name (name), _hInstance (hInst) {} WinSimpleClass (int resId, HINSTANCE hInst); char const * GetName () const { return _name.c_str (); } HINSTANCE GetInstance () const { return _hInstance; } HWND GetRunningWindow (); protected: HINSTANCE_hInstance; std::string_name; };
WinSimpleClass::WinSimpleClass (int resId, HINSTANCE hInst) : _hInstance (hInst) { ResString resStr (hInst, resId); _name = resStr; }
HWND WinSimpleClass::GetRunningWindow () { HWND hwnd = ::FindWindow (GetName (), 0); if (::IsWindow (hwnd)) { HWND hwndPopup = ::GetLastActivePopup (hwnd); if (::IsWindow (hwndPopup)) hwnd = hwndPopup; } else hwnd = 0;
return hwnd; }
--------------------------------------------------------------------------------
class WinClass: public WinSimpleClass { public: WinClass (char const * className, HINSTANCE hInst, WNDPROC wndProc); WinClass (int resId, HINSTANCE hInst, WNDPROC wndProc); void SetBgSysColor (int sysColor) { _class.hbrBackground = reinterpret_cast<HBRUSH> (sysColor + 1); } void SetResIcons (int resId); void Register (); protected: void SetDefaults (); WNDCLASSEX _class; };
WinClass::WinClass (char const * className, HINSTANCE hInst, WNDPROC wndProc) : WinSimpleClass (className, hInst) { _class.lpfnWndProc = wndProc; SetDefaults (); }
WinClass::WinClass (int resId, HINSTANCE hInst, WNDPROC wndProc) : WinSimpleClass (resId, hInst) { _class.lpfnWndProc = wndProc; SetDefaults (); }
void WinClass::SetDefaults () { // 提供合理的默认值 _class.cbSize = sizeof (WNDCLASSEX); _class.style = 0; _class.lpszClassName = GetName (); _class.hInstance = GetInstance (); _class.hIcon = 0; _class.hIconSm = 0; _class.lpszMenuName = 0; _class.cbClsExtra = 0; _class.cbWndExtra = 0; _class.hbrBackground = reinterpret_cast<HBRUSH> (COLOR_WINDOW + 1); _class.hCursor = ::LoadCursor (0, IDC_ARROW); }
void WinClass::SetResIcons (int resId) { _class.hIcon = ::LoadIcon (_class.hInstance, MAKEINTRESOURCE (resId)); // 可以使用LoadImage从同样的资源中装入小图标 _class.hIconSm = reinterpret_cast<HICON> ( ::LoadImage ( _class.hInstance, MAKEINTRESOURCE (resId), IMAGE_ICON, ::GetSystemMetrics (SM_CXSMICON), ::GetSystemMetrics (SM_CYSMICON), LR_SHARED)); }
void WinClass::Register () { if (::RegisterClassEx (&_class) == 0) throw WinException ("Internal error: RegisterClassEx failed."); }
--------------------------------------------------------------------------------
class TopWinClass: public WinClass { public: TopWinClass (int resId, HINSTANCE hInst, WNDPROC wndProc); };
TopWinClass::TopWinClass (int resId, HINSTANCE hInst, WNDPROC wndProc) : WinClass (resId, hInst, wndProc) { SetResIcons (resId); _class.lpszMenuName = MAKEINTRESOURCE (resId); }
-------------------------------------------------------------------------------- 一旦窗口类在系统被注册,你可以建立任意个你想要的这个类的窗口。他们将,当然,他们同享这个类注册的过程。稍后我们将可以了解在过程内窗口的不同实例的区别。 WinMaker类的工作非常像WinClass。它的构造函数提供了切合实际的默认值,可以通过调用细节方法去覆盖。一旦任何事都设置了,你调用Create方法去建立一个窗口,及调用Show方法去显示它。注意,在调用Create的瞬间,你的窗口过程被WM_CREATE消息调用。
顶层窗口使用TopWinMaker类建立,提供了适当的风格和标题。
--------------------------------------------------------------------------------  
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
2/2 首页 上一页 1 2 |