当前位置:编程学习 > VB >>

VB.NET窗体阴影

我现在使用的是VB2005在VB6下代码这么写:
Private Const CS_DROPSHADOW = &H20000  Private Const GCL_STYLE = (-26)      Private Declare Function GetClassLong Lib "user32" Alias "GetClassLongA" ( _          ByVal hwnd As Long, _          ByVal nIndex As Long) As Long  Private Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" ( _          ByVal hwnd As Long, _          ByVal nIndex As Long, _          ByVal dwNewLong As Long) As Long    Private Sub Form_Load()SetClassLong Me.hwnd, GCL_STYLE, GetClassLong(Me.hwnd, GCL_STYLE) Or CS_DROPSHADOWEnd Sub窗体边框有阴影效果
在VB.NET2005里稍微做的修改,但还是没有阴影效果
Public Class Form1    Private Const CS_DROPSHADOW = &H20000    Private Const GCL_STYLE = (-26)
    Private Declare Function GetClassLong Lib "user32" Alias "GetClassLongA" ( _            ByVal hwnd As Long, _            ByVal nIndex As Long) As Long    Private Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" ( _            ByVal hwnd As Long, _            ByVal nIndex As Long, _            ByVal dwNewLong As Long) As Long    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load            SetClassLong(Me.Handle, GCL_STYLE, GetClassLong(Me.Handle, GCL_STYLE) Or CS_DROPSHADOW)    End SubEnd Class请求高人指点,请帖可行的,自己测试通过能用的修改代码,谢谢!
追问:我用的是VB.NET.....
答案:你用的是什么系统,XP、Vista和Win7是不一样的

 

首先是WinAPI类,包装与调用WindowsAPI
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ServiceMaster
{
  class WinAPI
  {
    [DllImport(\"user32.dll\")]
    public extern static IntPtr GetWindow();

    [DllImport(\"user32.dll\")]
    public extern static bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
    public static uint LWA_COLORKEY = 0x00000001;
    public static uint LWA_ALPHA = 0x00000002;

    #region 阴影效果变量
    //声明Win32 API
    [DllImport(\"user32.dll\", CharSet = CharSet.Auto)]
    public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
    [DllImport(\"user32.dll\", CharSet = CharSet.Auto)]
    public static extern int GetClassLong(IntPtr hwnd, int nIndex);
    [DllImport(\"user32.dll\")]
    public extern static uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
    [DllImport(\"user32.dll\")]
    public extern static uint GetWindowLong(IntPtr hwnd, int nIndex);
    #endregion

    public enum WindowStyle : int
    {
      GWL_EXSTYLE = -20
    }

    public enum ExWindowStyle : uint
    {
      WS_EX_LAYERED = 0x00080000
    }

  }
}

然后是窗体中的调用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using System.IO;
namespace ServiceMaster
{
  public partial class MainFrom : Form
  {
    public MainFrom()
    {
      InitializeComponent();     
      const int CS_DropSHADOW = 0x20000;
      const int GCL_STYLE = (-26); 
      WinAPI.SetClassLong(this.Handle, GCL_STYLE, WinAPI.GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
    }
    protected override CreateParams CreateParams
    {
      get
      {
        CreateParams cp = base.CreateParams;

        cp.Parent = WinAPI.GetWindow();
        return cp;
      }
    }

    private void SetWindowShadow(byte bAlpha)
    {
      WinAPI.SetWindowLong(this.Handle, (int)WinAPI.WindowStyle.GWL_EXSTYLE,
      WinAPI.GetWindowLong(this.Handle, (int)WinAPI.WindowStyle.GWL_EXSTYLE) | (uint)WinAPI.ExWindowStyle.WS_EX_LAYERED);

      WinAPI.SetLayeredWindowAttributes(this.Handle, 0, bAlpha, WinAPI.LWA_COLORKEY | WinAPI.LWA_ALPHA);
    }
}

上一个:VB制作计算器的代码
下一个:关于VB发送数据的问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,