title: How do I make a static control with custom width and color?
body: <p>To change the static's control color and width, I did process the <code>WM_NCPAINT</code> message, but it's not working properly:</p>
  <ol>
  <li><p>the text isn't properly displayed. Should I draw it in this message? When I call <a href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowtextw" rel="nofollow noreferrer"><code>SetWindowText()</code></a>, the new text is properly set, but I still have the issue #2.</p>
  </li>
  <li><p>after a call to <a href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowtextw" rel="nofollow noreferrer"><code>SetWindowText()</code></a>, the text is set, but the border differs, it's smaller than the one I've draw in the <code>WM_NCPAINT</code> message.</p>
  </li>
  </ol>
  <p>Here's how I'm handling <code>WM_NCPAINT</code>:</p>
  <pre class="lang-c prettyprint-override"><code>case WM_NCPAINT:
  {
      PAINTSTRUCT ps;
      HDC dc = BeginPaint(hwnd, &amp;ps);
      RECT rt = {0};
      GetClientRect(hwnd, &amp;rt);
      HPEN pen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
      HPEN holdPen = SelectObject(dc, pen);
      DrawBorder(dc, &amp;rt);
      SelectObject(dc, holdPen);
      EndPaint(hwnd, &amp;ps);
      DeleteObject(pen);
      pen = NULL;
      return 0;
  }
  </code></pre>
  <p>When the application starts, the static control border looks like the one I've draw at <code>WM_NCPAINT</code>:</p>
  <p><a href="https://i.stack.imgur.com/sn7bs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sn7bs.png" alt="image" /></a></p>
  <p>But once I call <code>SetWindowText()</code>, for example, from a button click, it looks like this:</p>
  <p><a href="https://i.stack.imgur.com/0ykRo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0ykRo.png" alt="image" /></a></p>
  <p>What am I missing?</p>
  <p>Here's full code:</p>
  <pre class="lang-c prettyprint-override"><code>#pragma comment(lib, &quot;user32.lib&quot;)
  #pragma comment(lib, &quot;Comctl32.lib&quot;)
  #pragma comment(lib, &quot;Gdi32.lib&quot;)
      
  #define WIN32_LEAN_AND_MEAN
  #define UNICODE
  #define _UNICODE
      
  #include &lt;windows.h&gt;
  #include &lt;Commctrl.h&gt;
  #include &lt;crtdbg.h&gt;
  #include &lt;strsafe.h&gt;
  #include &lt;string.h&gt;
  #include &lt;assert.h&gt;
      
  LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  void EnableVisualStyles2(void);
  LRESULT CALLBACK ButtonProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  void SetDefaultFont(HWND hwnd);
  void DrawLine(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2);
  void DrawBorder(HDC hdc, RECT *rect);
  HFONT getSystemDefaultFont(void);
      
  WNDPROC oldButtonProc;
  HINSTANCE ghInstance;
  HFONT hDefaultFont;
  HWND btn, btn2;
      
  enum 
  {
      BTN_ID = 10,
      BTN2_ID,
  };
      
  int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                      PWSTR pCmdLine, int nCmdShow)
  {
      MSG  msg = {0};
      HWND hwnd;
      WNDCLASSW wc = {0};
  
      wc.lpszClassName = L&quot;Window&quot;;
      wc.hInstance     = hInstance;
      wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
      wc.lpfnWndProc   = WndProc;
      wc.hCursor = LoadCursor(0, IDC_ARROW);
  
      EnableVisualStyles2();
      if(!RegisterClass(&amp;wc)) {
          return -1;
      }
  
      int width = 540;
      int height = 460;
      int screenWidth = GetSystemMetrics(SM_CXSCREEN);
      int screenHeight = GetSystemMetrics(SM_CYSCREEN);
      int cx = (screenWidth - width) / 2;
      int cy = (screenHeight - height) / 2;
      hwnd = CreateWindowW(wc.lpszClassName, L&quot;Window&quot;,
                          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                          cx, cy, width, height, NULL, NULL, 
                          hInstance, NULL);
      ghInstance = hInstance;
  
      while (GetMessage(&amp;msg, NULL, 0, 0))
      {
          if (!IsDialogMessage(hwnd, &amp;msg))
          {
              TranslateMessage(&amp;msg);
              DispatchMessage(&amp;msg);
          }
      }
  
      DeleteObject(hDefaultFont);
      return (int) msg.wParam;
  }
      
  LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
      switch(msg)
      {
          case WM_CREATE:
          {
              btn =
                  CreateWindow(L&quot;static&quot;, L&quot;+&quot;,
                      WS_VISIBLE | WS_CHILD | WS_TABSTOP |
                      SS_NOTIFY | SS_CENTER | SS_CENTERIMAGE,
                      15, 25, 50, 50,
                      hwnd, (HMENU) BTN_ID, NULL, NULL);
              SetDefaultFont(btn);
              oldButtonProc = (WNDPROC) SetWindowLongPtr(btn,
                                          GWLP_WNDPROC,
                                          (LONG_PTR) ButtonProc);
              btn2 = 
                  CreateWindow(L&quot;Button&quot;, L&quot;Click me!&quot;,
                              WS_VISIBLE | WS_CHILD | WS_TABSTOP,
                              15, 100, 70, 25,
                              hwnd, (HMENU) BTN2_ID,
                              NULL, NULL);
              SetDefaultFont(btn2);
          }
          break;
      
          case WM_COMMAND:
          {
              switch(LOWORD(wParam))
              {
                  case BTN_ID:
                  {
                      //InvalidateRect(btn, NULL, TRUE);
                      
                      if(IsWindowVisible(btn2))
                      {
                          SetWindowText(btn, L&quot;+&quot;);
                          ShowWindow(btn2, SW_HIDE);
                      }
                      else
                      {
                          SetWindowText(btn, L&quot;-&quot;);
                          ShowWindow(btn2, SW_SHOW);
                      }
                  }
                  break;
      
                  case BTN2_ID:
                  {
                      InvalidateRect(btn, NULL, TRUE);
                      //SetWindowText(btn, L&quot;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0&quot;);
                  }
                  break;
              }
          }
          break;    
          
          case WM_DESTROY:
              PostQuitMessage(0);
              return 0;
      }
      
      return DefWindowProc(hwnd, msg, wParam, lParam);
  }
      
  LRESULT CALLBACK ButtonProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
      switch(msg)
      {
          case WM_NCPAINT:
          {
              PAINTSTRUCT ps;
              HDC dc = BeginPaint(hwnd, &amp;ps);
              RECT rt = {0};
              GetClientRect(hwnd, &amp;rt);
              HPEN pen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
              HPEN holdPen = SelectObject(dc, pen);
              DrawBorder(dc, &amp;rt);
              SelectObject(dc, holdPen);
              EndPaint(hwnd, &amp;ps);
              DeleteObject(pen);
              pen = NULL;
              return 0;
          }
      }
      
      return CallWindowProc(oldButtonProc, hwnd, msg, wParam, lParam);
  }
      
  void EnableVisualStyles2(void)
  {
      TCHAR dir[MAX_PATH] = {0};
      GetSystemDirectory(dir, sizeof(dir) / sizeof(*dir));
      
      ACTCTX actCtx = {0};
      actCtx.cbSize = sizeof(ACTCTX);
      actCtx.dwFlags =  ACTCTX_FLAG_RESOURCE_NAME_VALID |
                        ACTCTX_FLAG_SET_PROCESS_DEFAULT |
                        ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;
      actCtx.lpSource = L&quot;shell32.dll&quot;;
      actCtx.lpAssemblyDirectory = dir;
      actCtx.lpResourceName = (LPCTSTR) 124;
      ULONG_PTR cookie = FALSE;
      HANDLE h = CreateActCtx(&amp;actCtx);
      assert(h != INVALID_HANDLE_VALUE);
      assert(ActivateActCtx(h, &amp;cookie));
  }
      
  HFONT getSystemDefaultFont(void)
  {
      if(!hDefaultFont)
      {
          // get system default font
          NONCLIENTMETRICS ncm;
          ncm.cbSize = sizeof(NONCLIENTMETRICS);
          SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &amp;ncm, 0);
          hDefaultFont = CreateFontIndirect(&amp;ncm.lfMessageFont);
      }
      return hDefaultFont;
  }
      
  void SetDefaultFont(HWND hwnd)
  {
      SendMessage(hwnd, WM_SETFONT, (WPARAM)getSystemDefaultFont(), FALSE);
  }
      
  void DrawLine(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2)
  {
      MoveToEx(hdc, x1, y1, NULL);
      LineTo(hdc, x2, y2);
  }
      
  void DrawBorder(HDC hdc, RECT *rect)
  {
      DrawLine(hdc, rect-&gt;left, rect-&gt;top, rect-&gt;left, rect-&gt;bottom);
      DrawLine(hdc, rect-&gt;left, rect-&gt;top, rect-&gt;right, rect-&gt;top);
      DrawLine(hdc, rect-&gt;right, rect-&gt;top, rect-&gt;right, rect-&gt;bottom);
      DrawLine(hdc, rect-&gt;left, rect-&gt;bottom, rect-&gt;right, rect-&gt;bottom);
  }
  </code></pre>