📄 frmmain.cs
字号:
if (Success)
{
lstResults.Items.Add("An Input report has been read.");
Debug.WriteLine(" Input Report ID: " + InputReportBuffer[0]);
Debug.WriteLine(" Input Report Data:");
// Display the report data received in the form's list box.
lstResults.Items.Add(" Input Report ID: " + InputReportBuffer[0]);
lstResults.Items.Add(" Input Report Data:");
txtBytesReceived.Text = "";
for (Count = 0; Count < InputReportBuffer.Length; Count++)
{
// Add a leading zero to values from 0 to F.
if (InputReportBuffer[Count].ToString("x").Length < 2)
{
ByteValue = "0" + InputReportBuffer[Count].ToString("x");
}
else
{
ByteValue = InputReportBuffer[Count].ToString("x");
}
Debug.WriteLine(" " + ByteValue);
lstResults.Items.Add(" " + ByteValue);
// Display the received bytes in the "Received Bytes" text box.
txtBytesReceived.SelectionStart = txtBytesReceived.Text.Length;
txtBytesReceived.SelectedText = ByteValue + "\r\n";
}
}
else
{
lstResults.Items.Add("The attempt to read an Input report has failed.");
}
ScrollToBottomOfListBox();
// Enable requesting another transfer.
cmdOnce.Enabled = true;
}
else
{
// Read a report using interrupt transfers.
// To enable reading a report without blocking the main thread, this
// application uses an asynchronous delegate.
IAsyncResult ar;
Hid.InputReport myInputReport = new Hid.InputReport();
// Define a delegate for the Read method of myInputReport.
ReadInputReportDelegate MyReadInputReportDelegate = new ReadInputReportDelegate(myInputReport.Read);
// The BeginInvoke method calls myInputReport.Read to attempt to read a report.
// The method has the same parameters as the Read function,
// plus two additional parameters:
// GetInputReportData is the callback procedure that executes when the Read function returns.
// MyReadInputReportDelegate is the asynchronous delegate object.
// The last parameter can optionally be an object passed to the callback.
ar = MyReadInputReportDelegate.BeginInvoke
(_ReadHandle,
_HIDHandle,
_WriteHandle,
ref _MyDeviceDetected,
ref InputReportBuffer,
ref Success,
new AsyncCallback(GetInputReportData),
MyReadInputReportDelegate);
}
}
else
{
lstResults.Items.Add("No attempt to read an Input report was made.");
lstResults.Items.Add("The HID doesn't have an Input report.");
}
}
else
{
lstResults.Items.Add("Invalid handle. Device is probably a system mouse or keyboard.");
lstResults.Items.Add("No attempt to write an Output report or read an Input report was made.");
}
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void frmMain_Closed (System.Object eventSender, System.EventArgs eventArgs)
{
try {
// Perform shutdown operations.
Shutdown();
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void frmMain_Load (System.Object eventSender, System.EventArgs eventArgs)
{
// Perform startup operations.
try {
frmMy = this; //GRV
this.Show();
Startup();
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void GetInputReportBufferSize ()
{
// Purpose : Finds and displays the number of Input buffers
// : (the number of Input reports the host will store).
int NumberOfInputBuffers = 0;
try {
// Get the number of input buffers.
_MyHID.GetNumberOfInputBuffers(_HIDHandle, ref NumberOfInputBuffers);
// Display the result in the text box.
txtInputReportBufferSize.Text = System.Convert.ToString(NumberOfInputBuffers);
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void GetInputReportData (IAsyncResult ar)
{
// Purpose : Retrieves Input report data and status information.
// : This routine is called automatically when myInputReport.Read
// : returns.
// Accepts : ar - an object containing status information about
// : the asynchronous operation.
//GRV - Marshaling added/corrected
string ByteValue;
int Count;
byte[] InputReportBuffer = new byte[0];
bool Success = false;
try
{
// Define a delegate using the IAsyncResult object.
ReadInputReportDelegate deleg = ((ReadInputReportDelegate)(ar.AsyncState));
// Get the IAsyncResult object and the values of other paramaters that the
// BeginInvoke method passed ByRef.
deleg.EndInvoke(ref _MyDeviceDetected, ref InputReportBuffer, ref Success, ar);
// Display the received report data in the form's list box.
if (ar.IsCompleted & Success) {
MyMarshalToForm("AddItemToListBox", "An Input report has been read.");
Debug.WriteLine(" Input Report ID: " + InputReportBuffer[0]);
Debug.WriteLine(" Input Report Data:");
MyMarshalToForm("AddItemToListBox", " Input Report ID: " + InputReportBuffer[0].ToString());
MyMarshalToForm("AddItemToListBox", " Input Report Data:");
for (Count = 1; Count < InputReportBuffer.Length; Count++) {
// Add a leading zero to values from 0 to F.
if (InputReportBuffer[Count].ToString("x").Length < 2) {
ByteValue = "0" + InputReportBuffer[Count].ToString("x");
} else {
ByteValue = InputReportBuffer[Count].ToString("x");
}
Debug.WriteLine(" " + ByteValue);
MyMarshalToForm("AddItemToListBox", " " + ByteValue);
// Display the report data in the "Received Data" text box.
MyMarshalToForm("TextBoxSelectionStart", txtBytesReceived.Text);
MyMarshalToForm("AddItemToTextBox", ByteValue);
}
} else {
MyMarshalToForm("AddItemToListBox", "The attempt to read an Input report has failed.");
Debug.Write("The attempt to read an Input report has failed");
}
MyMarshalToForm("ScrollToBottomOfListBox" , "");
MyMarshalToForm("EnableCmdOnce","");
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void GetVendorAndProductIDsFromTextBoxes (ref short myVendorID, ref short myProductID)
{
// Purpose : Retrieves a Vendor ID and Product ID in hexadecimal
// : from the form's text boxes and converts the text to Shorts.
// Accepts : myVendorID - the Vendor ID as a Short.
// : myProductID - the Product ID as a Short.
try {
//myVendorID = System.Convert.ToInt16(Conversion.Val("&h" + txtVendorID.Text)) - 1107;
//myProductID = System.Convert.ToInt16(Conversion.Val("&h" + txtProductID.Text)) - offp;
myVendorID = short.Parse(txtVendorID.Text, System.Globalization.NumberStyles.HexNumber);
myProductID = short.Parse(txtProductID.Text, System.Globalization.NumberStyles.HexNumber);
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void InitializeDisplay ()
{
// Purpose : Initialize the elements on the form.
string ByteValue;
try {
// Create a dropdown list box for each byte to send in a report.
// Add a leading zero for values from 0 to F.
for (int i = 0; i <= 255; i++) {
if (i.ToString("x").Length < 2) {
ByteValue = "0" + i.ToString("x");
} else {
ByteValue = i.ToString("x");
}
this.cboByte0.Items.Insert(i, ByteValue);
}
for (int i = 0; i <= 255; i++) {
if (i.ToString("x").Length < 2) {
ByteValue = "0" + i.ToString("x");
} else {
ByteValue = i.ToString("x");
}
this.cboByte1.Items.Insert(i, ByteValue);
}
// Select a default value for each box
this.cboByte0.SelectedIndex = 0;
this.cboByte1.SelectedIndex = 128;
// Check the autoincrement box to increment the values each time a report is sent.
chkAutoincrement.CheckState = System.Windows.Forms.CheckState.Checked;
// Don't allow the user to select an input report buffer size until there is
// a handle to a HID.
cmdInputReportBufferSize.Enabled = false;
if (_MyHID.IsWindowsXpOrLater()) {
chkUseControlTransfersOnly.Enabled = true;
} else {
// If the operating system is earlier than Windows XP,
// disable the option to force Input and Output reports to use control transfers
// (not supported).
chkUseControlTransfersOnly.Enabled = false;
}
lstResults.Items.Add("For a more detailed event log, view debug statements in Visual Studio's Output window:");
lstResults.Items.Add("Click Build > Configuration Manager > Active Solution Configuration > Debug.");
lstResults.Items.Add("Then click View > Other Windows > Output.");
lstResults.Items.Add("");
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
void M
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -