C# FORM中创建圆形窗体 + 移动窗体

圆形窗体

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
using  System.Runtime.InteropServices;
None.gif
None.gif
namespace  Example007_用获取路径的方法得到圆形窗体
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Resources.ResourceManager resources 
= new System.Resources.ResourceManager(typeof(Form1));
InBlock.gif            
this.label1 = new System.Windows.Forms.Label();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// label1
InBlock.gif            
// 
InBlock.gif
            this.label1.AutoSize = true;
InBlock.gif            
this.label1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
InBlock.gif            
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
InBlock.gif            
this.label1.Location = new System.Drawing.Point(169178);
InBlock.gif            
this.label1.Name = "label1";
InBlock.gif            
this.label1.Size = new System.Drawing.Size(5619);
InBlock.gif            
this.label1.TabIndex = 0;
InBlock.gif            
this.label1.Text = "圆形窗体";
InBlock.gif            
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
InBlock.gif            
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
InBlock.gif            
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
InBlock.gif            
this.ClientSize = new System.Drawing.Size(392373);
InBlock.gif            
this.Controls.Add(this.label1);
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
InBlock.gif            
this.Text = "Example007 用获取路径的方法得到圆形窗体";
InBlock.gif            
this.Load += new System.EventHandler(this.Form1_Load);
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
//DLL引用
InBlock.gif
        [DllImport("gdi32")]
InBlock.gif        
private static extern IntPtr BeginPath(IntPtr hdc);
InBlock.gif        [DllImport(
"gdi32")]
InBlock.gif        
private static extern int SetBkMode(IntPtr hdc,int nBkMode);
InBlock.gif
InBlock.gif        
private System.Windows.Forms.Label label1;
InBlock.gif        
InBlock.gif        
const int transparent = 1;
InBlock.gif        
InBlock.gif        [DllImport(
"gdi32")]
InBlock.gif        
private static extern IntPtr EndPath(IntPtr hdc);
InBlock.gif        [DllImport(
"gdi32")]
InBlock.gif        
private static extern IntPtr PathToRegion(IntPtr hdc);
InBlock.gif        [DllImport(
"gdi32")]
InBlock.gif        
private static extern int Ellipse(IntPtr hdc,int x1,int y1,int x2,int y2);
InBlock.gif
InBlock.gif        [DllImport(
"gdi32")]
InBlock.gif        
private static extern int Circle(int x,int y,int r);
InBlock.gif
InBlock.gif        
InBlock.gif        [DllImport(
"user32")]
InBlock.gif        
private static extern IntPtr SetWindowRgn(IntPtr hwnd,IntPtr hRgn,bool bRedraw);
InBlock.gif        [DllImport(
"user32")]
InBlock.gif        
private static extern IntPtr GetDC(IntPtr hwnd);
InBlock.gif                
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 窗体加载
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        private void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IntPtr dc;
InBlock.gif            IntPtr region;
InBlock.gif            dc 
= GetDC(this.Handle);
InBlock.gif            BeginPath(dc);
InBlock.gif
InBlock.gif            
//根据路径创建不规则窗体
InBlock.gif
            SetBkMode(dc,transparent);
InBlock.gif
InBlock.gif            
//设置为透明模式
InBlock.gif
            
InBlock.gif            Ellipse(dc,
50,50,350,350);
InBlock.gif
InBlock.gif            
//Circle(this.Width / 2,this.Height /2,this.Width / 2);
InBlock.gif

InBlock.gif            EndPath(dc);
InBlock.gif            region 
= PathToRegion(dc);
InBlock.gif            SetWindowRgn(
this.Handle,region,true);
InBlock.gif
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
const int WM_NCHITTEST = 0x0084;
InBlock.gif        
const int HTCLIENT = 0x0001;
InBlock.gif        
const int HTCAPTION = 0x0002;
InBlock.gif
InBlock.gif        
protected override void WndProc(ref System.Windows.Forms.Message m)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch(m.Msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case WM_NCHITTEST:
InBlock.gif                    
base.WndProc(ref m);
InBlock.gif                    
if (m.Result == (IntPtr)HTCLIENT)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        m.Result 
= (IntPtr)HTCAPTION;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                    
base.WndProc(ref m);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/lizhi/archive/2007/02/03/638785.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值