#include "draw.hpp" #include "utils.hpp" #include "imgui/imgui.h" #include "imgui/imgui_impl_dx9.h" #include "imgui/imgui_impl_win32.h" #define STB_IMAGE_IMPLEMENTATION #include "stb/stb_image.h" #include#pragma comment(lib,"d3d9.lib") #include // 导入外部窗口过程函数 extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); namespace u_draw { LPDIRECT3D9 g_direct3d9 = nullptr; LPDIRECT3DDEVICE9 g_device9 = nullptr; D3DPRESENT_PARAMETERS g_present{ 0 }; LPDIRECT3DTEXTURE9 g_background = nullptr; // 创建设备 bool create_device(HWND h) { g_direct3d9 = Direct3DCreate9(D3D_SDK_VERSION); if (!g_direct3d9) return false; memset(&g_present, 0, sizeof(g_present)); g_present.Windowed = TRUE; g_present.SwapEffect = D3DSWAPEFFECT_DISCARD; g_present.BackBufferFormat = D3DFMT_UNKNOWN; g_present.EnableAutoDepthStencil = TRUE; g_present.AutoDepthStencilFormat = D3DFMT_D16; g_present.PresentationInterval = D3DPRESENT_INTERVAL_ONE; if (g_direct3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, h, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_present, &g_device9) < 0) return false; return true; } // 重置设备 void reset_device() { if (g_device9) { ImGui_ImplDX9_InvalidateDeviceObjects(); g_device9->Reset(&g_present); ImGui_ImplDX9_CreateDeviceObjects(); } } // 设置主题风格 void set_theme_style() { ImGuiStyle& style = ImGui::GetStyle(); style.Colors[ImGuiCol_Text] = ImVec4(1.0f, 1.0f, 1.0f, 1.00f); style.Colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f); style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.74f, 0.74f, 0.94f, 1.00f); style.Colors[ImGuiCol_Border] = ImVec4(0.50f, 0.50f, 0.50f, 0.60f); style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); style.Colors[ImGuiCol_frameBg] = ImVec4(0.62f, 0.70f, 0.72f, 0.56f); style.Colors[ImGuiCol_frameBgHovered] = ImVec4(0.95f, 0.33f, 0.14f, 0.47f); style.Colors[ImGuiCol_frameBgActive] = ImVec4(0.97f, 0.31f, 0.13f, 0.81f); style.Colors[ImGuiCol_TitleBg] = ImVec4(0.42f, 0.75f, 1.00f, 0.53f); style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.65f, 0.80f, 0.20f); style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.40f, 0.62f, 0.80f, 0.15f); style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.39f, 0.64f, 0.80f, 0.30f); style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.28f, 0.67f, 0.80f, 0.59f); style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.25f, 0.48f, 0.53f, 0.67f); style.Colors[ImGuiCol_CheckMark] = ImVec4(0.48f, 0.47f, 0.47f, 0.71f); style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.31f, 0.47f, 0.99f, 1.00f); style.Colors[ImGuiCol_Button] = ImVec4(1.00f, 0.79f, 0.18f, 0.78f); style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.42f, 0.82f, 1.00f, 0.81f); style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.72f, 1.00f, 1.00f, 0.86f); style.Colors[ImGuiCol_Header] = ImVec4(0.65f, 0.78f, 0.84f, 0.80f); style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.75f, 0.88f, 0.94f, 0.80f); style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.55f, 0.68f, 0.74f, 0.80f); style.Colors[ImGuiCol_ResizeGrip] = ImVec4(0.60f, 0.60f, 0.80f, 0.30f); style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.00f, 1.00f, 1.00f, 0.60f); style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.90f); style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(1.00f, 0.99f, 0.54f, 0.43f); style.Alpha = 1.0f; style.frameRounding = 4; style.IndentSpacing = 12.0f; style.frameBorderSize = 1.0f; style.WindowPadding = ImVec2{ 0.0f,0.0f }; } // 加载图片 LPDIRECT3DTEXTURE9 load_image(const char* f) { if (!g_device9) return nullptr; int width = 0, height = 0, channel = 0; unsigned char* data = stbi_load(f, &width, &height, &channel, 3); if (!data) return nullptr; LPDIRECT3DTEXTURE9 texture = nullptr; HRESULT hr = g_device9->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &texture, NULL); if (FAILED(hr)) { stbi_image_free(data); return nullptr; } int len = width * height * (channel + 1); std::unique_ptr buf(new unsigned char[len]); if (!buf) { stbi_image_free(data); texture->Release(); return nullptr; } for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { buf.get()[(i * width + j) * 4 + 0] = data[(i * width + j) * 3 + 2]; buf.get()[(i * width + j) * 4 + 1] = data[(i * width + j) * 3 + 1]; buf.get()[(i * width + j) * 4 + 2] = data[(i * width + j) * 3 + 0]; buf.get()[(i * width + j) * 4 + 3] = 0xff; } } D3DLOCKED_RECT lock_rect{ 0 }; hr = texture->LockRect(0, &lock_rect, NULL, 0); if (SUCCEEDED(hr) && lock_rect.pBits) { for (int y = 0; y < height; y++) memcpy((unsigned char*)lock_rect.pBits + lock_rect.Pitch * y, buf.get() + (width * (channel + 1)) * y, (width * (channel + 1))); texture->UnlockRect(NULL); } stbi_image_free(data); return texture; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { if (ImGui_ImplWin32_WndProcHandler(hWnd, message, wParam, lParam)) return true; switch (message) { case WM_SIZE: g_present.BackBufferWidth = LOWORD(lParam); g_present.BackBufferHeight = HIWORD(lParam); reset_device(); return 0; case WM_DESTROY: exit(-1); } return DefWindowProcW(hWnd, message, wParam, lParam); } bool initialize(HWND h) { // 创建设备 if (!create_device(h)) return false; // 创建imgui IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; // ImGui::StyleColorsLight(); // ImGui::StyleColorsClassic(); // ImGui::StyleColorsDark(); // 设置风格 set_theme_style(); // 加载中文字体 io.Fonts->AddFontFromFileTTF("C:\Windows\Fonts\msyh.ttc", 18.0f, NULL, io.Fonts->GetGlyphRangesChineseFull()); // 不需要ini文件 io.IniFilename = NULL; io.LogFilename = NULL; // 初始化 return ImGui_ImplWin32_Init(h) && ImGui_ImplDX9_Init(g_device9); } void render() { ImGui_ImplDX9_Newframe(); ImGui_ImplWin32_Newframe(); ImGui::Newframe(); if (g_background == nullptr) g_background = load_image("C:\Users\Fengyihua\Desktop\aa.jpg"); ImGui::GetBackgroundDrawList()->AddImage(g_background, ImVec2{ 0,0 }, ImVec2{ 1000,500 }); // ImGui::ShowDemoWindow(); draw(); if (ImGui::Begin("123")) { static float col[4]{ 1.0f,1.0f,1.0f,1.0f }; ImGuiStyle& style = ImGui::GetStyle(); style.Colors[ImGuiCol_Text] = ImVec4(col[0], col[1], col[2], 1.0f); ImGui::ColorPicker4("c", col); ImGui::End(); } ImGui::Endframe(); g_device9->SetRenderState(D3DRS_ZENABLE, FALSE); g_device9->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); g_device9->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); g_device9->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xFFFFFFFF, 1.0f, 0); if (g_device9->BeginScene() >= 0) { ImGui::Render(); ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); g_device9->EndScene(); } if (g_device9->Present(NULL, NULL, NULL, NULL) == D3DERR_DEVICELOST && g_device9->TestCooperativeLevel() == D3DERR_DEVICENOTRESET) reset_device(); } void draw() { ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoBackground; // ImGui::SetNextWindowBgAlpha(); 设置窗口背景透视度 ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(800, 310), ImGuiCond_FirstUseEver); if (ImGui::Begin("Self", 0, flags)) { const char* template_items_names[15] = { "Banana", "Apple", "Cherry", "Watermelon", "Grapefruit", "Strawberry", "Mango", "Kiwi", "Orange", "Pineapple", "Blueberry", "Plum", "Coconut", "Pear", "Apricot" }; const float TEXT_base_HEIGHT = ImGui::GetTextLineHeightWithSpacing(); static ImVec2 outer_size_value = ImVec2(0.0f, TEXT_base_HEIGHT * 12); struct MyItem { int ID; const char* Name; int Quantity; }; static ImVector items; if (items.empty()) { items.resize(30, MyItem()); for (int n = 0; n < 30; n++) { const int template_n = n % IM_ARRAYSIZE(template_items_names); MyItem& item = items[n]; item.ID = rand() % 100; item.Name = template_items_names[template_n]; item.Quantity = rand() % 100; } } ImGuiTableFlags table_flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_RowBg | ImGuiTableFlags_Borders | ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_SizingFixedFit; if (ImGui::BeginTable("table_advanced", 6, table_flags)) { ImGui::TableSetupColumn(u_utils::string_to_utf8("进程ID").c_str(), ImGuiTableColumnFlags_WidthFixed, 0.0f, 0); ImGui::TableSetupColumn(u_utils::string_to_utf8("进程名称").c_str(), ImGuiTableColumnFlags_WidthFixed, 0.0f, 1); ImGui::TableSetupColumn("Action", ImGuiTableColumnFlags_WidthFixed, 0.0f, 2); ImGui::TableSetupColumn("Quantity", ImGuiTableColumnFlags_WidthFixed, 0.0f, 3); ImGui::TableSetupColumn("Description", ImGuiTableColumnFlags_WidthFixed, 0.0f, 4); ImGui::TableSetupColumn("Hidden", ImGuiTableColumnFlags_WidthFixed, 0.0f, 5); ImGui::TableSetupScrollFreeze(1, 1); ImGui::TableHeadersRow(); ImGui::PushButtonRepeat(true); ImGuiListClipper clipper; clipper.Begin(items.Size); while (clipper.Step()) { for (int row_n = clipper.DisplayStart; row_n < clipper.DisplayEnd; row_n++) { MyItem* item = &items[row_n]; bool item_is_selected = false; ImGui::PushID(item->ID); ImGui::TableNextRow(); ImGui::TableSetColumnIndex(0); char label[32]{ 0 }; wsprintfA(label, "%04d", item->ID); if (ImGui::Selectable(label, item_is_selected, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) { } ImGui::TableSetColumnIndex(1); ImGui::TextUnformatted(item->Name); ImGui::TableSetColumnIndex(2); if (ImGui::SmallButton("Chop")) { item->Quantity += 1; } ImGui::SameLine(); if (ImGui::SmallButton("Eat")) { item->Quantity -= 1; } ImGui::TableSetColumnIndex(3); ImGui::Text("%d", item->Quantity); ImGui::TableSetColumnIndex(4); ImGui::Text("Lorem ipsum dolor sit amet"); ImGui::TableSetColumnIndex(5); ImGui::Text("1234"); // ImGui::TableSetColumnIndex(6); // ImGui::Text("1234"); ImGui::PopID(); } } ImGui::PopButtonRepeat(); ImGui::EndTable(); } ImGui::End(); } } }



