Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not pass winform control size into unmanaged code

Tags:

c++

c#

winforms

mfc

I use unmanaged libraries to obtain video stream from IP Camera. There is function:

[DllImport("client.dll", EntryPoint = "Network_ClientStartLive", SetLastError = true)]
protected static extern int Network_ClientStartLive(
        ref IntPtr pStream,
        IntPtr hDev,
        IntPtr pClientInfo,
        [MarshalAs(UnmanagedType.FunctionPtr)] ReadDatacbf lpfnCallbackFunc = null,
        UInt32 dwUserData = 0
    );

The pClientInfo is a pointer to structure type of:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
protected struct LiveConnect
{
    public UInt32 dwChannel;
    public IntPtr hPlayWnd;
    public UInt32 dwConnectMode;
}

where hPlayWnd is a handle of window in which video stream must be output. The library detects the video resolution by size of this window (during the call to Network_ClientStartLive). I checked it on C++ MFC program, where output window is Picture control and by setting size with method MoveWindow was defined output video resolution.

In the C# version of this program I'm using a PictureBox-control to draw the video stream. The video is displayed but the size of the PictureBox does not affect to video stream resolution. I tried several methods to change PictureBox size:

  1. setting pictureBox.Size
  2. using WinAPI SetWindowPos:
[DllImport("user32.dll")]
private static extern bool SetWindowPos(
    IntPtr hWnd, 
    IntPtr hWndInsertAfter,
    int x, 
    int y, 
    int width, 
    int height, 
    uint uFlags);

In both methods the size of the control was changed but the camera library continued to output video stream in maximum resolution.

How can I solve this problem?

Thanks!

like image 363
snk Avatar asked Apr 16 '13 07:04

snk


1 Answers

Every control in Windows Forms has a SizeChanged (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.sizechanged(v=vs.110).aspx) event. Maybe its possible to add code to change the video resolution manually in this event handler? If not, the PictureBox handle you provide may not be sending WM_SIZE messages which would be what the unmanaged library would be looking for. As mentioned in one of the comments, Spy++ (included with Visual Studio) would be a useful program to monitor the messages and make sure that the handle values and the events are what you expect them to be.

like image 118
Caleb Everett Avatar answered Oct 20 '22 11:10

Caleb Everett