2 * Copyright 2003 Martin Fuchs
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 // Martin Fuchs, 23.07.2003
29 // standard windows headers
30 #define WIN32_LEAN_AND_MEAN
31 #define WIN32_EXTRA_LEAN
40 #include <windowsx.h> // for SelectBrush(), ListBox_SetSel(), SubclassWindow(), ...
43 #include <malloc.h> // for alloca()
45 #include <stdlib.h> // for _MAX_DIR, ...
46 #include <stdio.h> // for sprintf()
51 #define _MAX_FNAME 256
52 #define _MAX_DIR _MAX_FNAME
53 #define _MAX_EXT _MAX_FNAME
61 #pragma warning(disable: 4786) // disable warnings about too long debug information symbols
64 // STL headers for strings and streams
69 #if _MSC_VER>=1300 // VS.Net
70 #define _NO_COMUTIL //@@
73 #if defined(_MSC_VER) && !defined(_NO_COMUTIL)
75 // COM utility headers
77 using namespace _com_util;
79 #endif // _MSC_VER && !_NO_COMUTIL
82 #define for if (0) {} else for
85 #define BUFFER_LEN 1024
88 struct CommonControlInit
90 CommonControlInit(DWORD flags=ICC_LISTVIEW_CLASSES|ICC_TREEVIEW_CLASSES|ICC_BAR_CLASSES|ICC_PROGRESS_CLASS|ICC_COOL_CLASSES)
92 INITCOMMONCONTROLSEX icc = {sizeof(INITCOMMONCONTROLSEX), flags};
94 InitCommonControlsEx(&icc);
101 struct WaitCursor //TODO: integrate with WM_SETCURSOR to enable multithreaded background tasks as program launching
105 _old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
110 SetCursor(_old_cursor);
120 WindowHandle(HWND hwnd=0)
123 operator HWND() const {return _hwnd;}
124 HWND* operator&() {return &_hwnd;}
131 /// critical section wrapper
133 struct CritSect : public CRITICAL_SECTION
137 InitializeCriticalSection(this);
142 DeleteCriticalSection(this);
147 /// Lock protects a code section utilizing a critical section
151 Lock(CritSect& crit_sect)
152 : _crit_sect(crit_sect)
154 EnterCriticalSection(&crit_sect);
159 LeaveCriticalSection(&_crit_sect);
163 CritSect& _crit_sect;
169 struct ClientRect : public RECT
171 ClientRect(HWND hwnd)
173 GetClientRect(hwnd, this);
176 operator LPRECT() {return this;}
179 struct WindowRect : public RECT
181 WindowRect(HWND hwnd)
183 GetWindowRect(hwnd, this);
186 operator LPRECT() {return this;}
189 struct Point : public POINT
191 Point(LONG x_, LONG y_)
197 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
200 x = GET_X_LPARAM(lparam);
201 y = GET_Y_LPARAM(lparam);
204 operator LPPOINT() {return this;}
208 struct FullScreenParameters
210 FullScreenParameters()
223 struct PaintCanvas : public PAINTSTRUCT
225 PaintCanvas(HWND hwnd)
228 BeginPaint(hwnd, this);
233 EndPaint(_hwnd, this);
236 operator HDC() const {return hdc;}
244 Canvas(HDC hdc) : _hdc(hdc) {}
246 operator HDC() {return _hdc;}
252 struct WindowCanvas : public Canvas
254 WindowCanvas(HWND hwnd)
255 : Canvas(GetDC(hwnd)), _hwnd(hwnd) {}
257 ~WindowCanvas() {ReleaseDC(_hwnd, _hdc);}
264 // double buffering classes
266 struct MemCanvas : public Canvas
269 : Canvas(CreateCompatibleDC(hdc)) {assert(_hdc);}
271 ~MemCanvas() {DeleteDC(_hdc);}
274 struct SelectedBitmap
276 SelectedBitmap(HDC hdc, HBITMAP hbmp)
277 : _hdc(hdc), _old_hbmp(SelectBitmap(hdc, hbmp)) {}
279 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc, _old_hbmp));}
286 struct BufferCanvas : public MemCanvas
288 BufferCanvas(HDC hdc, int x, int y, int w, int h)
289 : MemCanvas(hdc), _hdctarg(hdc),
290 _x(x), _y(y), _w(w), _h(h),
291 _bmp(_hdc, CreateCompatibleBitmap(hdc, w, h)) {}
293 BufferCanvas(HDC hdc, const RECT& rect)
294 : MemCanvas(hdc), _hdctarg(hdc),
295 _x(rect.left), _y(rect.top), _w(rect.right-rect.left), _h(rect.bottom-rect.top),
296 _bmp(_hdc, CreateCompatibleBitmap(hdc, _w, _h)) {}
304 struct BufferedCanvas : public BufferCanvas
306 BufferedCanvas(HDC hdc, int x, int y, int w, int h, DWORD mode=SRCCOPY)
307 : BufferCanvas(hdc, x, y, w, h), _mode(mode) {}
309 BufferedCanvas(HDC hdc, const RECT& rect, DWORD mode=SRCCOPY)
310 : BufferCanvas(hdc, rect), _mode(mode) {}
312 ~BufferedCanvas() {BitBlt(_hdctarg, _x, _y, _w, _h, _hdc, 0, 0, _mode);}
317 struct BufferedPaintCanvas : public PaintCanvas, public BufferedCanvas
319 BufferedPaintCanvas(HWND hwnd)
321 BufferedCanvas(PAINTSTRUCT::hdc, 0, 0, rcPaint.right, rcPaint.bottom)
325 operator HDC() {return BufferedCanvas::_hdc;}
331 TextColor(HDC hdc, COLORREF color)
332 : _hdc(hdc), _old_color(SetTextColor(hdc, color)) {}
334 ~TextColor() {SetTextColor(_hdc, _old_color);}
343 BkMode(HDC hdc, int bkmode)
344 : _hdc(hdc), _old_bkmode(SetBkMode(hdc, bkmode)) {}
346 ~BkMode() {SetBkMode(_hdc, _old_bkmode);}
350 COLORREF _old_bkmode;
355 FontSelection(HDC hdc, HFONT hFont)
356 : _hdc(hdc), _old_hFont(SelectFont(hdc, hFont)) {}
358 ~FontSelection() {SelectFont(_hdc, _old_hFont);}
365 struct BitmapSelection
367 BitmapSelection(HDC hdc, HBITMAP hBmp)
368 : _hdc(hdc), _old_hBmp(SelectBitmap(hdc, hBmp)) {}
370 ~BitmapSelection() {SelectBitmap(_hdc, _old_hBmp);}
386 typedef wstring super;
388 typedef string super;
392 String(LPCTSTR s) : super(s) {}
393 String(const String& other) : super(other) {}
395 String& operator=(LPCTSTR s) {assign(s); return *this;}
396 operator LPCTSTR() const {return c_str();}
400 #endif // __cplusplus
409 #define LONGLONGARG TEXT("I64")
411 #define LONGLONGARG TEXT("L")
417 #define _tcsrchr wcsrchr
419 #define _tcsrchr strrchr
425 #define _stprintf wcsprintf
427 #define _stprintf sprintf
434 extern void _wsplitpath(const WCHAR* path, WCHAR* drv, WCHAR* dir, WCHAR* name, WCHAR* ext);
436 extern void _splitpath(const CHAR* path, CHAR* drv, CHAR* dir, CHAR* name, CHAR* ext);
440 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
441 #define FILE_ATTRIBUTE_ENCRYPTED 0x00000040
442 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
443 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
444 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
448 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
449 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
450 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
454 extern void display_error(HWND hwnd, DWORD error);
456 // convert time_t to WIN32 FILETIME
457 extern BOOL time_to_filetime(const time_t* t, FILETIME* ftime);
459 // search for windows of a specific classname
460 extern int find_window_class(LPCTSTR classname);
462 // create a bitmap from an icon
463 extern HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd);
465 // launch a program or document file
466 extern BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow);
468 extern BOOL launch_fileA(HWND hwnd, LPSTR cmd, UINT nCmdShow);
470 #define launch_fileA launch_file
473 // call an DLL export like rundll32
474 BOOL RunDLL(HWND hwnd, LPCTSTR dllname, LPCSTR procname, LPCTSTR cmdline, UINT nCmdShow);