Click here to Skip to main content
16,016,760 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
for TWAIN i am referring this project GitHub - tmyroadctfig/twaindotnet

in this project their is WpfWindowMessageHook function which accepts the window.

But the problem is i want pass the USER CONTROL

I don't Know How to Pass The User Control Object to The WpfWindowMessageHook.

What I have tried:

and user control load :=

VB
_twain = New Twain(New WpfWindowMessageHook(Me))  
 
            AddHandler _twain.TransferImage, AddressOf TransferImage
 
            AddHandler _twain.ScanningComplete, AddressOf ScanComplete



the WpfWindowMessageHook funtion is

VB
public WpfWindowMessageHook(Window window)'How to Pass The user Control.
{
try
{

_source = (HwndSource)PresentationSource.FromDependencyObject(window);
_interopHelper = new WindowInteropHelper(window); 
}
catch (TwainException ex)
{
MessageBox.Show(ex.Message);
}

}
Posted
Updated 16-Nov-17 20:22pm
v3

1 solution

change in WpfWindowMessageHook.cs File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Interop;

namespace TwainDotNet.Wpf
{
    /// <summary>
    /// A windows message hook for WPF applications.
    /// </summary>
    /// 

    public class WpfWindowMessageHook : IWindowsMessageHook
    {
        HwndSource _source;
        System.IntPtr _interopHelper;
        WindowInteropHelper _window;
        bool FlagWindow;
      
        bool _usingFilter;
       
        //public System.IntPtr _handle = IntPtr.Zero;
    
        //public Window TheMainWindow { get { return App.Current.MainWindow; } }
        public WpfWindowMessageHook(Window window) 
        {
            _source = (HwndSource)PresentationSource.FromDependencyObject(window);
            _window = new WindowInteropHelper(window);
            FlagWindow = true;
        }
      
        public  WpfWindowMessageHook(System.Windows.Controls.UserControl window)
        {
           
            _source = (HwndSource)PresentationSource.FromDependencyObject(window);
            HwndSource source = (HwndSource)HwndSource.FromVisual(window);
        
          
            _interopHelper = source.Handle;
           

        }
  

        public IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (FilterMessageCallback != null)
            {
                return FilterMessageCallback(hwnd, msg, wParam, lParam, ref handled);
            }

            return IntPtr.Zero;
        }

        public bool UseFilter
        {
            get
            {
                return _usingFilter;
            }
            set 
            {
                if (!_usingFilter && value == true)
                {
                    _source.AddHook(FilterMessage);
                    _usingFilter = true;
                }

                if (_usingFilter && value == false)
                {
                    _source.RemoveHook(FilterMessage);
                    _usingFilter = false;
                }
            }
        }

        public FilterMessage FilterMessageCallback { get; set; }

        public IntPtr WindowHandle {
            get {
                if (FlagWindow == false)
                {
                    return _interopHelper;
                }
                else
                {
                   return _window.Handle;

                }
              
               
            } }

                    }
}
 
Share this answer
 
Comments
Graeme_Grant 17-Nov-17 3:22am    
Is this the answer to your problem or are you updating the question?
ketan Ram Patil 17-Nov-17 5:14am    
its solved :)
its an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900