cairo & cairomm example
2009-11-18
아래의 코드들은 wxWidgets와의 조합이며, wxEVT_PAINT 이벤트내에서 작성하면 될것이다. MinGW로 테스트 되었다.
아래의 코드는 cairo의 경우이다.
wxPaintDC dc(this); wxRect rect = GetClientRect(); if(rect.width == 0 || rect.height == 0) return; // RENDER_NATIVE HWND hWnd = (HWND)this->GetHandle(); HDC hDC = ::GetDC(hWnd); HDC hMemDC = CreateCompatibleDC(hDC); HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rect.width, rect.height); SelectObject(hMemDC, hBitmap); cairo_surface_t* surface = cairo_win32_surface_create(hMemDC); cairo_t* image = cairo_create(surface); cairo_pattern_t *pat; pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, 256.0); cairo_pattern_add_color_stop_rgba (pat, 1, 0, 0, 0, 1); cairo_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1); cairo_rectangle (image, 0, 0, rect.width, rect.height); cairo_set_source (image, pat); cairo_fill (image); cairo_pattern_destroy (pat); pat = cairo_pattern_create_radial (115.2, 102.4, 25.6, 102.4, 102.4, 128.0); cairo_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1); cairo_pattern_add_color_stop_rgba (pat, 1, 0, 0, 0, 1); cairo_set_source (image, pat); cairo_arc (image, 128.0, 128.0, 76.8, 0, 2 * M_PI); cairo_fill (image); cairo_pattern_destroy (pat); BitBlt(hDC, 0, 0, rect.width, rect.height, hMemDC, 0, 0, SRCCOPY); DeleteDC(hMemDC); DeleteObject(hBitmap); // Because we called ::GetDC make sure we relase the handle // back to the system or we'll have a memory leak. ::ReleaseDC(hWnd, hDC);
아래의 코드는 cairomm의 경우이다.
wxPaintDC dc(this); wxRect rect = GetClientRect(); if(rect.width == 0 || rect.height == 0) return; // RENDER_NATIVE HWND hWnd = (HWND)this->GetHandle(); HDC hDC = ::GetDC(hWnd); HDC hMemDC = CreateCompatibleDC(hDC); HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rect.width, rect.height); SelectObject(hMemDC, hBitmap); Cairo::RefPtr<Context> context; Cairo::RefPtr<Win32Surface> surface; surface = Win32Surface::create(hMemDC); context = Context::create(surface); // draw background Cairo::RefPtr<Cairo::LinearGradient> background_gradient_ptr = Cairo::LinearGradient::create (0, 0, rect.width, rect.height); background_gradient_ptr->add_color_stop_rgb (0, 0.7,0,0); background_gradient_ptr->add_color_stop_rgb (1, 0.3,0,0); // Draw a rectangle and fill with gradient context->set_source (background_gradient_ptr); context->rectangle (0, 0, rect.width, rect.height); context->fill(); surface->finish(); BitBlt(hDC, 0, 0, rect.width, rect.height, hMemDC, 0, 0, SRCCOPY); DeleteDC(hMemDC); DeleteObject(hBitmap); // Because we called ::GetDC make sure we relase the handle // back to the system or we'll have a memory leak. ::ReleaseDC(hWnd, hDC);
Categorized as: Programming
답글 남기기