I'm writing some wrapper functions and classes for the Windows API. A common occurrence I'm starting to come across is modifying Windows Styles.
The following is some example code that my adjusts a TextBox's Text Alignment based on the parameter ALIGNMENT enum. After much testing it seems I must remove the 2 alternative Alignment styles or it will conflict. Then SetWindowPos(..SWP_FRAMECHANGED) doesn't work either, so I replaced it with InvalidateRect() and UpdateWindow() to force the TextBox to be repainted after the style is updated.
I'd like some feedback if there is a simpler way of doing this. I feel like I'm overlooking something. Thanks! :)
enum ALIGNMENT
{
Left,
Right,
Center
};
void TextBox::Alignment(ALIGNMENT Alignment)
{
switch (Alignment)
{
case ALIGNMENT::Left:
SetWindowLongPtr(m_hWnd, GWL_STYLE, (GetWindowLongPtr(m_hWnd, GWL_STYLE) & ~ES_CENTER & ~ES_RIGHT) | ES_LEFT);
break;
case ALIGNMENT::Center:
SetWindowLongPtr(m_hWnd, GWL_STYLE, (GetWindowLongPtr(m_hWnd, GWL_STYLE) & ~ES_LEFT & ~ES_RIGHT) | ES_CENTER);
break;
case ALIGNMENT::Right:
SetWindowLongPtr(m_hWnd, GWL_STYLE, (GetWindowLongPtr(m_hWnd, GWL_STYLE) & ~ES_LEFT & ~ES_CENTER) | ES_RIGHT);
break;
}
InvalidateRect(m_hWnd, NULL, true);
UpdateWindow(m_hWnd);
};
In WinUser.h:
so you can do