Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#if !defined (FORMS_HPP__INCLUDED) #define FORMS_HPP__INCLUDED #if (_MSC_VER >= 1300) #pragma once #endif // (_MSC_VER >= 1300) #include <windows.h> #include <tchar.h> #include <string> typedef tstring std::basic_string<TCHAR>; namespace Forms { class Window { public: Window(); ~Window() { if (m_hWnd != NULL) ::DestroyWindow(m_hWnd); } operator ::HWND() const { return m_hWnd; } #pragma warning(disable: 4312) static Window* FromHandle(const ::HWND& hWnd) { return reinterpret_cast<Window*>(GetWindowLongPtr(hWnd, GWL_USERDATA)); }; #pragma warning(default: 4312) private: Window& operator=(const Window&) { return *this; } Window(const Window&); public: virtual void create(unsigned long, unsigned long, unsigned long, unsigned long, const tstring&, unsigned long, const tstring&, const ::HINSTANCE&, const ::HMENU& hMenu, Window* pParentWnd = NULL); virtual void create(unsigned long x, unsigned long y, unsigned long width, unsigned long height, const tstring& classname, unsigned long style, const tstring& caption, const ::HINSTANCE& instance, std::size_t id, Window* pParentWnd = NULL) { create(x, y, width, height, classname, style, caption, instance, reinterpret_cast<::HMENU>(id), pParentWnd); } virtual void create(const ::RECT& rect, const tstring& classname, unsigned long style, const tstring& caption, const ::HINSTANCE& instance, const ::HMENU& hMenu, Window* pParentWnd = NULL) { create(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, classname, style, caption, instance, id, pParentWnd); } virtual void create(const ::RECT& rect, const tstring& classname, unsigned long style, const tstring& caption, const ::HINSTANCE& instance, std::size_t id, Window* pParentWnd = NULL) { create(rect, classname, style, caption, instance, reinterpret_cast<::HMENU>(id), pParentWnd); } bool show(int cmd) { return !!::ShowWindow(m_hWnd); } public: ::HWND const& get_handle() const { return m_hWnd; } ::HWND const& get_parent() const { return ::GetParent(m_hWnd); } ::HMENU const& get_menu() const { return ::GetMenu(m_hWnd); } bool is_visible() const { return !!::IsWindowVisible(m_hWnd); } void get_window_name(tstring& caption) const { int size = ::GetWindowTextLength(m_hWnd) + 1; TCHAR* arr = new TCHAR[size]; ::GetWindowText(m_hWnd, arr, size); caption = arr; delete [] arr; } public: virtual LRESULT _message_proc(::HWND, UINT, WPARAM, LPARAM); private: static LRESULT CALLBACK __message_proc(::HWND, UINT, WPARAM, LPARAM); protected: ::HWND m_hWnd; }; class Button : public Window { public: void create(unsigned long x, unsigned long y, unsigned long width, unsigned long height, unsigned long style, const tstring& caption, const ::HINSTANCE& instance, std::size_t id, Window* pParentWnd = NULL) { Window::create(x, y, width, height, style, TEXT("button"), caption, instance, id, pParentWnd); } }; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
Window::Window() : m_hWnd(NULL) {} void Window::create(unsigned long x, unsigned long y, unsigned long width, unsigned long height, const std::wstring& classname, unsigned long style, const std::wstring& caption, const ::HINSTANCE& instance, const ::HEMNU& hMenu, Window* pParentWnd) { ::WNDCLASSEX wc; if (::GetClassInfoEx(NULL, classname.c_str(), &wc) == FALSE && ::GetClassInfoEx(instance, classname.c_str(), &wc) == FALSE) { ::ZeroMemory(&wc, sizeof(::WNDCLASSEX)); wc.cbSize = sizeof(::WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = __message_proc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = ::LoadIcon(instance, MAKEINTRESOURCE(IDI_ICOWARNING)); wc.hIconSm = static_cast<::HICON>(::LoadImage(wc.hInstance, MAKEINTRESOURCE(IDI_ICOWARNING), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR)); wc.hCursor = ::LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = ::CreateSolidBrush(RGB(237, 237, 237));//::CreateSolidBrush(RGB(50, 50, 50)); // 64 wc.lpszMenuName = NULL; wc.lpszClassName = classname.c_str(); if (::RegisterClassEx(&wc) == 0) throw std::runtime_error("class registration failed"); } m_pParentWnd = pParentWnd; m_hWnd = ::CreateWindowEx(0, classname.c_str(), caption.c_str(), style, x, y, width, height, (pParentWnd == NULL ? NULL : pParentWnd->get_handle()), hMenu, instance, static_cast<LPVOID>(this)); if (m_hWnd == NULL) throw std::runtime_error("window could not be created"); } LRESULT CALLBACK Window::__message_proc(::HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { Window* pWindow = Window::FromHandle(hWnd); #pragma warning(disable: 4311) if (message == WM_NCCREATE) { pWindow = reinterpret_cast<Window*>(reinterpret_cast<::LPCREATESTRUCT>(lParam)->lpCreateParams); SetWindowLongPtr(hWnd, GWL_USERDATA, reinterpret_cast<LONG>(pWindow)); } #pragma warning(default: 4311) return (pWindow != NULL ? pWindow->_message_proc(hWnd, message, wParam, lParam) : ::DefWindowProc(hWnd, message, wParam, lParam)); } LRESULT Window::_message_proc(::HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { return ::DefWindowProc(hWnd, message, wParam, lParam)); } |
Werbeanzeige