aa7481542 2024-10-11 22:38 采纳率: 16.7%
浏览 47
已结题

C++ 菜单窗口独立出来,可以随意移动放大缩小。

如何让窗口独立出来,不要在主窗口里面,独立随意移动放大缩小。



```c++

static void Render(ID3D11Device* g_pd3dDevice, const ImFontAtlas* FontAtlas)
{
    ImGui::SetNextWindowViewport(ImGui::GetMainViewport()->ID);
    ImGui::Begin("Map", nullptr, ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize);
    RECT Rect;
    GetWindowRect(GameData.Config.Overlay.hWnd, &Rect);
    DrawStates(Rect, FontAtlas);
    if (GameData.Config.ESP.MinMap)
    {
        std::string MapFile = "Assets/Maps/" + GameData.MapName + ".png";
        if (MapsSize[GameData.MapName].Size > 0 && GetFileAttributesA(MapFile.c_str()) != INVALID_FILE_ATTRIBUTES) {
           auto Allocator = Texture::LoadTexture(g_pd3dDevice, "Assets/Maps/" + GameData.MapName + ".png");
           GameData.Radar.ImageMapHeight = Allocator.Height;
           static DMARender::MapTransform mTrans = DMARender::MapTransform();
           if (mTrans.mapZoom < 0) {
               float mapSize = fmaxf(Allocator.Width, Allocator.Height);
               float screenSize = fminf(Rect.right - Rect.left, Rect.bottom - Rect.top);
               mTrans.mapZoom = screenSize / mapSize;
               mTrans.mapHeight = Allocator.Height;
               mTrans.mapWidth = Allocator.Width;
           }
           auto mousePos = ImGui::GetMousePos();
           static float lastMousePosX = mousePos.x;
           static float lastMousePosY = mousePos.y;
           if (ImGui::IsMouseDown(ImGuiMouseButton_Left) && !ImGui::GetIO().WantCaptureMouse) {
               mTrans.dragOffsetX += mousePos.x - lastMousePosX;
               mTrans.dragOffsetY += mousePos.y - lastMousePosY;
           }
           if (ImGui::GetIO().MouseWheel != 0.0f) {
               float oldZoom = mTrans.mapZoom;
               //Zoom in/out
               mTrans.mapZoom *= (1 + (ImGui::GetIO().MouseWheel * .05));
               if (mTrans.mapZoom < 0.01)
                   mTrans.mapZoom = 0.01;
               //Zoom toward cursor
               float deltaX = (Allocator.Width * oldZoom) - (Allocator.Width * mTrans.mapZoom);
               float deltaY = (Allocator.Height * oldZoom) - (Allocator.Height * mTrans.mapZoom);
               float percX = (mousePos.x - Rect.left - mTrans.dragOffsetX) / ((Allocator.Width * mTrans.mapZoom));
               float percY = (mousePos.y - Rect.top - mTrans.dragOffsetY) / ((Allocator.Width * mTrans.mapZoom));
               mTrans.dragOffsetX += (deltaX * percX);
               mTrans.dragOffsetY += (deltaY * percY);
           }
           lastMousePosX = mousePos.x;
           lastMousePosY = mousePos.y;
           ImDrawList* fgDrawList = ImGui::GetBackgroundDrawList();
           fgDrawList->AddImage(
               Allocator.Texture,
               ImVec2(
                   Rect.left + mTrans.dragOffsetX,
                   Rect.top + mTrans.dragOffsetY
               ),
               ImVec2(
                   Rect.left + mTrans.dragOffsetX + (Allocator.Width * mTrans.mapZoom),
                   Rect.top + mTrans.dragOffsetY + (Allocator.Height * mTrans.mapZoom)
               )
           );
           DrawOverlay(Rect, mTrans);
        }
        else {
           DrawStates(Rect, FontAtlas);
        }
    }

    DrawFPS(Rect);

    ImGui::End();

    DrawButton(Rect);


}

```

  • 写回答

8条回答 默认 最新

  • 叫兽-郭老师 新星创作者: Java技术领域 2024-10-11 22:39
    关注

    🕗晚上好,题主!目前:🟢[在线]
    📢参考通义千问、由郭老师整理提供!
    📣今日诗号:我心匪石,不可转也。

    很遗憾,仅凭你提供的代码片段,我无法给出完整的解决方案。要实现 C++ 中可独立移动、放大缩小的菜单窗口,需要更完整的代码和上下文信息。

    不过,我可以给你提供一些思路和代码示例,帮助你实现这个功能:

    思路:

    1. 创建独立窗口: 你需要创建一个独立于主窗口的子窗口,用于显示菜单。可以使用 Windows API (CreateWindowEx) 或跨平台库 (Qt, wxWidgets, SFML) 来实现。

    2. 窗口样式: 设置窗口样式以允许移动和调整大小。在 Windows API 中,可以使用 WS_CAPTION | WS_SYSMENU | WS_THICKFRAME 样式。

    3. 处理窗口消息: 你需要处理窗口消息,例如鼠标点击、鼠标移动和窗口大小改变事件,以实现移动和调整大小的功能。

    4. 渲染菜单内容: 在子窗口的客户区绘制菜单内容,可以使用 GDI/GDI+、Direct2D 或者其他图形库。

    代码示例 (Windows API):

    #include <windows.h>
    
    // 全局变量
    HWND hWndMenu; // 菜单窗口句柄
    
    // 窗口过程函数
    LRESULT CALLBACK MenuWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
      switch (message) {
        case WM_PAINT: {
          PAINTSTRUCT ps;
          HDC hdc = BeginPaint(hWnd, &ps);
          // TODO: 在此处绘制菜单内容
          EndPaint(hWnd, &ps);
          break;
        }
        case WM_DESTROY:
          PostQuitMessage(0);
          break;
        default:
          return DefWindowProc(hWnd, message, wParam, lParam);
      }
      return 0;
    }
    
    // 创建菜单窗口
    bool CreateMenuWindow(HWND hWndParent) {
      WNDCLASSEX wcex = {0};
      wcex.cbSize = sizeof(WNDCLASSEX);
      wcex.style = CS_HREDRAW | CS_VREDRAW;
      wcex.lpfnWndProc = MenuWndProc;
      wcex.hInstance = GetModuleHandle(NULL);
      wcex.lpszClassName = L"MenuWindowClass";
      if (!RegisterClassEx(&wcex)) {
        return false;
      }
    
      hWndMenu = CreateWindowEx(
          0,
          L"MenuWindowClass",
          L"Menu Window",
          WS_CAPTION | WS_SYSMENU | WS_THICKFRAME,
          CW_USEDEFAULT, CW_USEDEFAULT,
          300, 200,
          hWndParent,
          NULL,
          GetModuleHandle(NULL),
          NULL);
    
      if (!hWndMenu) {
        return false;
      }
    
      ShowWindow(hWndMenu, SW_SHOW);
      UpdateWindow(hWndMenu);
      return true;
    }
    
    int main() {
      // ... 创建主窗口 ...
    
      // 创建菜单窗口
      if (!CreateMenuWindow(hWndMain)) {
        // 处理错误
      }
    
      // ... 消息循环 ...
    
      return 0;
    }
    

    其他建议:

    • 考虑使用现有的 GUI 库,例如 Qt、wxWidgets、SFML,它们可以简化窗口创建和事件处理。
    • 使用资源编辑器创建菜单界面,可以更方便地设计和布局菜单元素。
    • 使用双缓冲技术避免绘制闪烁。

    希望以上信息能帮助你实现 C++ 可独立移动、放大缩小的菜单窗口!如果还有其他问题,请随时提问。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月12日
  • 创建了问题 10月11日