下面以Button为例来实现
窗体半透明实现简单直接设置属性或者用api来设置层属性来实现,但一般控件设置层属性,却得不到半透明效果。实际上做一下小改动就可以了。
响应窗体的OnActivate事件,代码如下。
void __fastcall TForm1::FormActivate(TObject *Sender)
{
long wsex = ::GetWindowLongPtr(Handle, GWL_EXSTYLE );
wsex |= WS_EX_TOOLWINDOW ;
wsex &= ~WS_EX_APPWINDOW;
::SetWindowLongPtr(Button1->Handle, GWL_EXSTYLE, wsex );
::SetParent(Button1->Handle ,GetDesktopWindow());
SetWindowLong(Button1->Handle, GWL_EXSTYLE, GetWindowLong(Button1->Handle, GWL_EXSTYLE ) ^ WS_EX_LAYERED ^ WS_EX_TRANSPARENT );
::SetLayeredWindowAttributes(Button1->Handle, RGB( 0, 0, 0 ), 255 - 100, LWA_ALPHA );
Button1->Refresh() ; // 一定要呼叫,否则失败
::SetParent(Button1->Handle,Form1->Handle);
Button1->Repaint() ; // 一定要呼叫,否则失败
}
半透明实现了,但发现button不响应事件了,实际上把button的消息也“透明”了。别急,语句换为
SetWindowLong(Button1->Handle, GWL_EXSTYLE, GetWindowLong(Button1->Handle, GWL_EXSTYLE ) ^ WS_EX_LAYERED );
就好了。
但依然存在一个问题就是窗体移动时,button不会跟着移动,这个好办添加WM_MOVE消息映射自己调整即可。
全部源代码如下:
h文件
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall FormActivate(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
void __fastcall CMMove(TMessage &msg); //消息接收处理函数
public: // User declarations
__fastcall TForm1(TComponent* Owner);
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_MOVE,TMessage,CMMove)
//决定WM_MOVE消息交给CMMove 函数处理
END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
cpp文件
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormActivate(TObject *Sender)
{
long wsex = ::GetWindowLongPtr(Handle, GWL_EXSTYLE );
wsex |= WS_EX_TOOLWINDOW ;
wsex &= ~WS_EX_APPWINDOW;
::SetWindowLongPtr(Button1->Handle, GWL_EXSTYLE, wsex );
::SetParent(Button1->Handle ,GetDesktopWindow());
SetWindowLong(Button1->Handle, GWL_EXSTYLE, GetWindowLong(Button1->Handle, GWL_EXSTYLE ) ^ WS_EX_LAYERED ^ WS_EX_TRANSPARENT );
::SetLayeredWindowAttributes(Button1->Handle, RGB( 0, 0, 0 ), 255 - 100, LWA_ALPHA );
Button1->Refresh() ; // 一定要呼叫,否则失败
::SetParent(Button1->Handle,Form1->Handle);
Button1->Repaint() ; // 一定要呼叫,否则失败
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CMMove(TMessage &msg) //消息接收处理函数
{
this->Button1->Left=this->Left+0.33*this->Width;
this->Button1->Top=this->Top+0.33*this->Height;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage("hao");
}
//---------------------------------------------------------------------------