shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.42k stars 1.15k forks source link

How to use opencvsharp to make imshow's window be the child of one picturebox ? #519

Closed futureflsl closed 5 years ago

futureflsl commented 6 years ago

I know in MFC,we can use the code make imshow's window be the child of one picture control void FillPicCtrl(int nID,CString strTitle)

{
    CRect rect;
    GetDlgItem(nID)->GetClientRect(rect);
    namedWindow((LPCTSTR)strTitle, WINDOW_NORMAL);
    resizeWindow((LPCTSTR)strTitle, rect.Width(), rect.Height());

    HWND hWnd = (HWND)cvGetWindowHandle((LPCTSTR)strTitle);
    HWND hParent = ::GetParent(hWnd);
    ::SetParent(hWnd, GetDlgItem(nID)->m_hWnd);
    ::ShowWindow(hParent, SW_HIDE);
}

now,I try to use the same method in opencvsharp

       [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
        public static extern IntPtr GetParent(IntPtr hWnd);
        [DllImport("user32.dll", EntryPoint = "SetParent")]
        public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); 
        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
      public void FillControl(PictureBox pic,string title)
        {

    Cv2.NamedWindow(title,WindowMode.AutoSize);
    Cv2.ResizeWindow(title, pic.Width, pic.Height);
     IntPtr hWnd = FindWindow("Main HighGUI class", title);
    //IntPtr hParent = GetParent(hWnd);
    SetParent(hWnd, pic.Handle);
    //ShowWindow(pic.Handle, 0);
        }

But It doesn't Work,Someone Can solve this problem?

shimat commented 6 years ago

I'm sorry but I don't really understand what you want to do. I tried the following code and it works.

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using OpenCvSharp;

namespace OpenCvSharpSamples
{
    class Program
    {
        [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
        public static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "SetParent")]
        public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        //[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        //public void FillControl(PictureBox pic, string title);

        [STAThread]
        static void Main(string[] args)
        {
            using (var bitmap = new Bitmap("lenna.png"))
            using (var mat = new Mat("a.jpg"))
            {
                var pictureBox = new PictureBox();
                pictureBox.Image = bitmap;
                pictureBox.ClientSize = new System.Drawing.Size(bitmap.Width, bitmap.Height);
                var form = new Form();
                form.Text = "WinForm";
                form.ClientSize = new System.Drawing.Size(800, 600);
                form.Controls.Add(pictureBox);

                const string title = "Main Window";
                Cv2.NamedWindow(title, WindowMode.AutoSize);
                Cv2.ImShow(title, mat);
                Cv2.ResizeWindow(title, pictureBox.Width, pictureBox.Height);

                IntPtr hWnd = FindWindow("Main HighGUI class", title);
                SetParent(hWnd, pictureBox.Handle);

                Application.Run(form);
            }
        }
    }
}

default