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 /// link dynamicly to functions by using GetModuleHandle() and GetProcAddress()
401 template<typename FCT
> struct DynamicFct
403 DynamicFct(LPCTSTR moduleName
, UINT ordinal
)
405 HMODULE hModule
= GetModuleHandle(moduleName
);
407 _fct
= (FCT
) GetProcAddress(hModule
, (LPCSTR
)ordinal
);
410 FCT
operator*() const {return _fct
;}
411 operator bool() const {return _fct
? true: false;}
418 /// link dynamicly to functions by using LoadLibrary() and GetProcAddress()
419 template<typename FCT
> struct DynamicLoadLibFct
421 DynamicLoadLibFct(LPCTSTR moduleName
, UINT ordinal
)
423 _hModule
= LoadLibrary(moduleName
);
425 _fct
= (FCT
) GetProcAddress(_hModule
, (LPCSTR
)ordinal
);
430 FreeLibrary(_hModule
);
433 FCT
operator*() const {return _fct
;}
434 operator bool() const {return _fct
? true: false;}
441 #endif // __cplusplus
450 #define LONGLONGARG TEXT("I64")
452 #define LONGLONGARG TEXT("L")
458 #define _tcsrchr wcsrchr
460 #define _tcsrchr strrchr
466 #define _stprintf wcsprintf
468 #define _stprintf sprintf
475 extern void _wsplitpath(const WCHAR
* path
, WCHAR
* drv
, WCHAR
* dir
, WCHAR
* name
, WCHAR
* ext
);
477 extern void _splitpath(const CHAR
* path
, CHAR
* drv
, CHAR
* dir
, CHAR
* name
, CHAR
* ext
);
481 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
482 #define FILE_ATTRIBUTE_ENCRYPTED 0x00000040
483 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
484 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
485 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
489 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
490 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
491 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
495 extern void display_error(HWND hwnd
, DWORD error
);
497 // convert time_t to WIN32 FILETIME
498 extern BOOL
time_to_filetime(const time_t* t
, FILETIME
* ftime
);
500 // search for windows of a specific classname
501 extern int find_window_class(LPCTSTR classname
);
503 // create a bitmap from an icon
504 extern HBITMAP
create_bitmap_from_icon(HICON hIcon
, HBRUSH hbrush_bkgnd
, HDC hdc_wnd
);
506 // launch a program or document file
507 extern BOOL
launch_file(HWND hwnd
, LPCTSTR cmd
, UINT nCmdShow
);
509 extern BOOL
launch_fileA(HWND hwnd
, LPSTR cmd
, UINT nCmdShow
);
511 #define launch_fileA launch_file
514 // call an DLL export like rundll32
515 BOOL
RunDLL(HWND hwnd
, LPCTSTR dllname
, LPCSTR procname
, LPCTSTR cmdline
, UINT nCmdShow
);