Skip to content

Commit

Permalink
fix: 修复 resizewindow() 会引起程序崩溃的问题(#59 引入) (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
yixy-only authored May 20, 2024
1 parent 6076175 commit 769a6e0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ege_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void setmode(int gdriver, int gmode);
// GDI+ 初始化
void gdipluinit();

Gdiplus::Graphics* recreateGdiplusGraphics(HDC hdc, const Gdiplus::Graphics* oldGraphics);

int swapbuffers();

bool isinitialized();
Expand Down
47 changes: 47 additions & 0 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,4 +1049,51 @@ void gdipluinit()
}
}

/**
* @brief 重新创建 Graphics 对象,并保持和原来的 Graphics 同样的属性配置
*
* @param hdc 图像所持有的 DC 句柄
* @param oldGraphics 旧 graphics 对象,如果为 NULL 则仅创建新的 Graphics 对象,不做额外的设置
* @return Gdiplus::Graphics* 创建的 Graphics 对象
*/
Gdiplus::Graphics* recreateGdiplusGraphics(HDC hdc, const Gdiplus::Graphics* oldGraphics)
{
Gdiplus::Graphics* newGraphics = Gdiplus::Graphics::FromHDC(hdc);

/* 保持与原来相同的设置 */
if (oldGraphics != NULL) {
/* 坐标变换设置 */
Gdiplus::Matrix transform;
oldGraphics->GetTransform(&transform);
newGraphics->SetTransform(&transform);

/* 绘图质量设置 */
newGraphics->SetSmoothingMode(oldGraphics->GetSmoothingMode());
newGraphics->SetInterpolationMode(oldGraphics->GetInterpolationMode());
newGraphics->SetPixelOffsetMode(oldGraphics->GetPixelOffsetMode());
newGraphics->SetTextRenderingHint(oldGraphics->GetTextRenderingHint());
newGraphics->SetCompositingQuality(oldGraphics->GetCompositingQuality());
newGraphics->SetTextContrast(oldGraphics->GetTextContrast());

/* 组合模式设置*/
newGraphics->SetCompositingMode(oldGraphics->GetCompositingMode());

/* 裁剪区域设置 */
Gdiplus::Region clipRegion;
oldGraphics->GetClip(&clipRegion);
newGraphics->SetClip(&clipRegion);

/* 页面单位和比例设置 */
newGraphics->SetPageUnit(oldGraphics->GetPageUnit());
newGraphics->SetPageScale(oldGraphics->GetPageScale());

/* 渲染原点 */
INT x, y;
oldGraphics->GetRenderingOrigin(&x, &y);
newGraphics->SetRenderingOrigin(x, y);
}

return newGraphics;
}

} // namespace ege
7 changes: 7 additions & 0 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ int IMAGE::resize_f(int width, int height)
m_height = height;
m_pBuffer = bmp_buf;

// BITMAP 更换后需重新创建 Graphics 对象(否则会在已销毁的 old_bitmap 上绘制,引发异常)
if (m_graphics != NULL) {
Gdiplus::Graphics* newGraphics = recreateGdiplusGraphics(m_hDC, m_graphics);
delete m_graphics;
m_graphics = newGraphics;
}

setviewport(0, 0, m_width, m_height, 1, this);

return 0;
Expand Down

0 comments on commit 769a6e0

Please sign in to comment.