Skip to content

Instantly share code, notes, and snippets.

@Nukem9
Created September 11, 2016 23:17
Show Gist options
  • Save Nukem9/7f85beec2b22eed03b4de6684d4a6512 to your computer and use it in GitHub Desktop.
Save Nukem9/7f85beec2b22eed03b4de6684d4a6512 to your computer and use it in GitHub Desktop.
int main(int, char**)
{
// Create application window
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, _T("ImGui Example"), NULL };
RegisterClassEx(&wc);
HWND hwnd = CreateWindow(_T("ImGui Example"), _T("ImGui DirectX11 Example"), WS_POPUP, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
// Initialize Direct3D
if (CreateDeviceD3D(hwnd) < 0)
{
CleanupDeviceD3D();
UnregisterClass(_T("ImGui Example"), wc.hInstance);
return 1;
}
// Show the window
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
// Setup ImGui binding
ImGui_ImplDX11_Init(hwnd, g_pd3dDevice, g_pd3dDeviceContext);
bool show_another_window = true;
ImVec4 clear_col = ImColor(114, 144, 154);
int moveOffsetX = 0;
int moveOffsetY = 0;
// Main loop
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
ImGui_ImplDX11_NewFrame();
if (ImGui::IsMouseClicked(0))
{
POINT point;
RECT rect;
GetCursorPos(&point);
GetWindowRect(hwnd, &rect);
// Calculate the difference between the cursor pos and window pos
moveOffsetX = point.x - rect.left;
moveOffsetY = point.y - rect.top;
}
// 2. Show another simple window, this time using an explicit Begin/End pair
ImGui::Begin("Another Window", &show_another_window, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
{
ImGui::SetWindowPos(ImVec2(0, 0), ImGuiSetCond_Once);
ImGui::SetWindowSize(ImVec2(1280, 800), ImGuiSetCond_Once);
ImGui::Text("Hello");
if ((moveOffsetY >= 0 && moveOffsetY <= 22) && // Cursor must be *on* the titlebar
ImGui::IsMouseDragging()) // User must drag mouse (hold LMB)
{
POINT point;
GetCursorPos(&point);
SetWindowPos(hwnd, nullptr, point.x - moveOffsetX, point.y - moveOffsetY, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
}
ImGui::End();
// Rendering
g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, (float*)&clear_col);
ImGui::Render();
g_pSwapChain->Present(0, 0);
}
ImGui_ImplDX11_Shutdown();
CleanupDeviceD3D();
UnregisterClass(_T("ImGui Example"), wc.hInstance);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment