Generated Image

如何创建文字型窗体

 
 
static void Main() 
        {
            Application.Run(new Form1());
        }
        [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("user32")]
        private static extern IntPtr GetDC(IntPtr hwnd);
        [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 IntPtr PathToRegion(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 SelectObject(IntPtr hdc,IntPtr hObject);
        [System.Runtime.InteropServices.DllImport("gdi32")]
        private static extern int TextOut(IntPtr hdc,int x,int y,string lpString, int nCount);
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern IntPtr SetWindowRgn(IntPtr hwnd,IntPtr hRgn,bool bRedraw);
 
        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            //创建新字体
            IntPtr MyFont=CreateFont(160,100,0,0,0,0,0,0,0,0,0,0,0,"宋体_GB2312");
            //取得设备句柄
            IntPtr MyDC=GetDC(this.Handle);
            //设置背景模式
            SetBkMode(MyDC,1);
            //开始记录轮廓路径
            BeginPath(MyDC);
            //设置文字输出字体
            IntPtr MyOldFont=SelectObject(MyDC,MyFont);
            //输出文字
            TextOut(MyDC,0,20,"桂圆",4);
            //恢复原始字体对象
            SelectObject(MyDC,MyOldFont);
            //结束记录轮廓路径绘制
            EndPath(MyDC);
            //创建文字区域
            IntPtr MyRgn=PathToRegion(MyDC);
            //使用文字区域创建程序窗体
            SetWindowRgn(this.Handle,MyRgn,true);
        }
 
        private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {//关闭程序
           this.Close();
        }    
posted @ 2007-02-12 12:38  桂圆  阅读(711)  评论(0编辑  收藏  举报