C#winform制作不規(guī)則窗體的案例-創(chuàng)新互聯(lián)

這篇文章主要介紹了C# winform制作不規(guī)則窗體的案例,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于成都網(wǎng)站設計、成都網(wǎng)站建設、同安網(wǎng)絡推廣、微信小程序開發(fā)、同安網(wǎng)絡營銷、同安企業(yè)策劃、同安品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供同安建站搭建服務,24小時服務熱線:18980820575,官方網(wǎng)址:muchs.cn
        private void Form1_Load(object sender, EventArgs e)
        {
            //重新繪制窗口樣式
            string fileName = @"C:\Users\admin\Desktop\Yuan1.png";
            Bitmap mybitmap = new Bitmap(fileName);
            CreateControlRegion(this, mybitmap);
            this.BackColor = Color.White;// 此處為添加部分  
            this.TransparencyKey = Color.White;//此處為添加部分  
        }
        /// <summary>
        /// 重新繪制窗口樣式
        /// </summary>
        /// <param name="control"></param>
        /// <param name="bitmap"></param>
        public static void CreateControlRegion(Control control, Bitmap bitmap)
        {
            // Return if control and bitmap are null  
            //判斷是否存在控件和位圖  
            if (control == null || bitmap == null)
                return;
            //設置控件大小為位圖大小  
            control.Width = bitmap.Width;
            control.Height = bitmap.Height;
            // Check if we are dealing with Form here   
            //當控件是form時  
            if (control is System.Windows.Forms.Form)
            {
                // Cast to a Form object  
                //強制轉(zhuǎn)換為FORM  
                Form form = (Form)control;
                //當FORM的邊界FormBorderStyle不為NONE時,應將FORM的大小設置成比位圖大小稍大一點  
                form.Width = control.Width;
                form.Height = control.Height;
                //沒有邊界  
                form.FormBorderStyle = FormBorderStyle.None;
                //將位圖設置成窗體背景圖片  
                form.BackgroundImage = bitmap;
                //計算位圖中不透明部分的邊界  
                GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
                //應用新的區(qū)域  
                form.Region = new Region(graphicsPath);

                // 以下為自己添加的語句,不添加此兩句會出現(xiàn)問題  
                form.Width = bitmap.Width;
                form.Height = bitmap.Height;
            }

            //當控件是button時  
            else if (control is System.Windows.Forms.Button)
            {
                //強制轉(zhuǎn)換為 button  
                Button button = (Button)control;
                //不顯示button text  
                button.Text = "";

                //改變 cursor的style  
                button.Cursor = Cursors.Hand;

                //設置button的背景圖片  
                button.BackgroundImage = bitmap;

                //計算位圖中不透明部分的邊界  
                GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
                // Apply new region   
                //應用新的區(qū)域  
                button.Region = new Region(graphicsPath);
                button.Width = bitmap.Width;
                button.Height = bitmap.Height;
                button.FlatStyle = FlatStyle.Popup;//此處為添加部分  
            }
        }
        private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)
        {
            //創(chuàng)建 GraphicsPath  
            GraphicsPath graphicsPath = new GraphicsPath();

            //使用左上角的一點的顏色作為我們透明色  
            Color colorTransparent = bitmap.GetPixel(0, 0);

            //第一個找到點的X  
            int colOpaquePixel = 0;

            // 偏歷所有行(Y方向)  
            for (int row = 0; row < bitmap.Height - 1; row++)
            {
                // Reset value   
                //重設  
                colOpaquePixel = 0;

                //偏歷所有列(X方向)  
                for (int col = 0; col < bitmap.Width - 1; col++)
                {
                    //如果是不需要透明處理的點則標記,然后繼續(xù)偏歷  
                    if (bitmap.GetPixel(col, row) != colorTransparent)
                    {

                        colOpaquePixel = col;

                        //建立新變量來記錄當前點  
                        int colNext = col;

                        ///從找到的不透明點開始,繼續(xù)尋找不透明點,一直到找到或則達到圖片寬度   
                        for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)
                        {
                            Color gpi = bitmap.GetPixel(colNext, row);
                            if (bitmap.GetPixel(colNext, row) == colorTransparent)
                            {

                                break;
                            }
                        }
                        //將不透明點加到graphics path  

                        {
                            graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));
                        }
                        col = colNext;
                    }
                }
            }
            return graphicsPath;
        }

感謝你能夠認真閱讀完這篇文章,希望小編分享C# winform制作不規(guī)則窗體的案例內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細的解決方法等著你來學習!

當前文章:C#winform制作不規(guī)則窗體的案例-創(chuàng)新互聯(lián)
文章位置:http://muchs.cn/article32/degesc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設、小程序開發(fā)、服務器托管網(wǎng)站制作、網(wǎng)站內(nèi)鏈、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司