在Winform表單中使用WPF控制項(附源碼)

來源:互聯網
上載者:User

今天是禮拜6,下雨,沒有外出,閑暇就寫一篇博文講下如何在Winform中使用WPF控制項。原有是我在百度上搜尋相關資訊無果,遂乾脆動手自己實現。

WPF控制項的漂亮是Winform無法匹及的,本文主旨是在Winform工程中如何使用WPF控制項

先看下效果,如:

左邊是傳統的Winform控制項,右邊是DebugLZQ引入的WPF控制項,我們可以很清楚的看到圖片的“半透明”效果、TextBox的透明背景!介面很Cool吧~

下面就介紹下,這個創新性工程的建立過程,並附上所有代碼:

首先,添加一個WPF自訂控制項,控制項的xaml很簡單,代碼如下:

<Grid  >                <Image  Margin="10,10,10,90" x:Name="img" Stretch="Uniform" Opacity="1">            <Image.BitmapEffect>                <DropShadowBitmapEffect Opacity="1" />            </Image.BitmapEffect>        </Image>        <TextBox Background="Transparent"  Foreground="White" Height="40" FontSize="32" Margin="44,0,56,36" x:Name="txtBox1" Opacity="0.5" Text="" VerticalAlignment="Bottom" />    </Grid>

其次,我們需要添加相應的設定效果的函數,代碼如下:

        public void SetSource(string fileName)        {            img.Source = new BitmapImage(new Uri(fileName) );        }        public void SetOpacity(double opacity)        {            img.Opacity = opacity;        }        //        public string GetText()        {            return txtBox1.Text;        }

OK,以上步驟完成後我們建立一個Winform應用程式,添加先關的引用,這個非常重要,否則控制項無法正常工作,引用的清單如下:

這裡需要重點提出的是WindowsFormsIntegration這個引用。

我們編譯整個項目,編譯成功在工具箱中會出現相應的控制項,如:

拖動這個控制項到Winform表單中,調用相應的函數。函數調用代碼如下:

userControl11.SetSource(@"D:\臨時檔案\建立檔案夾\DebugLZQ.jpg");            userControl11.SetOpacity(0.5);
string text = userControl11.GetText();

OK,至此程式完成。很簡單強大吧~

再次看下:

請點擊下面的“綠色通道”--“關注DebugLZQ”,共同學習進步~Thank you for your Time!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.