How can I simplify code that modifies Window Styles using SetWindowLong & GetWindowLong? -


i'm writing wrapper functions , classes windows api. common occurrence i'm starting come across modifying windows styles.

the following example code adjusts textbox's text alignment based on parameter alignment enum. after testing seems must remove 2 alternative alignment styles or conflict. setwindowpos(..swp_framechanged) doesn't work either, replaced invalidaterect() , updatewindow() force textbox repainted after style updated.

i'd feedback if there simpler way of doing this. feel 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:

#define es_left             0x0000l #define es_center           0x0001l #define es_right            0x0002l 

so can do

void textbox::alignment(alignment alignment) {     int style = es_left; // default left alignment     switch (alignment)     {         case alignment::center:             style = es_center;             break;         case alignment::right:             style = es_right;             break;     }      setwindowlongptr(m_hwnd, gwl_style, getwindowlongptr(m_hwnd, gwl_style) & ~(es_center|es_right) | style);     invalidaterect(m_hwnd, null, true);     updatewindow(m_hwnd); }; 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -