--- programs/regedit/hexedit.c	2016-09-16 17:14:20.000000000 +0200
+++ programs/regedit/hexedit.c	2016-09-18 12:28:42.548763767 +0200
@@ -37,7 +37,7 @@
 #include "main.h"
 
 /* spaces dividing hex and ASCII */
-#define DIV_SPACES 4
+#define DIV_SPACES 3
 
 typedef struct tagHEXEDIT_INFO
 {
@@ -51,6 +51,7 @@ typedef struct tagHEXEDIT_INFO
     BYTE *pData;
     INT   cbData;
     INT   nBytesPerLine; /* bytes of hex to display per line of the control */
+    INT   asciiWidth;
     INT   nScrollPos; /* first visible line */
 } HEXEDIT_INFO;
 
@@ -75,7 +76,7 @@ static LPWSTR HexEdit_GetLineText(BYTE *
     static const WCHAR percent_02xW[] = {'%','0','2','X',' ',0};
 
     LPWSTR lpszLine = HeapAlloc(GetProcessHeap(), 0,
-        (cbData * 3 + pad * 3 + DIV_SPACES + cbData + 1) * sizeof(WCHAR));
+        (cbData * 3 + pad * 3 + DIV_SPACES + cbData + 4) * sizeof(WCHAR));
     LONG i;
 
     if (!lpszLine)
@@ -99,7 +100,9 @@ static LPWSTR HexEdit_GetLineText(BYTE *
         else
             lpszLine[cbData * 3 + pad * 3 + DIV_SPACES + i] = '.';
     }
-    lpszLine[cbData * 3 + pad * 3 + DIV_SPACES + cbData] = 0;
+    for (i = 0; i < 3; i++)
+        lpszLine[cbData * 3 + pad * 3 + DIV_SPACES + cbData + i] = ' ';
+    lpszLine[cbData * 3 + pad * 3 + DIV_SPACES + cbData + 3] = 0;
     return lpszLine;
 }
 
@@ -108,11 +111,12 @@ HexEdit_Paint(HEXEDIT_INFO *infoPtr)
 {
     PAINTSTRUCT ps;
     HDC hdc = BeginPaint(infoPtr->hwndSelf, &ps);
+    INT *nWidths = HeapAlloc(GetProcessHeap(), 0, (infoPtr->nBytesPerLine * 4 + DIV_SPACES + 3) * sizeof(INT));
     INT nXStart, nYStart;
     COLORREF clrOldText;
     HFONT hOldFont;
     BYTE *pData;
-    INT iMode;
+    INT i,iMode;
     LONG lByteOffset = infoPtr->nScrollPos * infoPtr->nBytesPerLine;
 
     /* Make a gap from the frame */
@@ -126,7 +130,9 @@ HexEdit_Paint(HEXEDIT_INFO *infoPtr)
 
     iMode = SetBkMode(hdc, TRANSPARENT);
     hOldFont = SelectObject(hdc, infoPtr->hFont);
-        
+
+    for (i = 0; i < (infoPtr->nBytesPerLine * 4 + DIV_SPACES + 3); i++)
+        nWidths[i] = infoPtr->asciiWidth;
     for (pData = infoPtr->pData + lByteOffset; pData < infoPtr->pData + infoPtr->cbData; pData += infoPtr->nBytesPerLine)
     {
         LPWSTR lpszLine;
@@ -136,11 +142,13 @@ HexEdit_Paint(HEXEDIT_INFO *infoPtr)
         lpszLine = HexEdit_GetLineText(pData, nLineLen, infoPtr->nBytesPerLine - nLineLen);
 
         /* FIXME: draw hex <-> ASCII mapping highlighted? */
-        TextOutW(hdc, nXStart, nYStart, lpszLine, infoPtr->nBytesPerLine * 3 + DIV_SPACES + nLineLen);
+        //TextOutW(hdc, nXStart, nYStart, lpszLine, infoPtr->nBytesPerLine * 3 + DIV_SPACES + nLineLen + 3);
+        ExtTextOutW(hdc, nXStart, nYStart, 0, NULL, lpszLine, infoPtr->nBytesPerLine * 3 + DIV_SPACES + nLineLen, nWidths);
 
         nYStart += infoPtr->nHeight;
         HeapFree(GetProcessHeap(), 0, lpszLine);
     }
+    HeapFree(GetProcessHeap(), 0, nWidths);
 
     SelectObject(hdc, hOldFont);
     SetBkMode(hdc, iMode);
@@ -176,12 +184,12 @@ HexEdit_UpdateCaret(HEXEDIT_INFO *infoPt
     SelectObject(hdc, hOldFont);
     ReleaseDC(infoPtr->hwndSelf, hdc);
 
-    if (!nLineLen) size.cx = 0;
+    if (!nLineLen) { size.cx = 0; nCharOffset = 0; }
 
     HeapFree(GetProcessHeap(), 0, lpszLine);
 
     SetCaretPos(
-        GetSystemMetrics(SM_CXBORDER) + size.cx,
+        GetSystemMetrics(SM_CXBORDER) + infoPtr->asciiWidth * nCharOffset,
         GetSystemMetrics(SM_CYBORDER) + (nLine - infoPtr->nScrollPos) * infoPtr->nHeight);
 }
 
@@ -337,7 +345,7 @@ HexEdit_Char (HEXEDIT_INFO *infoPtr, WCH
 static inline LRESULT
 HexEdit_Create (HEXEDIT_INFO *infoPtr, LPCREATESTRUCTW lpcs)
 {
-    HexEdit_SetFont(infoPtr, GetStockObject(SYSTEM_FONT), FALSE);
+    HexEdit_SetFont(infoPtr, GetStockObject(ANSI_FIXED_FONT), FALSE);
     HexEdit_UpdateScrollbars(infoPtr);
 
     return 0;
@@ -510,7 +518,7 @@ HexEdit_SetFont (HEXEDIT_INFO *infoPtr,
     TEXTMETRICW tm;
     HDC hdc;
     HFONT hOldFont = NULL;
-    LONG i;
+    LONG i, asciiWidth;
     RECT rcClient;
 
     infoPtr->hFont = hFont;
@@ -532,13 +540,23 @@ HexEdit_SetFont (HEXEDIT_INFO *infoPtr,
         GetTextExtentPoint32W(hdc, lpszLine, lstrlenW(lpszLine), &size);
         HeapFree(GetProcessHeap(), 0, lpszLine);
         HeapFree(GetProcessHeap(), 0, pData);
-        if (size.cx > (rcClient.right - rcClient.left))
+        if (size.cx > (rcClient.right - rcClient.left - 32))
         {
             infoPtr->nBytesPerLine = i - 1;
             break;
         }
     }
 
+    infoPtr->asciiWidth = 1;
+    for (i = 32; i < 127; i++)
+    {
+        SIZE size;
+        GetTextExtentPoint32W(hdc, &i, 1, &size);
+        if (size.cx > asciiWidth)
+            infoPtr->asciiWidth = size.cx;
+    }
+    infoPtr->nBytesPerLine = (((rcClient.right - rcClient.left) / infoPtr->asciiWidth) - (DIV_SPACES + 3)) / 4;
+
     if (infoPtr->hFont)
         SelectObject(hdc, hOldFont);
     ReleaseDC (infoPtr->hwndSelf, hdc);
