using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Layout;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;
using HANDLE = System.IntPtr;
namespace ADCTest
{
public partial class Dialog : Form
{
private delegate void InvokeDelegate();
private delegate void UpdateStatusDelegate(TextBox text_b, String message);
private const int GWL_WNDPROC = -4;
private const int WM_SETTINGCHANGE = 0x1A;
//委托方法的定义
const int WM_COPYDATA = 0x004A;
const uint STANDARD_RIGHTS_REQUIRED = 0x000F0000;
const uint SYNCHRONIZE = 0x00100000;
const uint EVENT_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3);
const uint EVENT_MODIFY_STATE = 0x0002;
const long ERROR_FILE_NOT_FOUND = 2L;
delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("coredll.dll", EntryPoint = "GetWindowLong")]
private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("coredll.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr newWndProc);
[DllImport("coredll.dll")]
static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private static IntPtr oldWndProc = IntPtr.Zero;
private static WndProcDelegate newWndProc;
[DllImport("Coredll.dll", SetLastError = true)]
static extern IntPtr OpenEvent(uint dwDesiredAccess, bool bInheritHandle, string lpName);
public Dialog()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "ADC Testing";
newWndProc = new WndProcDelegate(WndProc);
oldWndProc = GetWindowLong(this.Handle, GWL_WNDPROC);
int success = SetWindowLong(this.Handle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(newWndProc));
HANDLE ehandle = OpenEvent(EVENT_ALL_ACCESS, false, "ADC_EVENT_FROM_FRIENDLYARM");
if (ehandle == null)
MessageBox.Show("open error");
SetEvent(ehandle);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.Handle.ToString());
}
public IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
// switch (msg)
// {
if (msg == WM_COPYDATA)
{
MessageBox.Show("Catch the msg");
COPYDATASTRUCT mystr = new COPYDATASTRUCT();
mystr = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));
// MessageBox.Show(mystr.lpData.ToString());
DoUpdate(textBox1, mystr.dwData.ToString());
}
else
DoUpdate(textBox1, "NULL");
// break;
// default:
//MessageBox.Show("test");
// DoUpdate(textBox1,"test");
// break;
// }
return CallWindowProc(oldWndProc, this.Handle, msg, wParam, lParam);
}
private void DoUpdate(TextBox text_b, string message) //委托修改textBox
{
if (this.InvokeRequired)
{
this.Invoke(new UpdateStatusDelegate(DoUpdate), new Object[] { text_b, message });
return;
}
text_b.Text = message;
}
[DllImport("coredll.dll", SetLastError = true)] //start event
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EventModify(HANDLE hEvent, [In, MarshalAs(UnmanagedType.U4)] int dEvent);
public enum EventFlags
{
PULSE = 1,
RESET = 2,
SET = 3
}
private static bool SetEvent(HANDLE hEvent)
{
return EventModify(hEvent, (int)EventFlags.SET);
}
}
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public string lpData;
}
}