E
Ed Sutton
How can a WndProc be created inside a component?
Currently I have a WndProc in my frmMain. It looks for WM_DEVICECHANGE
messages for connection and removal events for a USB/Serial device. My
frmMain uses a component to communicate with a USB/Serial device. I would
prefer that this component handle the WM_DEVICECHANGE messages internally
and only fires events to my frmMain when my USB device connects or
disconnects.
In case anyone is Googling for in WM_DEVICECHANGE I pasted the WndProc
method from my frmMain below:
Thanks in advance for any tips or suggestions,
-Ed Sutton
protected override void WndProc(ref Message message)
{
if(message.Msg == WM_DEVICECHANGE)
{
DeviceHelper.CPortInfo portInfo;
Debug.Write( "WM_DEVICECHANGE: ");
switch((int)message.WParam)
{
case DBT_DEVNODES_CHANGED:
Debug.WriteLine( "Device tree has changed");
break;
case DBT_DEVICEARRIVAL:
Debug.WriteLine( "System detected a new device");
enumerateSerialPorts();
// If we were previously connected to a device,
// automatically reconnect if we are reconnected
if( m_connectedPortInfoKey != null)
{
portInfo =
(DeviceHelper.CPortInfo)m_mapComportNamesToPortInfo[m_connectedPortInfoKey];
if(readerInterface.Open == false && portInfo != null )
{
readerInterface.CommPort = portInfo.PortNumber;
readerInterface.Open = true;
readerInterface.Ping();
}
}
break;
case DBT_DEVICEREMOVECOMPLETE:
Debug.WriteLine( "Device has been removed");
enumerateSerialPorts();
// If we were connected, and the device we were connected
to was removed, then disconnect it
// Automatically reconnect if it comes back
if( m_connectedPortInfoKey != null)
{
portInfo =
(DeviceHelper.CPortInfo)m_mapComportNamesToPortInfo[m_connectedPortInfoKey];
if( readerInterface.Open == true
&& portInfo == null)
{
readerInterface.Open = false;
MessageBox.Show("Please reconnect the reader or turn
power back on", "Device Removal" );
}
}
break;
default:
Debug.Write( String.Format( "What's this?? message.WParam
= {0:X}",message.WParam));
break;
}
}
base.WndProc(ref message);
}
Currently I have a WndProc in my frmMain. It looks for WM_DEVICECHANGE
messages for connection and removal events for a USB/Serial device. My
frmMain uses a component to communicate with a USB/Serial device. I would
prefer that this component handle the WM_DEVICECHANGE messages internally
and only fires events to my frmMain when my USB device connects or
disconnects.
In case anyone is Googling for in WM_DEVICECHANGE I pasted the WndProc
method from my frmMain below:
Thanks in advance for any tips or suggestions,
-Ed Sutton
protected override void WndProc(ref Message message)
{
if(message.Msg == WM_DEVICECHANGE)
{
DeviceHelper.CPortInfo portInfo;
Debug.Write( "WM_DEVICECHANGE: ");
switch((int)message.WParam)
{
case DBT_DEVNODES_CHANGED:
Debug.WriteLine( "Device tree has changed");
break;
case DBT_DEVICEARRIVAL:
Debug.WriteLine( "System detected a new device");
enumerateSerialPorts();
// If we were previously connected to a device,
// automatically reconnect if we are reconnected
if( m_connectedPortInfoKey != null)
{
portInfo =
(DeviceHelper.CPortInfo)m_mapComportNamesToPortInfo[m_connectedPortInfoKey];
if(readerInterface.Open == false && portInfo != null )
{
readerInterface.CommPort = portInfo.PortNumber;
readerInterface.Open = true;
readerInterface.Ping();
}
}
break;
case DBT_DEVICEREMOVECOMPLETE:
Debug.WriteLine( "Device has been removed");
enumerateSerialPorts();
// If we were connected, and the device we were connected
to was removed, then disconnect it
// Automatically reconnect if it comes back
if( m_connectedPortInfoKey != null)
{
portInfo =
(DeviceHelper.CPortInfo)m_mapComportNamesToPortInfo[m_connectedPortInfoKey];
if( readerInterface.Open == true
&& portInfo == null)
{
readerInterface.Open = false;
MessageBox.Show("Please reconnect the reader or turn
power back on", "Device Removal" );
}
}
break;
default:
Debug.Write( String.Format( "What's this?? message.WParam
= {0:X}",message.WParam));
break;
}
}
base.WndProc(ref message);
}