Winform Form Class에서는 윈도우 프로시져를 오버라이드 할 수 있게 제공해주지만

 

WPF Window Class에는 제공해주지 않고 있습니다.

 

아래의 코드와 같이 하시면 윈도우 프로시져를 확인 하실 수 있습니다.

 

 

 

        [DllImport("user32.dll")]

        static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);

        private const int WM_DRAWCLIPBOARD = 0x308;

        private const int WM_CHANGECBCHAIN = 0x30D;

 

        void OnLoaded(object sender, RoutedEventArgs e)

        {           

            HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);

            source.AddHook(new HwndSourceHook(WndProc));

 

 

            IntPtr mNextClipBoardViewerHWnd = SetClipboardViewer(new System.Windows.Interop.WindowInteropHelper(this).Handle);

        }

 

 

 

 

 

 

 

 

protected override void WndProc(ref Message m)

{

      UInt32 WM_DEVICECHANGE = 0x0219;

      UInt32 DBT_DEVTUP_VOLUME = 0x02;

      UInt32 DBT_DEVICEARRIVAL = 0x8000;

      UInt32 DBT_DEVICEREMOVECOMPLETE = 0x8004;

if ((m.Msg == WM_DEVICECHANGE) && (m.WParam.ToInt32() == DBT_DEVICEARRIVAL)) //디바이스 연결

{

      int devType = Marshal.ReadInt32(m.LParam, 4);

      if (devType == DBT_DEVTUP_VOLUME)

      {

            RefreshDevice();

      }

}

if ((m.Msg == WM_DEVICECHANGE) && (m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)) //디바이스 연결 해제

{

      int devType = Marshal.ReadInt32(m.LParam, 4);

      if (devType == DBT_DEVTUP_VOLUME)

      {

            RefreshDevice();

      }

}

 

base.WndProc(ref m);

}

 

 

 

public void RefreshDevice()

{

            listBox1.Items.Clear();

            string[] ls_drivers = System.IO.Directory.GetLogicalDrives(); //연결 되어있는 디바이스 얻어오기

            foreach (string device in ls_drivers)

            {

            System.IO.DriveInfo dr = new System.IO.DriveInfo(device);

            if (dr.DriveType == System.IO.DriveType.Removable) //제거 가능한 타입이라면

            {

                  listBox1.Items.Add(device);

            }

      }

}

 

+ Recent posts