日期:2014-05-17  浏览次数:20850 次

C# 与C++怎样通过CopyData传送字体
现在C#的结构体里有字体类型Font font;,C++的结构体应该用什么类型与之对应呢?

------解决方案--------------------
你是想通过WM_COPYDATA传递字体的FONT内容吧,直接传地址即可(32位整数),接收方根据地址解析出FONT的内容。
------解决方案--------------------
[C# Code]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);

private void SendMsg(string args, string name)
{
string txtContent = args;
int WINDOW_HANDLER = (int)FindWindow(null, name);

if (WINDOW_HANDLER != 0)
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(txtContent);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = txtContent;
cds.cbData = len + 1;
SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);
}
}
[code]