【发布时间】:2018-09-30 13:53:35
【问题描述】:
我在 UWP 编程方面没有太多经验,并且我构建了一个应用程序在蓝牙上做一些事情。现在我必须通过 API 传递一些信息。但不知何故“SendMessage”不起作用。这是我正在使用的代码
public static class ApiComm
{
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);
public static string DelphiFormClassName = "TFrmMain";//"wsdk_delphi_api_test";
public static string DelphiFormCaptionName = "Deneme";
private static IntPtr FindDelphiForm(string delphiFormClassName, string delphiFormCaptionName)
{
return FindWindow(delphiFormClassName, delphiFormCaptionName);
}
private static IntPtr FindDelphiForm()
{
return FindDelphiForm(DelphiFormClassName, DelphiFormCaptionName);
}
public static int TextToID(string text)
{
int mu = (text == null ? 0 : text.Length);
int result = 0;
for (int i = 0; i < mu; i++)
result = result + (((i + 1) * 256) + (byte)(text[i]));
return result;
}
private const int WM_COMMAND = 0x0111;
private const int StartFrom = 500;
private static int EventID(RemoteDeviceUpdate anEvent)
{
return StartFrom + (int)anEvent;
}
public static void SendInfo(int remoteID, RemoteDeviceUpdate anEvent)
{
IntPtr wP = FindDelphiForm();
if (wP != null && wP != IntPtr.Zero)
{
int eID = EventID(anEvent);
SendMessage(wP, WM_COMMAND, eID, remoteID);
}
}
public static void SendInfo(string remoteID, RemoteDeviceUpdate anEvent)
{
SendInfo(TextToID(remoteID), anEvent);
}
}
当我尝试将 SendInfo 用于另一个 .net 项目时,它工作得很好,但在 UWP 中却没有(而且它也没有给出任何错误)
我必须在我的项目中添加包或其他东西吗..
任何帮助将不胜感激,谢谢。
【问题讨论】: