2017-04-11 51 views
-1

如何傳遞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未聲明的標識符如何聲明它。也請告訴我是我正確的方式?

+0

你打算用這種形​​式做在你的C#的dll? –

+0

可以在顯示光標的同一顯示器上打開窗體。 – user7424581

+0

您無法從c#訪問您的表單參數。它是一個Delphi類實例,與c#類實例不兼容。你爲什麼不把邏輯添加到你的delphi代碼中?這只是幾行代碼。 –

回答

1

一個Delphi 的Delphi類,而不是一個.NET框架類的一個實例。因此,任何C#代碼都無法對任何此類表單引用做任何有意義的事情。

但是,你正在努力實現不需要表單對象,只是形式表示桌面窗口的窗口句柄什麼。

重寫您的C#代碼以接受HWND並將Form.WindowHandle傳遞給C#。然後您可能需要使用interop來調用相應的Win32 API函數來使用該窗口句柄。

說了這麼多,你要實現的目標應該是在有關自身Delphi應用程序簡單,無需涉及任何外部代碼,更不用說C#,在所有。唯一的併發症是你的德爾福版本(德爾福5)不直接支持MonitorFromPoint()方法。

實現一個不會特別困難,但如果你是在C#空間更加舒適那麼這可能是一個原因,繼續向下的電流路徑(但使用HWND),除非你可以實現需要得到幫助代碼在Delphi中。

+0

是@Deltics,我改變了我的c#代碼,正如你所說的。並從我的delphi 5代碼中將句柄作爲參數傳遞給c#dll。它爲我着迷。非常感謝。最後我得到了解決方案。 :) – user7424581

+0

請你告訴我如何實現MonitorFromPoint()在Delphi 5 @Deltics – user7424581

+0

我這樣AutoVueObj.Concatenate(self.WindowHandle)調用的方法;通過dll函數實現在c#中,但它給錯誤,類型錯誤是不能編組參數無效管理/非託管類型組合@Deltics – user7424581

相關問題