绘制一行文本
使用windows函数获取窗口句柄后使用imgui绘制。
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include "raylib.h" #include "imgui.h" #include "imgui_impl_win32.h" #include "imgui_impl_opengl3.h" #include "Win32Utils.h"
#ifndef _DEBUG
#pragma comment(lib, "raylib.lib") #pragma comment(lib, "winmm.lib")
#endif
int main() { InitWindow(1280, 720, "Fixed Conflict Architecture");
void* handle = GetRaylibNativeWindow();
ImGui::CreateContext(); ImGui_ImplWin32_Init(handle); ImGui_ImplOpenGL3_Init("#version 330");
while (!WindowShouldClose()) { ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplWin32_NewFrame(); ImGui::NewFrame();
ImGui::Begin("Success!"); ImGui::Text("No Header Conflicts Anymore."); ImGui::End();
BeginDrawing(); ClearBackground(BLACK);
ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); EndDrawing(); }
return 0; }
|
Win32Utils.h
1 2 3
| #pragma once
void* GetRaylibNativeWindow();
|
Win32Utils.cpp
1 2 3 4 5 6 7 8 9 10 11
| #define SM_CMONITORS 80 #include <windows.h> #define RAYLIB_H #include "Win32Utils.h"
extern "C" void* GetWindowHandle(void);
void* GetRaylibNativeWindow() { return GetWindowHandle(); }
|
配置编译环境
1.下载imgui
2.将相关文件链接到项目中
在vs2022中添加imgui目录与backend目录。
将所需cpp文件在项目中引用
1 2 3 4 5
| imgui.cpp imgui_demo.cpp imgui_draw.cpp imgui_tables.cpp imgui_widgets.cpp
|
此项目需要引用win32+opengl3
1 2
| imgui_impl_opengl3.cpp imgui_impl_win32.cpp
|
绘制按钮并添加鼠标点击事件
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include "raylib.h" #include "imgui.h" #include "imgui_impl_win32.h" #include "imgui_impl_opengl3.h" #include "Win32Utils.h"
#pragma comment(lib, "opengl32.lib")
#ifndef _DEBUG
#pragma comment(lib, "raylib.lib") #pragma comment(lib, "winmm.lib")
#endif
#include "GuiManager.h"
void DrawMyUserInterface() { ImGui::Begin("control");
if (ImGui::Button("exec scan")) { TraceLog(LOG_INFO, "start scan..."); } ImGui::End();
}
int main() { GuiManager::Init(1280, 720, "Raylib + ImGui Pro");
while (!WindowShouldClose()) {
GuiManager::NewFrame();
DrawMyUserInterface();
if (GuiManager::ShouldProcessGameInput()) { BeginDrawing(); ClearBackground(DARKGRAY); DrawText("Raylib 画出的字", 10, 50, 20, RAYWHITE); }
GuiManager::EndFrame(); }
GuiManager::Shutdown();
return 0; }
|
GuiManager.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #pragma once #include "raylib.h" #include "imgui.h"
namespace GuiManager { void Init(int width, int height, const char* title);
void NewFrame(); void EndFrame();
void Shutdown();
bool ShouldProcessGameInput(); }
|
Win32Utils.h
1 2 3 4 5 6
| #pragma once
void* GetNativeWindowHandle(); void InstallImGuiHook(void* windowHandle);
|
GuiManager.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include "GuiManager.h" #include "raylib.h" #include "Win32Utils.h" #include "imgui.h" #include "imgui_impl_opengl3.h" #include "imgui_impl_win32.h"
namespace GuiManager {
void GuiManager::Init(int w, int h, const char* title) { InitWindow(w, h, title);
void* handle = GetNativeWindowHandle();
ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); const char* fontPath = "C:\\Windows\\Fonts\\msyh.ttc"; ImFont* font = io.Fonts->AddFontFromFileTTF(fontPath, 18.0f, NULL, io.Fonts->GetGlyphRangesChineseFull()); if (font == NULL) { io.Fonts->AddFontDefault(); } InstallImGuiHook(handle);
ImGui_ImplWin32_Init(handle); ImGui_ImplOpenGL3_Init("#version 330"); }
void GuiManager::NewFrame() { ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplWin32_NewFrame(); ImGui::NewFrame();
BeginDrawing(); ClearBackground(DARKGRAY); }
void EndFrame() { ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
EndDrawing(); }
bool ShouldProcessGameInput() { return !ImGui::GetIO().WantCaptureMouse && !ImGui::GetIO().WantCaptureKeyboard; }
void Shutdown() { ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplWin32_Shutdown(); ImGui::DestroyContext(); CloseWindow(); } }
|
Win32Utils.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #define VC_EXTRALEAN #define WIN32_LEAN_AND_MEAN #include <windows.h> #include "Win32Utils.h" #include "imgui.h" extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
extern "C" void* GetWindowHandle(void);
static WNDPROC OriginalRaylibProc = NULL;
LRESULT CALLBACK MyInternalWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { if (ImGui_ImplWin32_WndProcHandler(hwnd, msg, wParam, lParam)) return 1;
return CallWindowProc(OriginalRaylibProc, hwnd, msg, wParam, lParam); }
void* GetNativeWindowHandle() { return GetWindowHandle(); }
void InstallImGuiHook(void* windowHandle) { HWND hwnd = (HWND)windowHandle; if (hwnd && OriginalRaylibProc == NULL) { OriginalRaylibProc = (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)MyInternalWndProc); } }
|