首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >褪色WS_EX_LAYERED形式

褪色WS_EX_LAYERED形式
EN

Stack Overflow用户
提问于 2018-12-18 12:23:32
回答 1查看 415关注 0票数 0

我有一个表单,通过使用以下函数调用进行单击:

代码语言:javascript
复制
SetWindowLong(Handle, GWL_EXSTYLE, (IntPtr)(GetWindowLong(Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED ^ WS_EX_TRANSPARENT));
SetLayeredWindowAttributes(Handle, 0, 0xFF, LWA_ALPHA);

但是,当我尝试使用System.Windows.Forms.Form.Opacity属性淡出该窗口时,会得到以下异常:

代码语言:javascript
复制
System.ComponentModel.Win32Exception (0x80004005): The parameter is not valid
   at System.Windows.Forms.Form.UpdateLayered()
   at System.Windows.Forms.Form.set_Opacity(Double value)

我怎样才能同时做到这两件事?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-18 13:08:58

下面的操作适用于windows窗体

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication30
{
    public partial class Form1 : Form
    {

        [DllImport("user32.dll", SetLastError = true)]
        static extern System.UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
        private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, uint dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
        static extern int SetLayeredWindowAttributes(IntPtr hWnd, int crKey, byte bAlpha, uint dwFlags);



        public const int GWL_EXSTYLE = -20;
        public const uint WS_EX_LAYERED = 0x80000;
        public const uint LWA_ALPHA = 0x2;
        public const uint LWA_COLORKEY = 0x1;
        public Form1()
        {
            InitializeComponent();

            IntPtr Handle = this.Handle;
            UInt32 windowLong = GetWindowLong(Handle, GWL_EXSTYLE);
            SetWindowLong32(Handle, GWL_EXSTYLE, (uint)(windowLong ^ WS_EX_LAYERED));
            SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53832933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档