1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // License Agreement:
4 //
5 // The following are Copyright � 2007, Casey Langen
6 //
7 // Sources and Binaries of: win32cpp
8 //
9 // All rights reserved.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are met:
13 //
14 // * Redistributions of source code must retain the above copyright notice,
15 // this list of conditions and the following disclaimer.
16 //
17 // * Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // * Neither the name of the author nor the names of other contributors may
22 // be used to endorse or promote products derived from this software
23 // without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 // POSSIBILITY OF SUCH DAMAGE.
36 //
37 //////////////////////////////////////////////////////////////////////////////
38
39 #include <pch.h>
40 #include <win32cpp/GroupBox.hpp>
41 #include <win32cpp/Application.hpp>
42
43 //////////////////////////////////////////////////////////////////////////////
44
45 using namespace win32cpp;
46
47 //////////////////////////////////////////////////////////////////////////////
48
49 ///\brief
50 ///Default constructor.
GroupBox(const uichar * caption)51 /*ctor*/ GroupBox::GroupBox(const uichar* caption)
52 : base()
53 , caption(caption)
54 {
55 }
56
Create(Window * parent)57 HWND GroupBox::Create(Window* parent)
58 {
59 HINSTANCE hInstance = Application::Instance();
60
61 // create the window
62 // (use WS_CLIPSIBLINGS to avoid unnecessary repainting of controls inside)
63 DWORD style = WS_CHILD | WS_VISIBLE | BS_GROUPBOX | WS_CLIPSIBLINGS;
64 //
65 HWND hwnd = CreateWindowEx(
66 NULL, // ExStyle
67 _T("BUTTON"), // Class name
68 this->caption.c_str(), // Window name
69 style, // Style
70 0, // X
71 0, // Y
72 100, // Width
73 100, // Height
74 parent->Handle(), // Parent
75 NULL, // Menu
76 hInstance, // Instance
77 NULL); // lParam
78
79 return hwnd;
80 }
81
OnChildAdded(Window * newChild)82 void GroupBox::OnChildAdded(Window* newChild)
83 {
84 SIZE idealSize;
85
86 idealSize.cx = 0;
87 idealSize.cy = 0;
88
89 // all child elements should be resized to their ideal size
90 if(newChild->SendMessageW(BCM_GETIDEALSIZE, 0, (LPARAM)(SIZE*)&idealSize)) {
91 newChild->Resize(idealSize.cx, idealSize.cy);
92 }
93
94 // all child elements should be WITHIN the groupbox frame,
95 // so we need to try to position them perfectly
96 RECT childRect = newChild->ClientRect();
97 newChild->MoveTo(
98 childRect.left + GetSystemMetrics(SM_CXDLGFRAME) + GetSystemMetrics(SM_CXBORDER),
99 childRect.top + GetSystemMetrics(SM_CYDLGFRAME) + GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYCAPTION)
100 );
101 }
102
OnEraseBackground(HDC hdc)103 void GroupBox::OnEraseBackground(HDC hdc)
104 {
105 HBRUSH bgbrush;
106 RECT bgrect;
107
108 // repaint background (since its white due to its parent controls)
109 bgbrush = ::CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
110 ::SelectObject(hdc, bgbrush);
111
112 ::GetClientRect(this->windowHandle, &bgrect);
113 ::FillRect(hdc, &bgrect, bgbrush);
114
115 ::DeleteObject(bgbrush);
116 }
117