一、将Qt窗口嵌入到桌面中。
声明一个最简单的类:
class Dialog : public QDialog
{
Q_OBJECT
public :
Dialog(QWidget *parent = 0);
~Dialog();
}
函数实现:
Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
//创建个LineEdit用来测试焦点
QLineEdit* le = new QLineEdit(this );
}
Dialog::~Dialog()
{
}
主函数:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
HWND desktopHwnd = findDesktopIconWnd();
if (desktopHwnd) SetParent(w.winId(), desktopHwnd);
w.show();
return a.exec();
}
运行效果:
有个窗口嵌入了桌面。按win+D组合键可以看到此窗口在桌面上。
二、让窗口全透明:
2.1最容易想到的就是setWindowOpacity()函数了。
w.setWindowOpacity(0.5),运行:结果杯具了,此函数完全无效,因为其父窗口特殊,这个函数内部使用的系统窗口标志不被支持。
2.2
w.setAttribute(Qt::WA_TranslucentBackground, true);
运行效果:
全透明ok。如果其父窗口为空的话,透明的地方会成为黑块。
三、让窗口半透明
3.1w.setAttribute(Qt::WA_TranslucentBackground, true) + 背景调色板
运行效果仍然是全透明,因为TranslucentBackground为true,根本不画背景。
3.2单纯的背景调色板:
QPalette pal = w.palette();
pal.setColor(QPalette::Background, QColor(100,100,100,50));
w.setPalette(pal);
w.setAutoFillBackground(true );
运行效果出现了半透明:
但是还没大功告成,不停点击桌面,再点击这个窗口,会发现这个窗口越来越不透明,直至完全不透明了。不知道是不是qt的bug。
ps:加一句 w.setAttribute(Qt::WA_OpaquePaintEvent,true); 窗口就能够一直保持这个效果了。即这个方案可行。
pps:此方案在XP也是黑色底块。
3.3转战paintEvent()
protected :
void paintEvent(QPaintEvent *);
void Dialog::paintEvent(QPaintEvent *e)
{
QPainter p(this );
p.fillRect(rect(), QColor(0,0xff,0,30));
}
用一个带有alpha值的颜色填充背景,运行效果发现颜色确实有alpha值,但是桌面的内容透不过来。
3.4setAttribute(Qt::WA_TranslucentBackground, true) + paintEvent()
运行效果:
得到了设想中的效果。
最终的主函数代码:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
HWND desktopHwnd = findDesktopIconWnd();
if (desktopHwnd) SetParent(w.winId(), desktopHwnd);
w.setAttribute(Qt::WA_TranslucentBackground, true );
w.show();
return a.exec();
}
最终的dialog实现代码:
Dialog::Dialog(QWidget *parent) : QWidget(parent)
{
//创建个LineEdit用来测试焦点
QLineEdit* le = new QLineEdit(this );
}
Dialog::~Dialog()
{
}
void Dialog::paintEvent(QPaintEvent *e)
{
QPainter p(this );
p.fillRect(rect(), QColor(0,0xff,0,30));
}
PS:
经测试此代码在XP运行不正常。窗口成为黑色背景块。只能是颜色半透明了。
还有就是图标会被盖住。只能把w.setAttribute(Qt::WA_TranslucentBackground, true );注释掉,有半透明颜色,无法看到桌面。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22893636/viewspace-663017/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/22893636/viewspace-663017/