主窗体圆角

  1. QPixmap pixmap(directoryOf("Images").absoluteFilePath("xiangximenu.png"));
  2. setMask(pixmap.mask()); // 可以不使用转换的, 使用一张专门的bitmap图片.
  3. setWindowFlags(Qt::Window|Qt::FramelessWindowHint);

只要让你的窗体执行了上面的代码就是几个角是圆角的,
xiangximenu.png 是一张圆角的图片
制作圆角图片大家可以访问这个网站
http://www.roundpic.com
解释:directory,只不过是为了解决平台兼容性的一段代码,所有的路径都是针对可执行文件的。


QT调用Win32_API

  1. #include <windows.h>
  2. ...
  3. #ifdef Q_WS_WIN //声明要使用windows API
  4. //获得窗口句柄
  5. //HWND myhwnd=this->winId();
  6. //关闭显示器API
  7. //::SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
  8. //注销用户API
  9. //ExitWindowsEx(EWX_LOGOFF, 0);
  10. //windows的messagebox,
  11. MessageBox(NULL,TEXT("This is windows window"),TEXT("HAHA YOYO"),MB_OK);
  12. #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设置两窗口父子关系

实践:
在头文件私有部分加入函数声明:

  1. static BOOL enumUserWindowsCB(HWNDhwnd,LPARAMlParam); //静态全局函数
  2. HWND findDesktopIconWnd();
  1. BOOL Widget::enumUserWindowsCB(HWND hwnd,LPARAM lParam)
  2. {
  3. long wflags = GetWindowLong(hwnd, GWL_STYLE);
  4. if(!(wflags & WS_VISIBLE)) return TRUE;
  5. HWND sndWnd;
  6. if( !(sndWnd=FindWindowEx(hwnd, NULL, L"SHELLDLL_DefView", NULL)) ) return TRUE;
  7. HWND targetWnd;
  8. if( !(targetWnd=FindWindowEx(sndWnd, NULL, L"SysListView32", L"FolderView")) ) return TRUE;
  9. HWND* resultHwnd = (HWND*)lParam;
  10. *resultHwnd = targetWnd;
  11. return FALSE;
  12. }
  13. HWND Widget::findDesktopIconWnd()
  14. {
  15. HWND resultHwnd = NULL;
  16. EnumWindows((WNDENUMPROC)enumUserWindowsCB, (LPARAM)&resultHwnd);
  17. return resultHwnd;
  18. }

有了上面两个函数的辅助,下面的工作就简单了,在需要的地方加入如下代码就可以实现了:

  1. HWND desktopHwnd = findDesktopIconWnd();
  2. if(desktopHwnd) SetParent(this->winId(), desktopHwnd);

From: https://www.cnblogs.com/hicjiajia/archive/2011/02/13/1953879.html


为程序添加图标

  1. 1. 添加资源文件, 在资源中添加文件, 记作文件名"ico.ico"
  2. 2. [*].pro文件增加一行 "RC_FILE = [].rc"
  3. 3. 在工程目录新建上一行添加文件名的文件"[*].rc"
  4. 4. rc中写入"IDI_ICON1 ICON DISCARDABLE [ico.ico]"
  5. 5. 编译工程

剪切板操作:

  1. //使用 QApplication::clipboard() 函数获得系统剪贴板对象。这个函数的返回值是 QClipboard 指针。
  2. QClipboard *board = QApplication::clipboard();
  3. //通过 setText(),setImage() 或者 setPixmap() 函数可以将数据放置到剪贴板内
  4. board->setText("Text from Qt Application");
  5. //使用text(),image() 或者 pixmap() 函数则可以从剪贴板获得数据
  6. QString str = board->text();

[保存/打开] 对话框

文件打开对话框

函数原型:

  1. QString getOpenFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )

代码示例:

  1. QString fileName;
  2. fileName = QFileDialog::getOpenFileName(this,
  3. tr("Open Config"),
  4. "",
  5. tr("SCD Files (*.scd);; CID Files (*.cid)"));
  6. if (!fileName.isNull())
  7. {
  8. //fileName即是选择的文件名
  9. }
  10. else
  11. //点是的取消

文件保存对话框

函数原型:

  1. QString getSaveFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )

使用示例:

  1. QString fileName;
  2. fileName = QFileDialog::getSaveFileName(this,
  3. tr("Open Config"),
  4. "",
  5. tr("Config Files (*.ifg)"));
  6. if (!fileName.isNull())
  7. {
  8. //fileName是文件名
  9. }
  10. else
  11. //点的是取消

设置文件过滤的示例

  1. "Image Files (*.png *.jpg *.bmp)" //多个文件使用空格分隔
  1. "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" //多个过滤使用两个分号分隔

屏幕截图

  1. #include <QPixmap>
  2. #include <QString>
  3. #include <QDebug>
  4. #include <QApplication>
  5. #include <QDesktopWidget>
  6. #include <QTextCodec>
  7. #include <QDir>
  8. #include <QScreen>
  9. #include <QDateTime>
  10. #include <QWindow>
  11. void Widget::on_pushButton_pressed()
  12. {
  13. qDebug() << "Widget::on_pushButton_pressed()" << "\r\n";
  14. qDebug() << "CurWorkPath: " << QDir::currentPath();
  15. QDesktopWidget& deskTop = *QApplication::desktop(); // 获取桌面
  16. QWindow * pobjWindow = deskTop.windowHandle(); // 获取桌面工具类
  17. QScreen* pobjScreen = pobjWindow->screen(); // 获取屏幕对象
  18. // 屏幕对象抓取屏幕Wnd, QT5 之后 不支持QPixMap::grabWindow()
  19. // 且 grabWindow() 不为静态函数
  20. QPixmap pixMap = pobjScreen->grabWindow(QApplication::desktop()->winId());
  21. QString strPicPath = "./%1_screenShot.jpg";
  22. qDebug() << "\r\nQDateTime::currentDateTimeUtc().toString(): " << QDateTime::currentDateTimeUtc().toString();
  23. strPicPath = strPicPath.arg(QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss")); // 格式化时间字符串
  24. qDebug() << "\r\nSavePicPath: " << strPicPath;
  25. pixMap.save(strPicPath, "JPG"); // 输出 屏幕截图
  26. QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); // 设置UTF-8支持
  27. }