几行代码便可实现VISTA软件界面

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace GlassMoth
{
  // Desktop Windows Manager APIs
  internal class VistaApi
  {
    [DllImport("dwmapi.dll")]
    internal static extern void DwmExtendFrameIntoClientArea(System.IntPtr hWnd, ref Margins pMargins);

    [DllImport("dwmapi.dll")]
    internal static extern void DwmIsCompositionEnabled(ref bool isEnabled);

    internal struct Margins
    {
      public int Left, Right, Top, Bottom;
    }

    // consts for wndproc
    internal const int WM_NCHITTEST = 0x84;
    internal const int HTCLIENT = 1;
    internal const int HTCAPTION = 2;
  }
}
/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace GlassMoth
{
  public partial class Form1 : Form
  {
    // margins for API call used in Paint
    private VistaApi.Margins marg;

    // Rectangles; one for each edge of the form
    private Rectangle topRect = Rectangle.Empty;
    private Rectangle botRect = Rectangle.Empty;
    private Rectangle lefRect = Rectangle.Empty;
    private Rectangle rigRect = Rectangle.Empty;

    public Form1()
    {
      InitializeComponent();

      trackBar1.Minimum = 0;
      trackBar1.Maximum = this.ClientSize.Height;

      this.FitGlass();
    }

    private void FitGlass()
    {
      // If DWM is not enabled then get out
      if (!this.IsGlassEnabled())
      {
        return;
      }

      // Set the Margins to their default values
      marg.Top = trackBar1.Value; // extend from the top
      marg.Left = 0;  // not used in this sample but could be
      marg.Right = 0; // not used in this sample but could be
      marg.Bottom = 0;// not used in this sample but could be

      this.Paint += new PaintEventHandler(this.Form1_Paint);

      // call the function that gives us glass,
      // passing a reference to our inset Margins
      VistaApi.DwmExtendFrameIntoClientArea(this.Handle, ref marg);     
    }

    private bool IsGlassEnabled()
    {
      if (Environment.OSVersion.Version.Major < 6)
      {
        Debug.WriteLine("How about trying this on Vista?");
        return false;
      }

      //Check if DWM is enabled
      bool isGlassSupported = false;
      VistaApi.DwmIsCompositionEnabled(ref isGlassSupported);
      return isGlassSupported;
    }

    // Alpha-blending Paint after the glass extension
    // this seems better than the winforms transparency approach because here we can click on the glass!
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
      // black brush for Alpha transparency
      SolidBrush blackBrush = new SolidBrush(Color.Black);

      Graphics g = e.Graphics;

      if (this.IsGlassEnabled())
      {     
        // setup the rectangles
        topRect = new Rectangle(0, 0, this.ClientSize.Width, marg.Top);
        lefRect = new Rectangle(0, 0, marg.Left, this.ClientSize.Height);
        rigRect = new Rectangle(this.ClientSize.Width - marg.Right, 0, marg.Right, this.ClientSize.Height);
        botRect = new Rectangle(0, this.ClientSize.Height - marg.Bottom, this.ClientSize.Width, marg.Bottom);

        // Fill Rectangles
        g.FillRectangle(blackBrush, topRect);
        g.FillRectangle(blackBrush, lefRect);
        g.FillRectangle(blackBrush, rigRect);
        g.FillRectangle(blackBrush, botRect);
      }

      g.DrawString("hello. Am I on glass?", this.Font, blackBrush, 10, 10);

      blackBrush.Dispose();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      this.Paint -= new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
      this.RecreateHandle(); //needed if changing on the fly
      this.FitGlass();
    }
       
    protected override void WndProc(ref Message m)
    {     
      base.WndProc(ref m);

      if (m.Msg == VistaApi.WM_NCHITTEST // if this is a click
        && m.Result.ToInt32() == VistaApi.HTCLIENT // ...and it is on the client
        && this.IsOnGlass(m.LParam.ToInt32())) // ...and specifically in the glass area
      {
        m.Result = new IntPtr(VistaApi.HTCAPTION); // lie and say they clicked on the title bar
      }
    }

    private bool IsOnGlass(int lParam)
    {
      // sanity check
      if (!this.IsGlassEnabled())
      {
        return false;
      }

      // get screen coordinates
      int x = (lParam << 16) >> 16; // lo order word
      int y =  lParam        >> 16; // hi order word

      // translate screen coordinates to client area
      Point p = this.PointToClient(new Point(x, y));

      // work out if point clicked is on glass
      if (topRect.Contains(p) || lefRect.Contains(p) || rigRect.Contains(p) || botRect.Contains(p))
      {
        return true;
      }

      return false;
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rains卍Soft

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值