如何傳遞Delphi窗體作爲參數傳遞給在c#創建的DLL的函數。我想將形式作爲參數傳遞給函數。 錯誤是: 不兼容的類型: '級參考' 和 '_form'如何傳遞形式作爲參數傳遞給在c#創建的DLL的函數在Delphi 5
C#代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace s3
{
//g1b572e8-7888-47e4-98t1-fe0e15855r32
[Guid("e1b572e8-7888-47e4-98e1-fe0e15855f49"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
public interface setmonitorwindow
{
void Concatenate([MarshalAs(UnmanagedType.LPWStr)] Form f1);
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form1 f2 = this;
Class1 c1 = new Class1();
c1.Concatenate(f2);
}
}
[Guid("76663fdc-8bb8-4e68-82j5-a110aa3c0uf2"),ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class Class1 : setmonitorwindow
{
[return: MarshalAs(UnmanagedType.SysInt)]
public void Concatenate([MarshalAs(UnmanagedType.LPWStr)] Form f1) //this function i wan call
{
var screen1 = Screen.FromPoint(Cursor.Position);
f1.StartPosition = FormStartPosition.Manual;
f1.Left = screen1.Bounds.Left + screen1.Bounds.Width/2 - f1.Width/2;
f1.Top = screen1.Bounds.Top + screen1.Bounds.Height/2 - f1.Height/2;
}
}
}
Delphi代碼:
begin
objsetmonitor := CoClass1.Create;
objsetmonitor.Concatenate(TForm1);
Form2.Show;
end;
我嘗試過其他方式也可作爲由DavidHeffernan爵士指定如下:
我執行以下代碼:
Function MonitorFromWindow(hwnd: HWND;dwFlags:DWORD):HWND; stdcall; external 'User32.dll';
procedure TForm2.Button1Click(Sender: TObject);
begin
result:=MonitorFromWindow(Form3.Handle,MONITOR_DEFAULTTONEAREST);
Form3.Show;
end;
但現在MONITOR_DEFAULTTONEAREST是在Delphi 5未聲明的標識符如何聲明它。也請告訴我是我正確的方式?
你打算用這種形式做在你的C#的dll? –
可以在顯示光標的同一顯示器上打開窗體。 – user7424581
您無法從c#訪問您的表單參數。它是一個Delphi類實例,與c#類實例不兼容。你爲什麼不把邏輯添加到你的delphi代碼中?這只是幾行代碼。 –