Skip to content

Commit

Permalink
Merge pull request #1177 from shimat/window_handle
Browse files Browse the repository at this point in the history
organize window constructors, rename enums, add handle getter
  • Loading branch information
shimat authored Jan 17, 2021
2 parents f80a2f8 + dcb34d9 commit 74f6777
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 110 deletions.
20 changes: 17 additions & 3 deletions src/OpenCvSharp/Cv2/Cv2_highgui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static partial class Cv2
/// Flags of the window. Currently the only supported flag is CV WINDOW AUTOSIZE. If this is set,
/// the window size is automatically adjusted to fit the displayed image (see imshow ), and the user can not change the window size manually.
/// </param>
public static void NamedWindow(string winName, WindowMode flags = WindowMode.Normal)
public static void NamedWindow(string winName, WindowFlags flags = WindowFlags.Normal)
{
if (string.IsNullOrEmpty(winName))
throw new ArgumentException("null or empty string.", nameof(winName));
Expand Down Expand Up @@ -147,7 +147,7 @@ public static void MoveWindow(string winName, int x, int y)
/// <param name="winName">Name of the window.</param>
/// <param name="propId">Window property to retrieve.</param>
/// <param name="propValue">New value of the window property.</param>
public static void SetWindowProperty(string winName, WindowProperty propId, double propValue)
public static void SetWindowProperty(string winName, WindowPropertyFlags propId, double propValue)
{
if (string.IsNullOrEmpty(winName))
throw new ArgumentException("null or empty string.", nameof(winName));
Expand Down Expand Up @@ -178,7 +178,7 @@ public static void SetWindowTitle(string winName, string title)
/// <param name="winName">Name of the window.</param>
/// <param name="propId">Window property to retrieve.</param>
/// <returns></returns>
public static double GetWindowProperty(string winName, WindowProperty propId)
public static double GetWindowProperty(string winName, WindowPropertyFlags propId)
{
if (string.IsNullOrEmpty(winName))
throw new ArgumentException("null or empty string.", nameof(winName));
Expand Down Expand Up @@ -454,6 +454,20 @@ public static void SetTrackbarMin(string trackbarName, string winName, int minVa
NativeMethods.highgui_setTrackbarMin(trackbarName, winName, minVal));
}

/// <summary>
/// get native window handle (HWND in case of Win32 and Widget in case of X Window)
/// </summary>
/// <param name="windowName"></param>
public static IntPtr GetWindowHandle(string windowName)
{
if (windowName == null)
throw new ArgumentNullException(nameof(windowName));

NativeMethods.HandleException(
NativeMethods.highgui_cvGetWindowHandle(windowName, out var ret));
return ret;
}

#if WINRT
// MP! Added: To correctly support imShow under WinRT.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public static extern ExceptionStatus highgui_getTrackbarPos(
//public static extern ExceptionStatus highgui_createButton(
// [MarshalAs(UnmanagedType.LPStr)] string barName, IntPtr onChange, IntPtr userData, int type, int initialButtonState, out int returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus highgui_cvGetWindowHandle([MarshalAs(UnmanagedType.LPStr)] string name, out IntPtr returnValue);

#if WINRT
// MP! Added: To correctly support imShow under WinRT.
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,21 @@

namespace OpenCvSharp
{
#if LANG_JP
/// <summary>
/// cvNamedWindowで使用するウィンドウのフラグ
/// Flags for cv::namedWindow
/// </summary>
#else
/// <summary>
/// Flags for the window
/// </summary>
#endif
[Flags]
public enum WindowMode
public enum WindowFlags
{
/// <summary>
/// the user can resize the window (no constraint) /
/// also use to switch a fullscreen window to a normal size
/// </summary>
Normal = 0x00000000,

#if LANG_JP
/// <summary>
/// 表示される画像サイズに合わせてウィンドウサイズが自動的に調整される
/// the user cannot resize the window, the size is constrainted by the image displayed.
/// </summary>
#else
/// <summary>
/// the user cannot resize the window, the size is constrainted by the image displayed
/// </summary>
#endif
AutoSize = 0x00000001,

/// <summary>
Expand All @@ -50,6 +38,16 @@ public enum WindowMode
/// <summary>
/// the ratio of the image is respected
/// </summary>
KeepRatio = 0x00000000
KeepRatio = 0x00000000,

/// <summary>
/// status bar and tool bar
/// </summary>
GuiExpanded = 0x00000000,

/// <summary>
/// old fashious way
/// </summary>
GuiNormal = 0x00000010,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@

namespace OpenCvSharp
{
#if LANG_JP
/// <summary>
/// ウィンドウのプロパティを取得・設定する際のプロパティID(cvGetWindowProperty/cvSetWindowProperty)
/// </summary>
#else
/// <summary>
/// Property identifiers for cvGetWindowProperty/cvSetWindowProperty
/// </summary>
#endif
public enum WindowProperty
public enum WindowPropertyFlags
{
/// <summary>
/// fullscreen property (can be WINDOW_NORMAL or WINDOW_FULLSCREEN)
Expand All @@ -33,5 +27,15 @@ public enum WindowProperty
/// opengl support
/// </summary>
OpenGL = 3,

/// <summary>
/// checks whether the window exists and is visible
/// </summary>
Visible = 4,

/// <summary>
/// property to toggle normal window being topmost or not
/// </summary>
Topmost = 5
}
}
111 changes: 29 additions & 82 deletions src/OpenCvSharp/Modules/highgui/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class Window : DisposableObject
/// </summary>
#endif
public Window()
: this(DefaultName(), WindowMode.AutoSize, null)
: this(DefaultName(), WindowFlags.AutoSize, null)
{
}

Expand All @@ -62,83 +62,11 @@ public Window()
/// <param name="image"></param>
#endif
public Window(Mat image)
: this(DefaultName(), WindowMode.AutoSize, image)
: this(DefaultName(), WindowFlags.AutoSize, image)
{

}

#if LANG_JP
/// <summary>
/// 適当なウィンドウ名で、画像の表示モードを指定して初期化
/// </summary>
/// <param name="flags">ウィンドウのフラグ</param>
/// <param name="image">ウィンドウに表示する画像</param>
#else
/// <summary>
/// Creates a window with a specified image and flag
/// </summary>
/// <param name="flags">Flags of the window. Currently the only supported flag is WindowMode.AutoSize.
/// If it is set, window size is automatically adjusted to fit the displayed image (see cvShowImage), while user can not change the window size manually. </param>
/// <param name="image"></param>
#endif
public Window(WindowMode flags, Mat? image)
: this(DefaultName(), flags, image)
{
}

#if LANG_JP
/// <summary>
/// ウィンドウ名を指定して初期化
/// </summary>
/// <param name="name">ウィンドウの識別に用いられるウィンドウ名で,ウィンドウのタイトルバ ーに表示される.</param>
#else
/// <summary>
/// Creates a window
/// </summary>
/// <param name="name">Name of the window which is used as window identifier and appears in the window caption. </param>
#endif
public Window(string name)
: this(name, WindowMode.AutoSize, null)
{
}

#if LANG_JP
/// <summary>
/// ウィンドウ名と画像の表示モードを指定して初期化
/// </summary>
/// <param name="name">ウィンドウの識別に用いられるウィンドウ名で,ウィンドウのタイトルバ ーに表示される.</param>
/// <param name="flags">ウィンドウのフラグ</param>
#else
/// <summary>
/// Creates a window
/// </summary>
/// <param name="name">Name of the window which is used as window identifier and appears in the window caption. </param>
/// <param name="flags">Flags of the window. Currently the only supported flag is WindowMode.AutoSize.
/// If it is set, window size is automatically adjusted to fit the displayed image (see cvShowImage), while user can not change the window size manually. </param>
#endif
public Window(string name, WindowMode flags)
: this(name, flags, null)
{
}

#if LANG_JP
/// <summary>
/// ウィンドウ名と始めから表示しておく画像を指定して初期化
/// </summary>
/// <param name="name">ウィンドウの識別に用いられるウィンドウ名で,ウィンドウのタイトルバ ーに表示される.</param>
/// <param name="image">ウィンドウに表示する画像</param>
#else
/// <summary>
/// Creates a window
/// </summary>
/// <param name="name">Name of the window which is used as window identifier and appears in the window caption. </param>
/// <param name="image">Image to be shown.</param>
#endif
public Window(string name, Mat? image)
: this(name, WindowMode.AutoSize, image)
{
}


#if LANG_JP
/// <summary>
/// ウィンドウ名と画像の表示モードと始めから表示しておく画像を指定して初期化
Expand All @@ -155,15 +83,24 @@ public Window(string name, Mat? image)
/// If it is set, window size is automatically adjusted to fit the displayed image (see cvShowImage), while user can not change the window size manually. </param>
/// <param name="image">Image to be shown.</param>
#endif
public Window(string name, WindowMode flags, Mat? image)
public Window(string name, WindowFlags flags = WindowFlags.AutoSize, Mat? image = null)
{
this.name = name ?? throw new ArgumentNullException(nameof(name));
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("Null or empty window name.", nameof(name));
}

this.name = name;
NativeMethods.HandleException(
NativeMethods.highgui_namedWindow(name, (int) flags));

this.image = image;
ShowImage(image);
if (image != null)
{
ShowImage(image);
}

trackbars = new Dictionary<string, CvTrackbar>();

if (!Windows.ContainsKey(name))
{
Windows.Add(name, this);
Expand Down Expand Up @@ -416,7 +353,7 @@ public void DisplayStatusBar(string text, int delayms)
/// <param name="propId">Property identifier</param>
/// <returns>Value of the specified property</returns>
#endif
public double GetProperty(WindowProperty propId)
public double GetProperty(WindowPropertyFlags propId)
{
return Cv2.GetWindowProperty(name, propId);
}
Expand Down Expand Up @@ -522,7 +459,7 @@ public void SaveWindowParameters()
/// <param name="propId">Property identifier</param>
/// <param name="propValue">New value of the specified property</param>
#endif
public void SetProperty(WindowProperty propId, double propValue)
public void SetProperty(WindowPropertyFlags propId, double propValue)
{
Cv2.SetWindowProperty(name, propId, propValue);
}
Expand Down Expand Up @@ -572,6 +509,16 @@ public void ShowImage(UMat? img)

#endregion

/// <summary>
/// get native window handle (HWND in case of Win32 and Widget in case of X Window)
/// </summary>
public IntPtr GetHandle()
{
NativeMethods.HandleException(
NativeMethods.highgui_cvGetWindowHandle(name, out var ret));
return ret;
}

#if LANG_JP
/// <summary>
/// 何かキーが押されるか、若しくはdelayで指定した時間(ミリ秒)待機する。
Expand Down Expand Up @@ -657,7 +604,7 @@ public static void ShowImages(IEnumerable<Mat> images, IEnumerable<string> names
var windows = new List<Window>();
for (var i = 0; i < imagesArray.Length; i++)
{
windows.Add(new Window(namesArray[i], imagesArray[i]));
windows.Add(new Window(namesArray[i], image: imagesArray[i]));
}

Cv2.WaitKey();
Expand Down
8 changes: 8 additions & 0 deletions src/OpenCvSharpExtern/highgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ CVAPI(ExceptionStatus) highgui_setTrackbarMin(const char *trackbarName, const ch
END_WRAP
}*/

CVAPI(ExceptionStatus) highgui_cvGetWindowHandle(const char* name, void **returnValue)
{
BEGIN_WRAP
*returnValue = cvGetWindowHandle(name);
END_WRAP
}


#ifdef _WINRT_DLL
CVAPI(ExceptionStatus) highgui_initContainer(::Windows::UI::Xaml::Controls::Panel^ panel)
{
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharpExtern/include_opencv.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#ifdef _WINRT_DLL
#include <opencv2/highgui/highgui_winrt.hpp>
#endif
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/shape.hpp>
#include <opencv2/stitching.hpp>
Expand Down
5 changes: 3 additions & 2 deletions test/OpenCvSharp.Tests/highgui/HighGuiTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using System;
using Xunit;

namespace OpenCvSharp.Tests.HighGui
{
Expand Down Expand Up @@ -26,6 +27,7 @@ public void Window()
using (var window = new Window("window01"))
{
window.ShowImage(img);
Assert.NotEqual(IntPtr.Zero, window.GetHandle());
Cv2.WaitKey();
}
using (var window = new Window("window02"))
Expand All @@ -37,4 +39,3 @@ public void Window()
}
}
}

0 comments on commit 74f6777

Please sign in to comment.