OSDN Git Service

Add TortoiseProc
[tortoisegit/TortoiseGitJp.git] / Utils / MiscUI / BaseWindow.cpp
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2008 - TortoiseSVN\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "stdafx.h"\r
20 #include "BaseWindow.h"\r
21 \r
22 \r
23 ResString::ResString (HINSTANCE hInst, int resId)\r
24 {\r
25         if (!::LoadString (hInst, resId, _buf, MAX_RESSTRING + 1))\r
26         {\r
27                 SecureZeroMemory(_buf, sizeof(_buf));\r
28         }\r
29 }\r
30 \r
31 \r
32 bool CWindow::RegisterWindow(UINT style, HICON hIcon, HCURSOR hCursor, HBRUSH hbrBackground, \r
33                                                                         LPCTSTR lpszMenuName, LPCTSTR lpszClassName, HICON hIconSm)\r
34 {\r
35         WNDCLASSEX wcx; \r
36  \r
37         // Fill in the window class structure with default parameters \r
38  \r
39         wcx.cbSize = sizeof(WNDCLASSEX);                                // size of structure \r
40         wcx.style = style;                                                              // redraw if size changes \r
41         wcx.lpfnWndProc = CWindow::stWinMsgHandler;             // points to window procedure \r
42         wcx.cbClsExtra = 0;                                                             // no extra class memory \r
43         wcx.cbWndExtra = 0;                                                             // no extra window memory \r
44         wcx.hInstance = hResource;                                              // handle to instance \r
45         wcx.hIcon = hIcon;                                                              // predefined app. icon \r
46         wcx.hCursor = hCursor;                                                  // predefined arrow \r
47         wcx.hbrBackground = hbrBackground;                              // white background brush \r
48         wcx.lpszMenuName = lpszMenuName;                                // name of menu resource \r
49         wcx.lpszClassName = lpszClassName;                              // name of window class \r
50         wcx.hIconSm = hIconSm;                                                  // small class icon \r
51  \r
52         // Register the window class. \r
53         return RegisterWindow(&wcx); \r
54 }\r
55 \r
56 bool CWindow::RegisterWindow(CONST WNDCLASSEX* wcx)\r
57 {\r
58         // Register the window class. \r
59         sClassName = std::wstring(wcx->lpszClassName);\r
60 \r
61         if (RegisterClassEx(wcx) == 0)\r
62                 return FALSE;\r
63         else\r
64                 return TRUE;\r
65 }\r
66 \r
67 LRESULT CALLBACK CWindow::stWinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)\r
68 {\r
69         CWindow* pWnd;\r
70 \r
71         if (uMsg == WM_NCCREATE)\r
72         {\r
73                 // get the pointer to the window from lpCreateParams which was set in CreateWindow\r
74                 SetWindowLongPtr(hwnd, GWLP_USERDATA, (long)((LPCREATESTRUCT(lParam))->lpCreateParams));\r
75         }\r
76 \r
77         // get the pointer to the window\r
78         pWnd = GetObjectFromWindow(hwnd);\r
79 \r
80         // if we have the pointer, go to the message handler of the window\r
81         // else, use DefWindowProc\r
82         if (pWnd)\r
83                 return pWnd->WinMsgHandler(hwnd, uMsg, wParam, lParam);\r
84         else\r
85                 return DefWindowProc(hwnd, uMsg, wParam, lParam);\r
86 }\r
87 \r
88 bool CWindow::Create()\r
89\r
90         // Create the window\r
91         RECT rect;\r
92 \r
93         rect.top = 0;\r
94         rect.left = 0;\r
95         rect.right = 600;\r
96         rect.bottom = 400;\r
97 \r
98         return Create(WS_OVERLAPPEDWINDOW | WS_VISIBLE, NULL, &rect);\r
99 }\r
100 \r
101 bool CWindow::Create(DWORD dwStyles, HWND hParent /* = NULL */, RECT* rect /* = NULL */)\r
102\r
103         return CreateEx(0, dwStyles, hParent, rect);    \r
104 }\r
105 \r
106 bool CWindow::CreateEx(DWORD dwExStyles, DWORD dwStyles, HWND hParent /* = NULL */, RECT* rect /* = NULL */)\r
107 {\r
108         // send the this pointer as the window creation parameter\r
109         if (rect == NULL)\r
110                 m_hwnd = CreateWindowEx(dwExStyles, sClassName.c_str(), sWindowTitle.c_str(), dwStyles, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hParent, NULL, hResource, (void *)this);\r
111         else\r
112         {\r
113                 m_hwnd = CreateWindowEx(dwExStyles, sClassName.c_str(), sWindowTitle.c_str(), dwStyles, rect->left, rect->top, \r
114                         rect->right - rect->left, rect->bottom - rect->top, hParent, NULL, hResource, \r
115                         (void *)this);\r
116         }\r
117         return (m_hwnd != NULL);\r
118 }\r
119 \r
120 void CWindow::SetTransparency(BYTE alpha, COLORREF color /* = 0xFF000000 */)\r
121 {\r
122         if (alpha == 255)\r
123         {\r
124                 LONG_PTR exstyle = GetWindowLongPtr(*this, GWL_EXSTYLE);\r
125                 exstyle &= ~WS_EX_LAYERED;\r
126                 SetWindowLongPtr(*this, GWL_EXSTYLE, exstyle);\r
127         }\r
128         else\r
129         {\r
130                 LONG_PTR exstyle = GetWindowLongPtr(*this, GWL_EXSTYLE);\r
131                 exstyle |= WS_EX_LAYERED;\r
132                 SetWindowLongPtr(*this, GWL_EXSTYLE, exstyle);\r
133         }\r
134         COLORREF col = color;\r
135         DWORD flags = LWA_ALPHA;\r
136         if (col & 0xFF000000)\r
137         {\r
138                 col = RGB(255, 255, 255);\r
139                 flags = LWA_ALPHA;\r
140         }\r
141         else\r
142         {\r
143                 flags = LWA_COLORKEY;\r
144         }\r
145         SetLayeredWindowAttributes(*this, col, alpha, flags);\r
146 }\r