主窗体圆角
QPixmap pixmap(directoryOf("Images").absoluteFilePath("xiangximenu.png"));
setMask(pixmap.mask()); // 可以不使用转换的, 使用一张专门的bitmap图片.
setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
只要让你的窗体执行了上面的代码就是几个角是圆角的,xiangximenu.png
是一张圆角的图片
制作圆角图片大家可以访问这个网站http://www.roundpic.com
解释:directory,只不过是为了解决平台兼容性的一段代码,所有的路径都是针对可执行文件的。
QT调用Win32_API
#include <windows.h>
...
#ifdef Q_WS_WIN //声明要使用windows API
//获得窗口句柄
//HWND myhwnd=this->winId();
//关闭显示器API
//::SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
//注销用户API
//ExitWindowsEx(EWX_LOGOFF, 0);
//windows的messagebox,
MessageBox(NULL,TEXT("This is windows window"),TEXT("HAHA YOYO"),MB_OK);
#endif
将窗体嵌入桌面
在试用某些App时,发现有些Ap的窗口可以嵌入桌面,这样Win+D键的时候就可以看到,很方便。在网上搜索了一下,原理就是将窗口的父窗口设置成桌面。
这个父窗口在Xp下通过FindWindow(“Program manager”,”progman”)找到,但是在win7下这个方法找到的父窗口就不好用了。用spy++看了之后,写了下面一段代码findDesktopIconWnd(),在xp和win7下都可以找到这个父窗口。子窗口再调用SetParent(child,parent)就可以在桌面上看到了。
在Qt中实践下:
理论:
0、#include
1、调用Win32 API获取桌面句柄,通过QWidget::winId()获得窗口句柄
2、调用Win32 API设置两窗口父子关系
实践:
在头文件私有部分加入函数声明:
static BOOL enumUserWindowsCB(HWNDhwnd,LPARAMlParam); //静态全局函数
HWND findDesktopIconWnd();
BOOL Widget::enumUserWindowsCB(HWND hwnd,LPARAM lParam)
{
long wflags = GetWindowLong(hwnd, GWL_STYLE);
if(!(wflags & WS_VISIBLE)) return TRUE;
HWND sndWnd;
if( !(sndWnd=FindWindowEx(hwnd, NULL, L"SHELLDLL_DefView", NULL)) ) return TRUE;
HWND targetWnd;
if( !(targetWnd=FindWindowEx(sndWnd, NULL, L"SysListView32", L"FolderView")) ) return TRUE;
HWND* resultHwnd = (HWND*)lParam;
*resultHwnd = targetWnd;
return FALSE;
}
HWND Widget::findDesktopIconWnd()
{
HWND resultHwnd = NULL;
EnumWindows((WNDENUMPROC)enumUserWindowsCB, (LPARAM)&resultHwnd);
return resultHwnd;
}
有了上面两个函数的辅助,下面的工作就简单了,在需要的地方加入如下代码就可以实现了:
HWND desktopHwnd = findDesktopIconWnd();
if(desktopHwnd) SetParent(this->winId(), desktopHwnd);
From: https://www.cnblogs.com/hicjiajia/archive/2011/02/13/1953879.html
为程序添加图标
1. 添加资源文件, 在资源中添加文件, 记作文件名"ico.ico"
2. [*].pro文件增加一行 "RC_FILE = [].rc"
3. 在工程目录新建上一行添加文件名的文件"[*].rc"
4. 在rc中写入"IDI_ICON1 ICON DISCARDABLE [ico.ico]"
5. 编译工程
剪切板操作:
//使用 QApplication::clipboard() 函数获得系统剪贴板对象。这个函数的返回值是 QClipboard 指针。
QClipboard *board = QApplication::clipboard();
//通过 setText(),setImage() 或者 setPixmap() 函数可以将数据放置到剪贴板内
board->setText("Text from Qt Application");
//使用text(),image() 或者 pixmap() 函数则可以从剪贴板获得数据
QString str = board->text();
[保存/打开] 对话框
文件打开对话框
函数原型:
QString getOpenFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )
代码示例:
QString fileName;
fileName = QFileDialog::getOpenFileName(this,
tr("Open Config"),
"",
tr("SCD Files (*.scd);; CID Files (*.cid)"));
if (!fileName.isNull())
{
//fileName即是选择的文件名
}
else
//点是的取消
文件保存对话框
函数原型:
QString getSaveFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )
使用示例:
QString fileName;
fileName = QFileDialog::getSaveFileName(this,
tr("Open Config"),
"",
tr("Config Files (*.ifg)"));
if (!fileName.isNull())
{
//fileName是文件名
}
else
//点的是取消
设置文件过滤的示例
"Image Files (*.png *.jpg *.bmp)" //多个文件使用空格分隔
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" //多个过滤使用两个分号分隔
屏幕截图
#include <QPixmap>
#include <QString>
#include <QDebug>
#include <QApplication>
#include <QDesktopWidget>
#include <QTextCodec>
#include <QDir>
#include <QScreen>
#include <QDateTime>
#include <QWindow>
void Widget::on_pushButton_pressed()
{
qDebug() << "Widget::on_pushButton_pressed()" << "\r\n";
qDebug() << "CurWorkPath: " << QDir::currentPath();
QDesktopWidget& deskTop = *QApplication::desktop(); // 获取桌面
QWindow * pobjWindow = deskTop.windowHandle(); // 获取桌面工具类
QScreen* pobjScreen = pobjWindow->screen(); // 获取屏幕对象
// 屏幕对象抓取屏幕Wnd, QT5 之后 不支持QPixMap::grabWindow()
// 且 grabWindow() 不为静态函数
QPixmap pixMap = pobjScreen->grabWindow(QApplication::desktop()->winId());
QString strPicPath = "./%1_screenShot.jpg";
qDebug() << "\r\nQDateTime::currentDateTimeUtc().toString(): " << QDateTime::currentDateTimeUtc().toString();
strPicPath = strPicPath.arg(QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss")); // 格式化时间字符串
qDebug() << "\r\nSavePicPath: " << strPicPath;
pixMap.save(strPicPath, "JPG"); // 输出 屏幕截图
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); // 设置UTF-8支持
}