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 struct HiddenWindow
: public WindowHandle
133 HiddenWindow(HWND hwnd
)
136 SetWindowPos(hwnd
, 0, 0, 0, 0, 0, SWP_HIDEWINDOW
|SWP_NOREDRAW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
141 SetWindowPos(_hwnd
, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
146 /// critical section wrapper
148 struct CritSect
: public CRITICAL_SECTION
152 InitializeCriticalSection(this);
157 DeleteCriticalSection(this);
162 /// Lock protects a code section utilizing a critical section
166 Lock(CritSect
& crit_sect
)
167 : _crit_sect(crit_sect
)
169 EnterCriticalSection(&crit_sect
);
174 LeaveCriticalSection(&_crit_sect
);
178 CritSect
& _crit_sect
;
182 /// Thread base class
189 _hThread
= INVALID_HANDLE_VALUE
;
196 CloseHandle(_hThread
);
203 _hThread
= CreateThread(NULL
, 0, ThreadProc
, this, 0, NULL
);
211 Lock
lock(_crit_sect
);
215 // wait for finishing
216 WaitForSingleObject(_hThread
, INFINITE
);
220 virtual int Run() = 0;
222 bool is_alive() const {return _alive
;}
227 static DWORD WINAPI
ThreadProc(void* para
);
236 struct ClientRect
: public RECT
238 ClientRect(HWND hwnd
)
240 GetClientRect(hwnd
, this);
243 operator LPRECT() {return this;}
245 POINT
& pos() {return *(LPPOINT
)this;}
248 struct WindowRect
: public RECT
250 WindowRect(HWND hwnd
)
252 GetWindowRect(hwnd
, this);
255 operator LPRECT() {return this;}
257 POINT
& pos() {return *(LPPOINT
)this;}
260 struct Point
: public POINT
262 Point(LONG x_
, LONG y_
)
268 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
271 x
= GET_X_LPARAM(lparam
);
272 y
= GET_Y_LPARAM(lparam
);
275 operator LPPOINT() {return this;}
279 inline void ClientToScreen(HWND hwnd
, RECT
* prect
)
280 {::ClientToScreen(hwnd
,(LPPOINT
)&prect
->left
); ::ClientToScreen(hwnd
,(LPPOINT
)&prect
->right
);}
282 inline void ScreenToClient(HWND hwnd
, RECT
* prect
)
283 {::ScreenToClient(hwnd
,(LPPOINT
)&prect
->left
); ::ScreenToClient(hwnd
,(LPPOINT
)&prect
->right
);}
286 struct FullScreenParameters
288 FullScreenParameters()
301 struct PaintCanvas
: public PAINTSTRUCT
303 PaintCanvas(HWND hwnd
)
306 BeginPaint(hwnd
, this);
311 EndPaint(_hwnd
, this);
314 operator HDC() const {return hdc
;}
322 Canvas(HDC hdc
) : _hdc(hdc
) {}
324 operator HDC() {return _hdc
;}
330 struct WindowCanvas
: public Canvas
332 WindowCanvas(HWND hwnd
)
333 : Canvas(GetDC(hwnd
)), _hwnd(hwnd
) {}
335 ~WindowCanvas() {ReleaseDC(_hwnd
, _hdc
);}
342 // double buffering classes
344 struct MemCanvas
: public Canvas
347 : Canvas(CreateCompatibleDC(hdc
)) {assert(_hdc
);}
349 ~MemCanvas() {DeleteDC(_hdc
);}
352 struct SelectedBitmap
354 SelectedBitmap(HDC hdc
, HBITMAP hbmp
)
355 : _hdc(hdc
), _old_hbmp(SelectBitmap(hdc
, hbmp
)) {}
357 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc
, _old_hbmp
));}
364 struct BufferCanvas
: public MemCanvas
366 BufferCanvas(HDC hdc
, int x
, int y
, int w
, int h
)
367 : MemCanvas(hdc
), _hdctarg(hdc
),
368 _x(x
), _y(y
), _w(w
), _h(h
),
369 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, w
, h
)) {}
371 BufferCanvas(HDC hdc
, const RECT
& rect
)
372 : MemCanvas(hdc
), _hdctarg(hdc
),
373 _x(rect
.left
), _y(rect
.top
), _w(rect
.right
-rect
.left
), _h(rect
.bottom
-rect
.top
),
374 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, _w
, _h
)) {}
382 struct BufferedCanvas
: public BufferCanvas
384 BufferedCanvas(HDC hdc
, int x
, int y
, int w
, int h
, DWORD mode
=SRCCOPY
)
385 : BufferCanvas(hdc
, x
, y
, w
, h
), _mode(mode
) {}
387 BufferedCanvas(HDC hdc
, const RECT
& rect
, DWORD mode
=SRCCOPY
)
388 : BufferCanvas(hdc
, rect
), _mode(mode
) {}
390 ~BufferedCanvas() {BitBlt(_hdctarg
, _x
, _y
, _w
, _h
, _hdc
, 0, 0, _mode
);}
395 struct BufferedPaintCanvas
: public PaintCanvas
, public BufferedCanvas
397 BufferedPaintCanvas(HWND hwnd
)
399 BufferedCanvas(PAINTSTRUCT::hdc
, 0, 0, rcPaint
.right
, rcPaint
.bottom
)
403 operator HDC() {return BufferedCanvas::_hdc
;}
409 TextColor(HDC hdc
, COLORREF color
)
410 : _hdc(hdc
), _old_color(SetTextColor(hdc
, color
)) {}
412 ~TextColor() {SetTextColor(_hdc
, _old_color
);}
421 BkMode(HDC hdc
, int bkmode
)
422 : _hdc(hdc
), _old_bkmode(SetBkMode(hdc
, bkmode
)) {}
424 ~BkMode() {SetBkMode(_hdc
, _old_bkmode
);}
428 COLORREF _old_bkmode
;
433 FontSelection(HDC hdc
, HFONT hFont
)
434 : _hdc(hdc
), _old_hFont(SelectFont(hdc
, hFont
)) {}
436 ~FontSelection() {SelectFont(_hdc
, _old_hFont
);}
443 struct BitmapSelection
445 BitmapSelection(HDC hdc
, HBITMAP hBmp
)
446 : _hdc(hdc
), _old_hBmp(SelectBitmap(hdc
, hBmp
)) {}
448 ~BitmapSelection() {SelectBitmap(_hdc
, _old_hBmp
);}
464 typedef wstring super
;
466 typedef string super
;
470 String(LPCTSTR s
) : super(s
) {}
471 String(const super
& other
) : super(other
) {}
472 String(const String
& other
) : super(other
) {}
474 String
& operator=(LPCTSTR s
) {assign(s
); return *this;}
475 String
& operator=(const super
& s
) {assign(s
); return *this;}
477 operator LPCTSTR() const {return c_str();}
481 /// link dynamicly to functions by using GetModuleHandle() and GetProcAddress()
482 template<typename FCT
> struct DynamicFct
484 DynamicFct(LPCTSTR moduleName
, UINT ordinal
)
486 HMODULE hModule
= GetModuleHandle(moduleName
);
488 _fct
= (FCT
) GetProcAddress(hModule
, (LPCSTR
)ordinal
);
491 DynamicFct(LPCTSTR moduleName
, LPCSTR name
)
493 HMODULE hModule
= GetModuleHandle(moduleName
);
495 _fct
= (FCT
) GetProcAddress(hModule
, name
);
498 FCT
operator*() const {return _fct
;}
499 operator bool() const {return _fct
? true: false;}
506 /// link dynamicly to functions by using LoadLibrary() and GetProcAddress()
507 template<typename FCT
> struct DynamicLoadLibFct
509 DynamicLoadLibFct(LPCTSTR moduleName
, UINT ordinal
)
511 _hModule
= LoadLibrary(moduleName
);
513 _fct
= (FCT
) GetProcAddress(_hModule
, (LPCSTR
)ordinal
);
516 DynamicLoadLibFct(LPCTSTR moduleName
, LPCSTR name
)
518 _hModule
= LoadLibrary(moduleName
);
520 _fct
= (FCT
) GetProcAddress(_hModule
, name
);
525 FreeLibrary(_hModule
);
528 FCT
operator*() const {return _fct
;}
529 operator bool() const {return _fct
? true: false;}
536 #endif // __cplusplus
545 #define LONGLONGARG TEXT("I64")
547 #define LONGLONGARG TEXT("L")
553 #define _tcsrchr wcsrchr
555 #define _tcsrchr strrchr
561 #define _stprintf wcsprintf
563 #define _stprintf sprintf
570 extern void _wsplitpath(const WCHAR
* path
, WCHAR
* drv
, WCHAR
* dir
, WCHAR
* name
, WCHAR
* ext
);
572 extern void _splitpath(const CHAR
* path
, CHAR
* drv
, CHAR
* dir
, CHAR
* name
, CHAR
* ext
);
576 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
577 #define FILE_ATTRIBUTE_ENCRYPTED 0x00000040
578 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
579 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
580 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
584 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
585 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
586 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
587 #define Window_SetIcon(hwnd, type, hicon) (HICON)SendMessage(hwnd, WM_SETICON, type, (LPARAM)(hicon))
591 // center window in respect to its parent window
592 extern void CenterWindow(HWND hwnd
);
594 // move window into visibility
595 extern void MoveVisible(HWND hwnd
);
597 // display error message
598 extern void display_error(HWND hwnd
, DWORD error
);
600 // convert time_t to WIN32 FILETIME
601 extern BOOL
time_to_filetime(const time_t* t
, FILETIME
* ftime
);
603 // search for windows of a specific classname
604 extern int find_window_class(LPCTSTR classname
);
606 // create a bitmap from an icon
607 extern HBITMAP
create_bitmap_from_icon(HICON hIcon
, HBRUSH hbrush_bkgnd
, HDC hdc_wnd
);
609 // launch a program or document file
610 extern BOOL
launch_file(HWND hwnd
, LPCTSTR cmd
, UINT nCmdShow
);
612 extern BOOL
launch_fileA(HWND hwnd
, LPSTR cmd
, UINT nCmdShow
);
614 #define launch_fileA launch_file
617 // call an DLL export like rundll32
618 BOOL
RunDLL(HWND hwnd
, LPCTSTR dllname
, LPCSTR procname
, LPCTSTR cmdline
, UINT nCmdShow
);