2012-03-31 39 views
-3

我想知道我們如何在Windows應用程序窗體中使用dll?在c#中的Winform應用程序中使用dll#

我想創建一種PhotoViewer,我必須在dll和GUI中分離應用程序的核心。因此,在我的GUI中,如果我點擊給定的按鈕,我將調用dll中的相應函數。

對於例如:

在DLL允許加載圖片的功能:

private void btn_browse_Click(object sender, System.EventArgs e) 
{ 
    try 
    { 
     OpenFileDialog open = new OpenFileDialog(); 
     open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; 
     if (open.ShowDialog()==DialogResult.OK) 
     { 
      pictureBox1.Image = new Bitmap(open.FileName); 
     } 
    } 
    catch (Exception) 
    { 
     throw new ApplicationException("Failed loading image"); 
    } 

}//End of the loading picture function 

在這段代碼,問題是,在這個代碼我的DLL不知道,有一個圖片框pictureBox1.Image在gui中!

最後我不明白如何嵌套dll和gui。

感謝您從一個動態鏈接庫(DLL/.DLL)爲您在運行時決定各種形式在您的幫助;-)

+0

dll中的方法是什麼?您需要將pictureBox作爲parm傳遞給dll方法。 – yieldvs 2012-03-31 18:17:35

+0

如果你想學習如何編寫Windows應用程序,這將是有用的:http://apparchguide.codeplex.com/ – surfen 2012-03-31 18:18:49

+0

下面的代碼是在我點擊按鈕加載時,將調用dll中的方法照片。 – Quentin91360 2012-03-31 18:19:40

回答

1

要調用一個WinForm類(class MyForm : Form)(我認爲這是你問,但誰知道),你將需要使用System.Reflection,你可能會像這樣進入一個方法使用泛型做一些事情,如以下

if (bIfDllIsWinForm) 
{ 
    classInstance = Activator.CreateInstance(classType); 
    Form dllWinForm = (Form)classInstance; 
    dllWinForm.Show(); 

    // Invoke required method. 
    MethodInfo methodInfo = classType.GetMethod(strSomeMethodName); 
    if (methodInfo != null) 
    { 
     object result = null; 
     result = methodInfo.Invoke(classInst, new object[] { dllParams }); 
     return result.ToString(); 
    } 
else 
{ 
    // Else not a WinForm do something simalar. 
}  

,並通過被稱爲在培訓相關的方法名你的.dll。

我希望這會有所幫助。

+0

謝謝!這正是我所需要的:-) – Quentin91360 2012-04-02 22:19:01

+0

接受答案然後:]。 – MoonKnight 2012-04-03 07:58:58