使用SetLayeredWindowAttributes可以方便的制作透明窗体,此函数在w2k以上才支持,而且如果希望直接使用的话,可能需要下载最新的SDK。不过此函数在w2k的user32.dll里有实现,所以如果你不希望下载巨大的sdk的话,可以直接使用GetProcAddress获取该函数的指针。

以下是MSDN上的原内容,我会加以解释。

The SetLayeredWindowAttributes function sets the opacity and transparency color key of a layered window.

BOOL SetLayeredWindowAttributes(

HWND hwnd,
    COLORREF crKey,
    BYTE bAlpha,
    DWORD dwFlags
);
Parameters

hwnd
[in] Handle to the layered window. A layered window is created by specifying WS_EX_LAYERED when creating the window with the CreateWindowEx function or by setting WS_EX_LAYERED via SetWindowLong after the window has been created.

crKey
[in] COLORREF structure that specifies the transparency color key to be used when composing the layered window. All pixels painted by the window in this color will be transparent. To generate a COLORREF, use the RGB macro.

bAlpha
[in] Alpha value used to describe the opacity of the layered window. Similar to the SourceConstantAlpha member of the BLENDFUNCTION structure. When bAlpha is 0, the window is completely transparent. When bAlpha is 255, the window is opaque.

dwFlags
[in] Specifies an action to take. This parameter can be one or more of the following values.
LWA_COLORKEY
Use crKey as the transparency color.
LWA_ALPHA
Use bAlpha to determine the opacity of the layered window.

Return Value

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

详细解说

参数1、主要说的是所要设置的窗体必须是WS_EX_LAYERED格式,设置方法如下:

//设置窗体为WS_EX_LAYERED格式

SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,

GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);

//其实0x80000 == WS_EX_LAYERED

参数2、意思是可以设置指定的颜色为透明色,通过RGB宏设置。

参数3、可以简单的理解为窗体透明的程度范围为0~255(0为完全透明,255不透明)。

参数4、可以取两个值LWA_COLORKEY (0x1)和 LWA_ALPHA(0x2),如下:

取值为LWA_ALPHA即等于2时,参数2无效,通过参数3决定透明度.

取值为LWA_COLORKEY即等于1时,参数3无效,参数2指定的颜色为透明色,其他颜色则正常显示.

把以下代码放OnInitDialog中即可实现半透明窗体

SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, 

GetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE)^WS_EX_LAYERED);

HINSTANCE hInst = LoadLibrary("User32.DLL"); //显式加载DLL
if (hInst)
{
typedef BOOL(WINAPI *MYFUNC) (HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL; //取得SetLayeredWindowAttributes函数指针
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if (fun) fun(this->GetSafeHwnd(), , , ); //通过第三个参数来设置窗体透明程度
FreeLibrary(hInst);
}

其实只需要三句代码就行

 SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE,
GetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE) ^ WS_EX_LAYERED);
SetLayeredWindowAttributes(,,)//第三个参数设置透明度,第四个参数设置透明样式

效果:

 

MFC窗口显隐的更多相关文章

  1. day50 盒子显隐2D形变

    复习 1.浮动布局 解决block盒子同行显示 => 不完全脱离文档流 => 不再撑开父级高度 脱离文档流: 不在页面中占位(显示层次高于文档流) 不完全: 可以通过清浮动操作, 让子级重 ...

  2. Python12/11--盒子的显隐/布局/z-index/流式布局思想

    1.盒子的显隐 display:none      在页面中不占位,采用定位布局后,显示隐藏都不会影响其他标签,不需要用动画处理时,一般用这个 opacoity : 0        在页面中占位,采 ...

  3. 浏览器页面的显隐对js的setInterval()执行所产生的bug

    前段时间,所写的一个”js无间隙滚动效果“,当页面离开后,重新返回时,会出现动画的错乱.我以为是因为我代码逻辑的原因导致的,但是,当在火狐浏览器上进行浏览时却没有动画错乱的问题. 于是乎,在网上查找是 ...

  4. 将Cocos2dX渲染到MFC窗口上

    引用:http://www.cnblogs.com/windeer/archive/2012/11/18/2767750.html 引言 现在智能手机已经慢慢进入大众化,移动类应用开始火爆起来,游戏类 ...

  5. 根据滑动显隐状态栏的iOS实现

    之前很多兄弟问如何实现类似于淘宝客户端搜索列表那种动态显隐的效果,这几天刚好有时间,就实现了几个例子搞一下,其实原理很简单,也参考了github上一位兄弟的实现.不多说,上代码 @interface ...

  6. 同时支持控制台和MFC窗口程序的APP

    BOOL CMyApp::InitInstance() { if ( m_bShowGui==FALSE ) { FILE *stream = NULL; AllocConsole(); // 开辟控 ...

  7. C#中显/隐式实现接口及其访问方法

    原贴地址: http://www.cnblogs.com/dudu837/archive/2009/12/07/1618663.html 在实现接口的时候,VS提供了两个菜单,一个是"实现接 ...

  8. MFC窗口重绘

    Invalidate()与 UpdateAllViews()有什么分别 Invalidate()是让程序重画窗口. UpdateAllViews()是在DOC/VIEW结构中, 当一个视图的数据改变后 ...

  9. MFC窗口分割以及各窗口间的通讯

    一个偶然的机会又重新接触了MFC窗口的分割,自己结合资料重新写了一个窗口分割的程序,现将具体流程跟大家分享一下: 1.我们先创建一个MFC单文档类的程序,具体分割方式先将单文档整个客户区分成两行一列, ...

随机推荐

  1. vue11 vue实例方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. P1824 进击的奶牛

    题目描述 Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi<=1,000,000 ...

  3. OpenGL编程逐步深入(二)在窗口中显示一个点

    准备知识 在本文中我们将会接触到OpenGl的扩展库GLEW( OpenGL Extension Wrangler Library),GLEW可以帮助我们处理OpenGl中繁琐的扩展管理.一旦初始化后 ...

  4. JS实现PC端全兼容复制

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. C# Dapper 基本使用 增删改查事务等

    using DapperTest.Models; using System.Collections.Generic; using System.Web.Http; using Dapper; usin ...

  6. 手把手教你在VMware虚拟机中安装Ubuntu14.04系统

    在VMware中创建完虚拟机之后,一般需要给虚拟机安装系统,比较受青睐的系统有Ubuntu和Centos,关于Centos系统的安装之前已经写过了,感兴趣的小伙伴可以戳这篇文章:靠谱的centos7. ...

  7. JSP中 input type 用法

    JSP中 input type 用法 Input表示Form表单中的一种输入对象,其又随Type类型的不同而分文本输入框,密码输入框,单选/复选框,提交/重置按钮等,下面一一介绍. 1,type=te ...

  8. 保留原先小程序名称 更改微信小程序主体

    首先给小程序开发者普及一些官方消息: 1.目前官方是不允许修改已经认证的小程序主体信息!(公众号可以修改) 2.小程序与公众号的名称是全平台唯一,即如果小程序叫‘ABC’其他小程序和公众号就不能存在‘ ...

  9. leetcode 链表 Partition List

    Partition List Total Accepted: 19761 Total Submissions: 73252My Submissions Given a linked list and ...

  10. 【Android】利用自己定义View的重绘实现拖动移动,获取组件的尺寸

    以下利用一个app来说明怎样利用自己定义View的重绘实现拖动移动.获取组件的尺寸. 例如以下图,触摸拖动,或者轻轻点击屏幕都能移动图片.假设碰到文字,则会弹出提示. 这里是利用自己定义View的重绘 ...