PylonGUI.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //-----------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2011-2021 Basler AG
  4. // http://www.baslerweb.com
  5. // Author: NN
  6. //-----------------------------------------------------------------------------
  7. /*!
  8. \file
  9. \brief Implements a simple window to show a pylon Image.
  10. To use the pylon GUI classes and functions you should not include this file directly.
  11. Instead, you should include \c pylon/PylonGUIIncludes.h.
  12. */
  13. #ifndef PYLON_GUI_H_INCLUDED__
  14. #define PYLON_GUI_H_INCLUDED__
  15. #include <pylon/stdinclude.h>
  16. #pragma pack(push, PYLON_PACKING)
  17. #include <pylon/PylonLinkage.h>
  18. #ifdef PYLONGUI_EXPORTS
  19. # define PYLONGUI_API APIEXPORT
  20. #else
  21. # define PYLONGUI_API APIIMPORT
  22. # ifndef PYLON_NO_AUTO_IMPLIB
  23. # pragma comment(lib, PYLON_LIB_NAME( "PylonGUI" ))
  24. # endif
  25. #endif
  26. #if defined(__cplusplus)
  27. namespace Pylon
  28. {
  29. /** \addtogroup Pylon_ImageHandlingSupport
  30. * @{
  31. */
  32. // forwards
  33. interface IImage;
  34. ///////////////////////////////////////////////////////////////////////
  35. // convenience function to show an image in one row
  36. /*!
  37. \brief Convenience function to display an image on screen with one function call.
  38. \param[in] winIndex The window used for displaying the image. Valid window indexes range from 0 to 31.
  39. \param[in] image The image to display in the window, specified in winIndex.
  40. You can also pass a CGrabResultPtr.
  41. This convenience function displays the image or grab result passed in the \a image parameter in a window on the screen.
  42. Using this function is the easiest way to display an image on the screen.
  43. You can pass any IImage based object or a GrabResultPtr. If the window hasn't been created before, it will be created with default positions and the image will be shown.
  44. When the function returns, you can free/delete the image.
  45. If you need more control over the window creation (e.g. to specify the position or size of the window), you can use the CPylonImageWindow class.
  46. When you pass an empty/invalid (e.g. image.IsValid() returns false) image, the window will be cleared.
  47. \code
  48. // This smart pointer will receive the grab result data.
  49. CGrabResultPtr ptrGrabResult;
  50. // Open the first device.
  51. CInstantCamera Camera( CTlFactory::GetInstance().CreateFirstDevice());
  52. // Grab one image.
  53. Camera.GrabOne( 1000, ptrGrabResult);
  54. // Display the image on the screen. If the grab result is invalid, the image window will be empty.
  55. Pylon::DisplayImage(1, ptrGrabResult);
  56. \endcode
  57. \post
  58. <ul>
  59. <li>The window specified by \a winIndex will be created and shown on the screen.
  60. <li>If the passed image is valid, it is shown in the window specified by \a winIndex.
  61. <li>If the passed image is invalid, the window specified by \a winIndex will be cleared.
  62. </ul>
  63. \remark
  64. To use this function, you must explicitly include \c pylon/PylonGUIIncludes.h in your code.
  65. Unlike other pylon headers, the GUI headers will not be included automatically when you include \c pylon/PylonIncludes.h.
  66. \error
  67. Can throw an exception if the passed winIndex is invalid or the window could not be created.
  68. \threading
  69. This function is thread-safe.
  70. */
  71. PYLONGUI_API void DisplayImage( size_t winIndex, const Pylon::IImage& image );
  72. /*!
  73. \class CPylonImageWindow
  74. \brief Class to manage a pylon image window.
  75. Use this class to manage a pylon image window.
  76. To diplay an image, you need to create and show it first. You can the call SetImage() to let the window display any %Pylon image. The following snippet shows how to do this:
  77. \code
  78. CPylonImageWindow win;
  79. // Create the window with winIndex = 1 using default position and size.
  80. // The window will not be visible until Show() has been called.
  81. win.Create(1);
  82. // Set the contents of the window.
  83. win.SetImage(myImage);
  84. // Make the window visible on the screen.
  85. win.Show();
  86. \endcode
  87. If the user closes the window, it will only be hidden and not truly closed. You can call Show() to make it visible again.
  88. The window will be closed (destroyed) when you call Close().
  89. The destructor automatically closes (i.e destroys) the window. To prevent this, you can call Detach() before the object is destroyed.
  90. All windows will be closed when PylonTerminate() is called.
  91. \remark The easiest way to display an image on screen is to use the DisplayImage() global function.
  92. \remark
  93. To use this class, you must explicitly include \c pylon/PylonGUIIncludes.h in your code.
  94. Unlike other pylon headers, the GUI headers will not be included automatically when you include \c pylon/PylonIncludes.h.
  95. */
  96. class PYLONGUI_API CPylonImageWindow
  97. {
  98. public:
  99. ///////////////////////////////////////////////////////////////////////
  100. /*!
  101. \brief Creates an empty CPylonImageWindow object. To actuall create the window, you must call Create()
  102. \error
  103. Does not throw C++ exceptions.
  104. */
  105. CPylonImageWindow( void );
  106. ///////////////////////////////////////////////////////////////////////
  107. /*!
  108. \brief Destroys a CPylonImageWindow object. If the window hasn't been closed, it will be closed.
  109. \error
  110. Does not throw C++ exceptions.
  111. \threading
  112. This function is thread-safe.
  113. */
  114. ~CPylonImageWindow( void );
  115. ///////////////////////////////////////////////////////////////////////
  116. /*!
  117. \brief Returns the window handle (HWND) for the image window.
  118. Returns the native window handle of the image window.
  119. The handle will become invalid after Close() has been called.
  120. \return Returns the native window handle (HWND) for the image window.
  121. \error
  122. Throws an exception if the window hasn't been created yet or the window has been already closed.
  123. \threading
  124. This function is thread-safe.
  125. */
  126. HWND GetWindowHandle() const;
  127. ///////////////////////////////////////////////////////////////////////
  128. /*!
  129. \brief Returns the window index handled by this object.
  130. The index will become valid if you call Create() or Attach().
  131. The index will become invalid when you call Close() or Detach().
  132. Valid window indexes range from 0 to 31.
  133. \return Returns the window index stored in this object.
  134. \error
  135. Does not throw C++ exceptions.
  136. \threading
  137. This function is thread-safe.
  138. \sa IsValid()
  139. */
  140. size_t GetWindowIndex( void ) const
  141. {
  142. return m_winIndex;
  143. }
  144. ///////////////////////////////////////////////////////////////////////
  145. /*!
  146. \brief Returns true if the object holds a valid window index.
  147. Returns true if the object holds a valid window index
  148. \return Returns \a true if the objects holds a valid window index. Otherwise, \a false is returned.
  149. \error
  150. Does not throw C++ exceptions.
  151. \threading
  152. This function is thread-safe.
  153. */
  154. bool IsValid( void ) const;
  155. ///////////////////////////////////////////////////////////////////////
  156. /*!
  157. \brief Attaches the window index passed in \a winIndex to this object.
  158. \param[in] winIndex The new window index to attach to this object. Valid window indexes range from 0 to 31.
  159. \error
  160. Throws an exception if the object already holds a valid window index. If you want to reuse an existing object, you should call Detach() or Close() to clear the object.
  161. \threading
  162. This function is thread-safe.
  163. */
  164. void Attach( size_t winIndex );
  165. ///////////////////////////////////////////////////////////////////////
  166. /*!
  167. \brief Detaches the window index from this object and returns the window index previously stored in the object.
  168. Detaches the window index from this object and returns the window index previously stored in the object.
  169. After this function returns the state of the object will be invalid.
  170. You can call this function to prevent the window being closed when the CPylonImageWindow is destroyed.
  171. If you don't close a CPylonImageWindow, it will be closed automatically when PylonTerminate() is called.
  172. \return Returns the current window index. If no window index is stored, invalidWinIndex will be returned.
  173. \error
  174. Does not throw C++ exceptions.
  175. \threading
  176. This function is thread-safe.
  177. */
  178. size_t Detach( void );
  179. ///////////////////////////////////////////////////////////////////////
  180. /*!
  181. \brief Creates a window for index passed in \a winIndex.
  182. Creates a window for the index passed in \a winIndex. If there is already an existing window with \a winIndex, an exception will be thrown.
  183. When you pass the \a CPylonImageWindow::useDefault for the \a x parameter, the window will be shown at the same position and with the same size as the previous one before it was closed.
  184. Otherwise, the position and size parameters are passed directly to the CreateWindow() function of the Windows API.
  185. When the function returns successfully, the window has been created but is not visible. You must call Show() to make the window visible.
  186. \param[in] winIndex The window used for displaying the image. Valid window indexes range from 0 to 31.
  187. \param[in] x The x-coordinate of the upper-left corner of the window in screen coordinates.
  188. You can pass the CPylonImageWindow::useDefault value to create the window at the same position as the previous one before it was closed. The other position parameters will be ignored.
  189. If the window is shown the very first time the system select a default position and size for the window.
  190. \param[in] y The y-coordinate of the upper-left corner of the window in screen-coordinates.
  191. \param[in] nWidth The width of the window in screen coordinates.
  192. \param[in] nHeight The height of the window in screen coordinates.
  193. \error
  194. <ul>
  195. <li>Will throw an exception if an invalid \a winIndex is passed.
  196. <li>Will throw an exception if the a window with the specified window index already exists.
  197. </ul>
  198. \threading
  199. This function is thread-safe.
  200. */
  201. void Create( size_t winIndex, int x = useDefault, int y = useDefault, int nWidth = useDefault, int nHeight = useDefault );
  202. ///////////////////////////////////////////////////////////////////////
  203. /*!
  204. \brief Shows or hides the image window.
  205. This function calls ShowWindow from the Windows API.
  206. You can pass the same values for the \a nShow parameter as described in the official documentation for ShowWindow()
  207. The default value \a nShowDefault (which equals SW_SHOW) will show the window in its current state and position.
  208. When the user closes the window it will be hidden, but not destroyed. You can call Show() to make it visible again.
  209. \remark After you create a window using Create() it will be invisible. You must call Show() to make the window visible.
  210. \error
  211. Throws an exception if the window hasn't been created yet and the object doesn't hold a valid window index.
  212. \threading
  213. This function is thread-safe.
  214. */
  215. void Show( int nShow = showDefault ) const;
  216. ///////////////////////////////////////////////////////////////////////
  217. /*!
  218. \brief Hides the image window.
  219. This is a convenience function to hide the window. It will call Show(SW_HIDE)
  220. \error
  221. Throws an exception if the window hasn't been created yet and the object doesn't hold a valid window index.
  222. \threading
  223. This function is thread-safe.
  224. \sa Show()
  225. */
  226. void Hide( void ) const;
  227. ///////////////////////////////////////////////////////////////////////
  228. /*!
  229. \brief Closes and destroys the window.
  230. Closes and destroys the window.
  231. When the function returns successfully, the object will be invalid and the stored window index will be set to invalid.
  232. If you don't close a CPylonImageWindow, it will be closed automatically when PylonTerminate() is called.
  233. \remark If you just want to hide the window from the user you should call Hide() instead.
  234. \error
  235. Does not throw C++ exceptions.
  236. \threading
  237. This function is thread-safe.
  238. */
  239. bool Close( void );
  240. ///////////////////////////////////////////////////////////////////////
  241. /*!
  242. \brief Sets the contents of the image window.
  243. Use this function to set the image shown in the window.
  244. You can pass any IImage based object (like CPylonImage or CPylonBitmapImage).
  245. You can also pass a GrabResultPtr object as it will convert its contents automatically to an IImage.
  246. If you pass an empty or invalid image the contents of the image window will be cleared.
  247. \param[in] image The image to display in the window.
  248. \error
  249. Throws an exception if the window hasn't been created yet and the object doesn't hold a valid window index.
  250. \threading
  251. This function is thread-safe.
  252. */
  253. void SetImage( const Pylon::IImage& image ); //accepts also CGrabResultPtr
  254. public:
  255. /// Default value for window position/size when calling Create()
  256. static const int useDefault = static_cast<int>(0x80000000); // CW_USEDEFAULT
  257. /// Default value for window position/size when calling Show()
  258. static const int showDefault = 5; // SW_SHOW
  259. protected:
  260. /// Invalid window index.
  261. static const size_t invalidWinIndex = static_cast<size_t>(-1);
  262. /// The window index managed by this object. Valid window indexes range from 0 to 31. If no window is currently managed, invalidWinIndex will be stored.
  263. size_t m_winIndex;
  264. protected:
  265. // no copy
  266. CPylonImageWindow( const CPylonImageWindow& );
  267. CPylonImageWindow& operator=( const CPylonImageWindow& );
  268. };
  269. /**
  270. * @}
  271. */
  272. }
  273. #endif /* __cplusplus */
  274. #pragma pack(pop)
  275. #endif /* PYLON_GUI_H_INCLUDED__ */