LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CREATE: if (FAILED(D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory))) { return -1; // Fail CreateWindowEx. } return 0; case WM_DESTROY: DiscardGraphicsResources(); SafeRelease(&pFactory); PostQuitMessage(0); return 0; case WM_PAINT: OnPaint(); return 0; case WM_SIZE: Resize(); return 0; } return DefWindowProc(m_hwnd, uMsg, wParam, lParam); }
// DrawTimeFrequencyGrid drawing grids. We leave the renderTarget context up to the caller. // Note the top left is origen void DrawTimeFrequencyGrid::Draw( ID2D1RenderTarget *apRenderTarget, D2D1_RECT_F rc, ParameterPack *ptheParameterPack, double time0, double time1 ) { pRenderTarget = apRenderTarget; ptheFrequencyScale = ptheParameterPack->GetFrequencyScale(); // CString palette = ptheParameterPack->GetPalette(); SetPalette(); HRESULT hr = S_OK; hr = pRenderTarget->CreateSolidColorBrush(vGridColor, &pVGridBrush); if ( FAILED(hr)) return; hr = pRenderTarget->CreateSolidColorBrush(labelColor, &pLabelBrush); if ( FAILED(hr)) return; hr = pRenderTarget->CreateSolidColorBrush(textBackgroundColor, &pTextBackgroundBrush); if ( FAILED(hr)) return; // pRenderTarget->BeginDraw(); // Get display times and other information double highFrequencyLimit = ptheParameterPack->GetFilterHigh(); double lowFrequencyLimit = ptheParameterPack->GetFilterLow(); rectangleSize = pRenderTarget->GetSize(); dataBottom = rc.bottom; dataTop = rc.top; DrawVerticalGrid( 10, time0, time1 ); MAPDOUBLE *pLabelMap = ptheParameterPack->GetLabelMap(); if ( ptheParameterPack->GetScaleChoice() != SCALE_NONE ) DrawHorizontalGrid( pLabelMap, 10, lowFrequencyLimit, highFrequencyLimit ); DiscardGraphicsResources(); }
void GraphicsWindow::OnPaint() { HRESULT hr = CreateGraphicsResources(); if (SUCCEEDED(hr)) { PAINTSTRUCT ps; BeginPaint(m_hwnd, &ps); pRenderTarget->BeginDraw(); pRenderTarget->Clear( D2D1::ColorF(D2D1::ColorF::SkyBlue) ); // Clear the device to blue // Draw hands SYSTEMTIME time; GetLocalTime(&time); // 60 minutes = 30 degrees, 1 minute = 0.5 degree const float fHourAngle = (360.0f / 12) * (time.wHour) + (time.wMinute * 0.5f); const float fMinuteAngle =(360.0f / 60) * (time.wMinute); const float fSecondAngle = (360.0f / 60) * (time.wSecond); // Draw all stored shapes for (auto i = ellipses.begin(); i != ellipses.end(); ++i) { (*i)->Draw(pRenderTarget, pBrush); // Outline the selected item inside the loop to ensure it is at the right depth if (Selection() == *i) { pBrush->SetColor(D2D1::ColorF(D2D1::ColorF::Red)); // SetColor is a non-expensive operation pRenderTarget->DrawEllipse(Selection()->ellipse, pBrush, 2.0f); } // Add clock hands to the ellipses, because.. why not pBrush->SetColor(D2D1::ColorF(D2D1::ColorF::Black)); DrawClockHand(*i, 0.6f, fHourAngle, 6); DrawClockHand(*i, 0.85f, fMinuteAngle, 4); DrawClockHand(*i, 0.85f, fSecondAngle, 2); // Restore the identity transformation for the next ellipse after rotating the clock hands pRenderTarget->SetTransform( D2D1::Matrix3x2F::Identity() ); } hr = pRenderTarget->EndDraw(); // If an error occurs during any of the draw commands (begin, clear, fillellipse) it is returned here if (FAILED(hr) || hr == D2DERR_RECREATE_TARGET) { // If we failed for some reason, including if the device has become unavailable and we need to recreate then discard current resources DiscardGraphicsResources(); } EndPaint(m_hwnd, &ps); } }
void MainWindow::OnPaint() { HRESULT hr = CreateGraphicsResources(); if (SUCCEEDED(hr)) { PAINTSTRUCT ps; BeginPaint(m_hwnd, &ps); pRenderTarget->BeginDraw(); pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::SkyBlue)); pRenderTarget->FillEllipse(ellipse, pBrush); hr = pRenderTarget->EndDraw(); if (FAILED(hr) || hr == D2DERR_RECREATE_TARGET) { DiscardGraphicsResources(); } EndPaint(m_hwnd, &ps); } }
// CResonanceStudioDraw drawing // Note the top left is origen void ResonanceStudioDraw::Draw(HWND m_hWnd, RECT rc, CResonanceStudioDoc *pDoc ) { CWaitCursor wait; clientRectangle = rc; pTheCoordinator = pDoc->GetCoordinator(); if ( pTheCoordinator == nullptr ) { return; } ptheParameterPack = pTheCoordinator->GetParameterPack(); ptheFrequencyScale = ptheParameterPack->GetFrequencyScale(); CPropertiesWnd *pthePropertiesWnd = &(pTheMainFrame->m_wndProperties); pthePropertiesWnd->SetOutputStatus(); SetPalette(); HRESULT hr = CreateGraphicsResources(m_hWnd, rc); if (SUCCEEDED(hr)) { pRenderTarget->BeginDraw(); // set the background and force a paint while we do our procesing pRenderTarget->Clear(backgroundColor); if ( pTheCoordinator->ProcessAndGetWICBitmap( &pWICBitmap ) ) { // nothing to do. need actual screen size in renderTarget to map } // Get display times and other information highFrequencyLimit = ptheParameterPack->GetFilterHigh(); lowFrequencyLimit = ptheParameterPack->GetFilterLow(); rectangleSize = pRenderTarget->GetSize(); dataBottom = rectangleSize.height; dataTop = audioScreenFraction * rectangleSize.height; if (pWICBitmap != nullptr ) { // Copy WIC bitmap to ID2D1 bitmap hr = pRenderTarget->CreateBitmapFromWicBitmap( pWICBitmap, NULL, &pbitmap ); time0 = pTheCoordinator->GetCurrentDisplayStartTime(); time1 = pTheCoordinator->GetCurrentDisplayEndTime(); double rate = pTheCoordinator->GetSamplingRate(); SetStatus( time0, time1 ); SetPixelTimeGrid( time1-time0, rate ); // Use top 20% of screen for audio //const double xAxis = 0.1 * rectangleSize.height; double vScale = 0.2 * rectangleSize.height ; DrawAudioLine( 0.0, vScale ); // Display the bitmap. Collect rectangle data first. Display to bottom section, below audio UINT uiWidth = 0; UINT uiHeight = 0; pWICBitmap->GetSize( &uiWidth, &uiHeight ); D2D1_RECT_F sourceRectangle; CPropertiesWnd *pthePropertiesWnd = &(pTheMainFrame->m_wndProperties); if ( pthePropertiesWnd->GetFullScreenOrPan() == "Pan" ) { // Limit travel float x = (float) panViewCorner.x; float y = (float) panViewCorner.y; x = max( x, 0.0f ); y = max( y, 0.0f ); x = min( x, uiWidth - rectangleSize.width ); y = min( y, uiHeight - rectangleSize.height ); sourceRectangle = D2D1::RectF(x, y, x + rectangleSize.width, y + rectangleSize.height); } else { sourceRectangle = D2D1::RectF(0.0f, 0.0, (float) uiWidth, (float) uiHeight); } D2D1_RECT_F destinationRectangle = D2D1::RectF( 0.0f, (float) vScale, rectangleSize.width, rectangleSize.height); pRenderTarget->DrawBitmap(pbitmap, destinationRectangle,1.0F,D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, sourceRectangle); ptheDrawTimeFrequencyGrid->Draw( pRenderTarget, D2D1::RectF(0.0, (float) dataTop, rectangleSize.height, (float) dataBottom ), ptheParameterPack, time0, time1 ); } hr = pRenderTarget->EndDraw(); wait.Restore(); DiscardGraphicsResources(); } }
LRESULT GraphicsWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CREATE: // Should be created before the first WM_PAINT message if (FAILED(D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory))) { return -1; // Fail CreateWindowEx. } DPIScale::Initialize(pFactory); SetTimer(m_hwnd, m_nTimerID, 1000, NULL); return 0; case WM_DESTROY: KillTimer(m_hwnd, m_nTimerID); DiscardGraphicsResources(); SafeRelease(&pFactory); break; case WM_TIMER: InvalidateRect(m_hwnd, NULL, FALSE); // allow the clock faces to cycle the second hand break; case WM_PAINT: OnPaint(); return 0; case WM_LBUTTONDOWN: OnLButtonDown(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), (DWORD)wParam); return 0; case WM_LBUTTONUP: OnLButtonUp(); return 0; case WM_MOUSEMOVE: OnMouseMove(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), (DWORD)wParam); return 0; case WM_SIZE: Resize(); return 0; case WM_COMMAND: // We've received an accelerator command switch (LOWORD(wParam)) { case ID_DRAW_MODE: SetMode(DrawMode); break; case ID_SELECT_MODE: SetMode(SelectMode); break; case ID_TOGGLE_MODE: if (mode == DrawMode) { SetMode(SelectMode); } else { SetMode(DrawMode); } break; case ID_MOVE_UP: MoveSelectionUp(); break; case ID_MOVE_DOWN: MoveSelectionDown(); break; case ID_DELETE: DeleteSelection(); break; } return 0; case WM_SETCURSOR: // The mouse is back on our window, maintain the cursor display we had before if (LOWORD(lParam) == HTCLIENT) { SetCursor(hCursor); return TRUE; } break; } return DefWindowProc(m_hwnd, uMsg, wParam, lParam); }