You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Win32Util.h 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. // -=- Win32Util.h
  19. // Miscellaneous but useful Win32 API utility functions & classes.
  20. // In particular, a set of classes which wrap GDI objects,
  21. // and some to handle palettes.
  22. #ifndef __RFB_WIN32_GDIUTIL_H__
  23. #define __RFB_WIN32_GDIUTIL_H__
  24. #include <rfb/ColourMap.h>
  25. #include <rfb/PixelFormat.h>
  26. #include <rfb/Rect.h>
  27. #include <rfb_win32/TCharArray.h>
  28. namespace rfb {
  29. namespace win32 {
  30. class LogicalPalette {
  31. public:
  32. LogicalPalette();
  33. ~LogicalPalette();
  34. void setEntries(int start, int count, const Colour* cols);
  35. HPALETTE getHandle() {return palette;}
  36. protected:
  37. HPALETTE palette;
  38. int numEntries;
  39. };
  40. class DeviceContext {
  41. public:
  42. DeviceContext() : dc(0) {}
  43. virtual ~DeviceContext() {}
  44. operator HDC() const {return dc;}
  45. PixelFormat getPF() const;
  46. static PixelFormat getPF(HDC dc);
  47. protected:
  48. HDC dc;
  49. };
  50. class WindowDC : public DeviceContext {
  51. public:
  52. WindowDC(HWND wnd);
  53. virtual ~WindowDC();
  54. protected:
  55. HWND hwnd;
  56. };
  57. class CompatibleDC : public DeviceContext {
  58. public:
  59. CompatibleDC(HDC existing);
  60. virtual ~CompatibleDC();
  61. };
  62. class BitmapDC : public CompatibleDC {
  63. public:
  64. BitmapDC(HDC hdc, HBITMAP hbitmap);
  65. ~BitmapDC();
  66. protected:
  67. HBITMAP oldBitmap;
  68. };
  69. class CompatibleBitmap {
  70. public:
  71. CompatibleBitmap(HDC hdc, int width, int height);
  72. virtual ~CompatibleBitmap();
  73. operator HBITMAP() const {return hbmp;}
  74. protected:
  75. HBITMAP hbmp;
  76. };
  77. struct BitmapInfo {
  78. BITMAPINFOHEADER bmiHeader;
  79. union {
  80. struct {
  81. DWORD red;
  82. DWORD green;
  83. DWORD blue;
  84. } mask;
  85. RGBQUAD color[256];
  86. };
  87. };
  88. inline void initMaxAndShift(DWORD mask, int* max, int* shift) {
  89. for ((*shift) = 0; (mask & 1) == 0; (*shift)++) mask >>= 1;
  90. (*max) = (rdr::U16)mask;
  91. }
  92. class PaletteSelector {
  93. public:
  94. PaletteSelector(HDC dc, HPALETTE pal);
  95. ~PaletteSelector();
  96. bool isRedrawRequired() {return redrawRequired;}
  97. protected:
  98. HPALETTE oldPal;
  99. HDC device;
  100. bool redrawRequired;
  101. };
  102. struct IconInfo : public ICONINFO {
  103. IconInfo(HICON icon);
  104. ~IconInfo();
  105. };
  106. struct ModuleFileName : public TCharArray {
  107. ModuleFileName(HMODULE module=0);
  108. };
  109. struct FileVersionInfo : public TCharArray {
  110. FileVersionInfo(const TCHAR* filename=0);
  111. const TCHAR* getVerString(const TCHAR* name, DWORD langId = 0x080904b0);
  112. };
  113. bool splitPath(const TCHAR* path, TCHAR** dir, TCHAR** file);
  114. class DynamicFnBase {
  115. public:
  116. DynamicFnBase(const TCHAR* dllName, const char* fnName);
  117. ~DynamicFnBase();
  118. bool isValid() const {return fnPtr != 0;}
  119. protected:
  120. void* fnPtr;
  121. HMODULE dllHandle;
  122. };
  123. template<class T> class DynamicFn : public DynamicFnBase {
  124. public:
  125. DynamicFn(const TCHAR* dllName, const char* fnName) : DynamicFnBase(dllName, fnName) {}
  126. T operator *() const {return (T)fnPtr;};
  127. };
  128. // Structure containing info on the monitor nearest the window.
  129. // Copes with multi-monitor OSes and older ones.
  130. #if (WINVER >= 0x0500)
  131. struct MonitorInfo : MONITORINFOEXA {
  132. MonitorInfo(HWND hwnd);
  133. };
  134. #else
  135. struct MonitorInfo {
  136. MonitorInfo(HWND hwnd);
  137. DWORD cbSize;
  138. RECT rcMonitor;
  139. RECT rcWork;
  140. DWORD dwFlags;
  141. char szDevice[1]; // Always null...
  142. };
  143. #endif
  144. void moveToMonitor(HWND handle, const char* device);
  145. class Handle {
  146. public:
  147. Handle(HANDLE h_=0) : h(h_) {}
  148. ~Handle() {
  149. if (h) CloseHandle(h);
  150. }
  151. operator HANDLE() {return h;}
  152. HANDLE h;
  153. };
  154. // Center the window to a rectangle, or to a parent window.
  155. // Optionally, resize the window to lay within the rect or parent window
  156. // If the parent window is NULL then the working area if the window's
  157. // current monitor is used instead.
  158. void centerWindow(HWND handle, const RECT& r, bool clipToRect=false);
  159. void centerWindow(HWND handle, HWND parent, bool clipToRect=false);
  160. // MsgBox helper function. Define rfb::win32::AppName somewhere in your
  161. // code and MsgBox will use its value in informational messages.
  162. extern TStr AppName;
  163. int MsgBox(HWND parent, const TCHAR* message, UINT flags);
  164. // Get the computer name
  165. struct ComputerName : TCharArray {
  166. ComputerName() : TCharArray(MAX_COMPUTERNAME_LENGTH+1) {
  167. ULONG namelength = MAX_COMPUTERNAME_LENGTH+1;
  168. if (!GetComputerName(buf, &namelength))
  169. _tcscpy(buf, _T(""));
  170. }
  171. };
  172. // Allocate and/or manage LocalAlloc memory.
  173. struct LocalMem {
  174. LocalMem(int size) : ptr(LocalAlloc(LMEM_FIXED, size)) {
  175. if (!ptr) throw rdr::SystemException("LocalAlloc", GetLastError());
  176. }
  177. LocalMem(void* p) : ptr(p) {}
  178. ~LocalMem() {LocalFree(ptr);}
  179. operator void*() {return ptr;}
  180. void* takePtr() {
  181. void* t = ptr; ptr = 0; return t;
  182. }
  183. void* ptr;
  184. };
  185. };
  186. };
  187. #endif