BCB中如何实现控件半透明(Y___Y 请帮忙)
原贴地址:http://blog.csdn.net/y___y/archive/2007/02/01/1499939.aspx
我按照Y___Y 的文章写出代码
SetWindowLong(Button1->Handle, GWL_EXSTYLE, GetWindowLong(Button1->Handle, GWL_EXSTYLE ) ^ WS_EX_LAYERED ^ WS_EX_TRANSPARENT );
也换成了
SetWindowLong(Button1->Handle, GWL_EXSTYLE, GetWindowLong(Button1->Handle, GWL_EXSTYLE ) ^ WS_EX_LAYERED );
?
button还是不响应事件(无法通过鼠标点击,但是可以响应键盘)!请问为什么呢?
我用的BCB6下面是我的代码
.cpp
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
? : TForm(Owner)
{
? 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);
? //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::Button1Click(TObject *Sender)
{
? ShowMessage("hao");
}
//---------------------------------------
void __fastcall TForm1::CMMove(TMessage &msg) //消息接收处理函数
{
? this->Button1->Left=this->Left+0.33*this->Width;
? this->Button1->Top=this->Top+0.33*this->Height;
}
//---------------------------------------
.h
//---------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------
class TForm1 : public TForm
{
__published:// IDE-managed Components
? TButton *Button1;
? TButton *Button2;
? 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
[解决办法]
最近比较忙,来的不多
i_love_pc说的正确,因为如果父窗体不是桌面的话,就无法半透明,因此导致按钮可视位置与相应鼠标的位置不符合,当时做的比较毛糙,没多加考虑。lz可以自行调整(WM_MOVE来进行)