'GDI+ PenAlignmentInset seems to be different than GDI PS_INSIDEFRAME
I've encountered a strange issue when trying to draw rectangles with GDI+, but I'm not sure if I'm doing something wrong.
The GDI+ pen alignment setting, PenAlignmentInset, seems to not work properly when the pen width is 1.
I've created a minimally reproducible demo below in order to illustrate the point. First, a red GDI rectangle is drawn using PS_INSIDEFRAME, and then a blue GDI+ rectangle is drawn right over it using PenAlignmentInset. When the pen widths are 4 and 2, for example, the GDI+ rectangle completely covers the red GDI rectangle, as you’d expect. Their alignment corresponds. But when the pen width is 1, the GDI+ rectangle’s border placement is incorrect, I believe, and part of the red GDI rectangle is thus visible.
#include <windows.h>
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
const wchar_t CLASS_NAME[] = L"MyClass";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"GDI+ bug demo", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Gdiplus::GdiplusShutdown(gdiplusToken);
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
// Demonstrating a width of 4 units
{
HPEN hpen = CreatePen(PS_INSIDEFRAME, 4, RGB(255, 0, 0));
HGDIOBJ hgdiobjOldPen = SelectObject(hdc, hpen);
Rectangle(hdc, 50, 50, 150, 150);
SelectObject(hdc, hgdiobjOldPen);
DeleteObject(hpen);
Gdiplus::Graphics gfx(hdc);
Gdiplus::Pen pen(Gdiplus::Color(0, 0, 255), 4);
pen.SetAlignment(Gdiplus::PenAlignmentInset);
gfx.DrawRectangle(&pen, 50, 50, 100, 100);
}
// Demonstrating a width of 2 units
{
HPEN hpen = CreatePen(PS_INSIDEFRAME, 2, RGB(255, 0, 0));
HGDIOBJ hgdiobjOldPen = SelectObject(hdc, hpen);
Rectangle(hdc, 200, 50, 300, 150);
SelectObject(hdc, hgdiobjOldPen);
DeleteObject(hpen);
Gdiplus::Graphics gfx(hdc);
Gdiplus::Pen pen(Gdiplus::Color(0, 0, 255), 2);
pen.SetAlignment(Gdiplus::PenAlignmentInset);
gfx.DrawRectangle(&pen, 200, 50, 100, 100);
}
// Demonstrating a width of 1 unit - PROBLEM !!!!
{
HPEN hpen = CreatePen(PS_INSIDEFRAME, 1, RGB(255, 0, 0));
HGDIOBJ hgdiobjOldPen = SelectObject(hdc, hpen);
Rectangle(hdc, 350, 50, 450, 150);
SelectObject(hdc, hgdiobjOldPen);
DeleteObject(hpen);
Gdiplus::Graphics gfx(hdc);
Gdiplus::Pen pen(Gdiplus::Color(0, 0, 255), 1);
pen.SetAlignment(Gdiplus::PenAlignmentInset);
gfx.DrawRectangle(&pen, 350, 50, 100, 100);
}
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Does anyone know why that might be happening? Thank you for any input.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

