c# winform 应用编程代码总结 6

22、空心字体效果演示

        [System.Runtime.InteropServices.DllImport("gdi32")]
        private static extern IntPtr CreateFont(int H,int W,int E,int O,int FW,int I,int u,int S,int C,int OP,int CP,int Q,int PAF,string F);
        [System.Runtime.InteropServices.DllImport("gdi32")]
        private static extern IntPtr BeginPath(IntPtr hdc);
        [System.Runtime.InteropServices.DllImport("gdi32")]
        private static extern IntPtr EndPath(IntPtr hdc);
        [System.Runtime.InteropServices.DllImport("gdi32")]
        private static extern int SetBkMode(IntPtr hdc,int nBkMode);
        [System.Runtime.InteropServices.DllImport("gdi32")]
        private static extern IntPtr StrokePath(IntPtr hdc);
        [System.Runtime.InteropServices.DllImport("gdi32")]
        private static extern int TextOut(IntPtr hdc,int x,int y,string lpString,int nCount);
        [System.Runtime.InteropServices.DllImport("gdi32")]
        private static extern IntPtr SelectObject(IntPtr hdc,IntPtr hObject);
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern IntPtr GetDC(IntPtr hwnd);
        const int FW_HEAVY = 900;
        const int ANSI_CHARSET = 0;
        const int OUT_DEFAULT_PRECIS = 0;
        const int CLIP_DEFAULT_PRECIS = 0;
        const int DEFAULT_QUALITY = 0;
        const int DEFAULT_PITCH = 0;
        const int FF_SWISS = 32;
        const int TRANSPARENT = 1;

        private void button1_Click(object sender, System.EventArgs e)
        {
            IntPtr dc=GetDC(this.Handle);
            IntPtr m_Font=CreateFont(50, 20, 0, 0, FW_HEAVY, 1, 0,
                0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
                CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
                DEFAULT_PITCH | FF_SWISS, "Times New Roman");
            //创建字体
            //这里的字体一定要是TrueType Font

            BeginPath(dc);
            //开始捕获轮廓
            SetBkMode(dc, TRANSPARENT );
            IntPtr m_OldFont=SelectObject(dc,m_Font);
            TextOut(dc,10,0,this.textBox1.Text,this.textBox1.Text.Length);
            SelectObject(dc,m_OldFont);
            EndPath(dc);
            //结束捕获
            StrokePath(dc);
            //将捕获的轮廓用当前的Pen画到Canvas上       
        }

image

23、反转图片的颜色

        [System.Runtime.InteropServices.DllImport("gdi32")]
        private static extern int StretchBlt(IntPtr hDestDC,int x,int y,int nWidth,
            int nHeight,IntPtr hSrcDC,int xSrc,int ySrc,int WidthSrc,int HeightSrc,int dwRop);
        const int NOTSRCCOPY = 0x330008;
        //声明所需函数和常量
    
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern IntPtr GetDC(IntPtr hwnd);

        IntPtr hDC1,hDC2;
        Graphics g1,g2;
        private void button1_Click(object sender, System.EventArgs e)
        {
            if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                Bitmap bmp;
                bmp=new Bitmap(this.openFileDialog1.FileName);
                this.pictureBox1.Image=bmp;
                this.pictureBox1.Refresh();
        
                hDC1=g1.GetHdc();
                hDC2=g2.GetHdc();
                StretchBlt(hDC2,0,0,this.pictureBox2.Width,this.pictureBox2.Height,
                    hDC1,0,0,this.pictureBox1.Width,this.pictureBox1.Height,NOTSRCCOPY);
                g1.ReleaseHdc(hDC1);
                g2.ReleaseHdc(hDC2);
            }
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            this.pictureBox1.SizeMode=PictureBoxSizeMode.StretchImage;
            this.pictureBox2.SizeMode=PictureBoxSizeMode.StretchImage;
            g1=this.pictureBox1.CreateGraphics();
            g2=this.pictureBox2.CreateGraphics();
        }

image

24、创建缩略图

        private void button1_Click(object sender, System.EventArgs e)
        {
            if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                Image image = new Bitmap(this.openFileDialog1.FileName);
                Image pThumbnail = image.GetThumbnailImage(100, 100, null, new 
                    IntPtr());
                this.CreateGraphics().DrawImage(
                    pThumbnail,
                    10,
                    10, 
                    pThumbnail.Width,
                    pThumbnail.Height);            
            }
        }

image

本系列文章是作者学习《Visual C#.NET 应用编程150例》(源码)心得笔记,欢迎转载,请注明原文地址,如有疑问,可以通过 [email protected] 联系作者本人。

你可能感兴趣的:(WinForm)