用获取路径的方法得到圆形窗体(C#2005)
2008-03-22 10:14
417 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Example007_用获取路径的方法得到圆形窗体
{
public partial class Form1 : Form
{
[DllImport("gdi32")]
private static extern IntPtr BeginPath(IntPtr hdc);
[DllImport("gdi32")]
private static extern int SetBkMode(IntPtr hdc, int nBkMode);
const int TRANSPARENT = 1;
[DllImport("gdi32")]
private static extern IntPtr EndPath(IntPtr hdc);
[DllImport("gdi32")]
private static extern IntPtr PathToRegion(IntPtr hdc);
[DllImport("gdi32")]
private static extern int Ellipse(IntPtr hdc, int X1, int Y1, int X2, int Y2);
[DllImport("user32")]
private static extern IntPtr SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);
[DllImport("user32")]
private static extern IntPtr GetDC(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
IntPtr dc;
IntPtr region;
dc = GetDC(this.Handle);//得到窗体的绘图操作句柄
BeginPath(dc);//记录路径信息
SetBkMode(dc, TRANSPARENT);//设置为透明模式
Ellipse(dc, 20, 20, 220, 220);
EndPath(dc);//记录路径信息
region = PathToRegion(dc);//映射路径信息为区域信息
SetWindowRgn(this.Handle, region, true);//设置窗体的形状为指定区域
}
//解决窗体的移动问题。
//通过PreProcessMessage方法筛选后,所有窗体消息都发送到WndProc方法。
//本实例通过重载该方法取到WM_NCHITTEST消息,默认处理后,通过m.Result = (IntPtr)HTCAPTION语句设置消息处理的返回值为HTCAPTION。
//这样用户在窗体中拖动鼠标时,即可以达到和拖动标题栏相同的效果。
const int WM_NCHITTEST = 0x0084;
const int HTCLIENT = 0x0001;
const int HTCAPTION = 0x0002;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if (m.Result == (IntPtr)HTCLIENT)
m.Result = (IntPtr)HTCAPTION;
break;
default:
base.WndProc(ref m);
break;
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Example007_用获取路径的方法得到圆形窗体
{
public partial class Form1 : Form
{
[DllImport("gdi32")]
private static extern IntPtr BeginPath(IntPtr hdc);
[DllImport("gdi32")]
private static extern int SetBkMode(IntPtr hdc, int nBkMode);
const int TRANSPARENT = 1;
[DllImport("gdi32")]
private static extern IntPtr EndPath(IntPtr hdc);
[DllImport("gdi32")]
private static extern IntPtr PathToRegion(IntPtr hdc);
[DllImport("gdi32")]
private static extern int Ellipse(IntPtr hdc, int X1, int Y1, int X2, int Y2);
[DllImport("user32")]
private static extern IntPtr SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);
[DllImport("user32")]
private static extern IntPtr GetDC(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
IntPtr dc;
IntPtr region;
dc = GetDC(this.Handle);//得到窗体的绘图操作句柄
BeginPath(dc);//记录路径信息
SetBkMode(dc, TRANSPARENT);//设置为透明模式
Ellipse(dc, 20, 20, 220, 220);
EndPath(dc);//记录路径信息
region = PathToRegion(dc);//映射路径信息为区域信息
SetWindowRgn(this.Handle, region, true);//设置窗体的形状为指定区域
}
//解决窗体的移动问题。
//通过PreProcessMessage方法筛选后,所有窗体消息都发送到WndProc方法。
//本实例通过重载该方法取到WM_NCHITTEST消息,默认处理后,通过m.Result = (IntPtr)HTCAPTION语句设置消息处理的返回值为HTCAPTION。
//这样用户在窗体中拖动鼠标时,即可以达到和拖动标题栏相同的效果。
const int WM_NCHITTEST = 0x0084;
const int HTCLIENT = 0x0001;
const int HTCAPTION = 0x0002;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if (m.Result == (IntPtr)HTCLIENT)
m.Result = (IntPtr)HTCAPTION;
break;
default:
base.WndProc(ref m);
break;
}
}
}
}
相关文章推荐
- Control Study -> 用获取路径方法得到圆形窗体(如将: PictureBox变成圆形)
- Control Study -> 用获取路径方法得到圆形窗体(如将: PictureBox变成圆形)
- Control Study -> 用获取路径方法得到圆形窗体(如将: PictureBox变成圆形)
- 【C#】[窗体]圆形窗体(获取路径)
- c#中获取路径的方法
- C#中获取程序当前路径的集中方法[转]
- C#中几种获取路径方法
- C#中获取当前路径的几种方法
- C# WinForm获取 当前执行程序路径的几种方法
- 总结C#获取当前路径的7种方法
- C# 获取当前路径方法
- C#获取当前路径的方法如下
- C#获取项目程序及运行路径的方法
- C#获取项目程序及运行路径的方法
- C#获取项目程序路径的方法
- C#获取当前路径的方法集合
- C#获取相对路径的八种方法
- C#获取程序当前路径的方法
- c# 获取文件路径的几种方法
- C# 获取当前路径7种方法