main_simple.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // #include <QApplication>
  2. // #include <QDebug>
  3. // #include <QOpenGLFunctions>
  4. // #include <QOpenGLTexture>
  5. // #include <QOpenGLWidget>
  6. // #include <QScreen>
  7. // #include <QTimer>
  8. // #include <QWindow>
  9. // #include <dwmapi.h>
  10. // #include <windows.h>
  11. // #pragma comment(lib, "dwmapi.lib")
  12. // #pragma comment(lib, "user32.lib")
  13. // class ThumbnailWidget : public QOpenGLWidget, protected QOpenGLFunctions
  14. // {
  15. // Q_OBJECT
  16. // public:
  17. // ThumbnailWidget(QWidget *parent = nullptr)
  18. // : QOpenGLWidget(parent)
  19. // {
  20. // setFixedSize(800, 600); // 固定预览区域大小
  21. // updateTimer = new QTimer(this);
  22. // connect(updateTimer, &QTimer::timeout, this, &ThumbnailWidget::updateThumbnails);
  23. // updateTimer->start(200); // 每200ms更新一次
  24. // }
  25. // ~ThumbnailWidget()
  26. // {
  27. // makeCurrent();
  28. // for (auto texture : textures) {
  29. // delete texture;
  30. // }
  31. // textures.clear();
  32. // doneCurrent();
  33. // }
  34. // protected:
  35. // void initializeGL() override
  36. // {
  37. // initializeOpenGLFunctions();
  38. // glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
  39. // }
  40. // void resizeGL(int w, int h) override { glViewport(0, 0, w, h); }
  41. // void paintGL() override
  42. // {
  43. // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  44. // glEnable(GL_TEXTURE_2D);
  45. // glEnable(GL_BLEND);
  46. // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  47. // int cols = 4; // 每行4个缩略图
  48. // int thumbSize = width() / cols; // 保持1:1比例
  49. // int row = 0, col = 0;
  50. // for (auto texture : textures) {
  51. // if (!texture)
  52. // continue;
  53. // texture->bind();
  54. // glBegin(GL_QUADS);
  55. // int x = col * thumbSize;
  56. // int y = row * thumbSize;
  57. // glTexCoord2f(0, 0);
  58. // glVertex2f(x, y);
  59. // glTexCoord2f(1, 0);
  60. // glVertex2f(x + thumbSize, y);
  61. // glTexCoord2f(1, 1);
  62. // glVertex2f(x + thumbSize, y + thumbSize);
  63. // glTexCoord2f(0, 1);
  64. // glVertex2f(x, y + thumbSize);
  65. // glEnd();
  66. // if (++col >= cols) {
  67. // col = 0;
  68. // row++;
  69. // }
  70. // }
  71. // }
  72. // private slots:
  73. // void updateThumbnails()
  74. // {
  75. // // 清理旧纹理
  76. // makeCurrent();
  77. // for (auto texture : textures) {
  78. // delete texture;
  79. // }
  80. // textures.clear();
  81. // // 获取所有窗口
  82. // EnumWindows(enumWindowsProc, reinterpret_cast<LPARAM>(this));
  83. // // 添加桌面预览
  84. // addDesktopThumbnail();
  85. // update();
  86. // }
  87. // private:
  88. // QTimer *updateTimer;
  89. // QVector<QOpenGLTexture *> textures;
  90. // static BOOL CALLBACK enumWindowsProc(HWND hwnd, LPARAM lParam)
  91. // {
  92. // if (!IsWindowVisible(hwnd))
  93. // return TRUE;
  94. // ThumbnailWidget *widget = reinterpret_cast<ThumbnailWidget *>(lParam);
  95. // widget->addWindowThumbnail(hwnd);
  96. // return TRUE;
  97. // }
  98. // void addWindowThumbnail(HWND hwnd)
  99. // {
  100. // RECT rect;
  101. // GetClientRect(hwnd, &rect);
  102. // int width = rect.right - rect.left;
  103. // int height = rect.bottom - rect.top;
  104. // if (width == 0 || height == 0)
  105. // return;
  106. // // 创建临时纹理
  107. // QImage image(width, height, QImage::Format_ARGB32);
  108. // HDC hdcScreen = GetDC(NULL);
  109. // HDC hdcMem = CreateCompatibleDC(hdcScreen);
  110. // HBITMAP hbm = CreateCompatibleBitmap(hdcScreen, width, height);
  111. // SelectObject(hdcMem, hbm);
  112. // // 打印窗口内容到内存DC
  113. // PrintWindow(hwnd, hdcMem, PW_CLIENTONLY);
  114. // // 从HBITMAP获取数据
  115. // BITMAPINFO bmi = {0};
  116. // bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  117. // bmi.bmiHeader.biWidth = width;
  118. // bmi.bmiHeader.biHeight = -height; // 从上到下
  119. // bmi.bmiHeader.biPlanes = 1;
  120. // bmi.bmiHeader.biBitCount = 32;
  121. // bmi.bmiHeader.biCompression = BI_RGB;
  122. // GetDIBits(hdcMem, hbm, 0, height, image.bits(), &bmi, DIB_RGB_COLORS);
  123. // // 创建OpenGL纹理
  124. // makeCurrent();
  125. // QOpenGLTexture *texture = new QOpenGLTexture(image.mirrored());
  126. // texture->setMinificationFilter(QOpenGLTexture::Linear);
  127. // texture->setMagnificationFilter(QOpenGLTexture::Linear);
  128. // textures.append(texture);
  129. // // 清理资源
  130. // DeleteObject(hbm);
  131. // DeleteDC(hdcMem);
  132. // ReleaseDC(NULL, hdcScreen);
  133. // }
  134. // void addDesktopThumbnail()
  135. // {
  136. // QScreen *screen = QApplication::primaryScreen();
  137. // QPixmap desktopPix = screen->grabWindow(0);
  138. // QImage image = desktopPix.toImage();
  139. // makeCurrent();
  140. // QOpenGLTexture *texture = new QOpenGLTexture(image);
  141. // texture->setMinificationFilter(QOpenGLTexture::Linear);
  142. // texture->setMagnificationFilter(QOpenGLTexture::Linear);
  143. // textures.prepend(texture); // 桌面放在第一个
  144. // }
  145. // };
  146. // int main(int argc, char *argv[])
  147. // {
  148. // QApplication app(argc, argv);
  149. // ThumbnailWidget widget;
  150. // widget.setWindowTitle("Window Thumbnail Preview");
  151. // widget.show();
  152. // return app.exec();
  153. // }
  154. // #include "main_simple.moc"