{"id":18173,"date":"2009-05-25T10:00:00","date_gmt":"2009-05-25T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2009\/05\/25\/why-cant-you-change-the-alignment-of-an-edit-control-after-it-has-been-created\/"},"modified":"2009-05-25T10:00:00","modified_gmt":"2009-05-25T10:00:00","slug":"why-cant-you-change-the-alignment-of-an-edit-control-after-it-has-been-created","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20090525-00\/?p=18173","title":{"rendered":"Why can&#8217;t you change the alignment of an edit control after it has been created?"},"content":{"rendered":"<p>Commenter Kevin Eshbach asks <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/pages\/407234.aspx#569387\"> why you cannot programatically change the text centering style of an edit control after the control has been created<\/a>.<\/p>\n<p> The edit control is one of the original window manager controls, so it comes with a lot of history. First thing to note is that the <code>WM_STYLECHANGED<\/code> message did not exist until Windows&nbsp;95. The edit control predates Windows&nbsp;95 by over a decade, so the short answer is &#8220;It can&#8217;t respond to the change in styles because it doesn&#8217;t even know that you changed them!&#8221; <\/p>\n<p> Back in the old days, the only time a control was informed of its styles was in the <code>CREATESTRUCT<\/code> passed as parameters to the <code>WM_CREATE<\/code> and <code>WM_NCCREATE<\/code> messages. Consequently, most controls back in those days cached their styles in member variables so that they wouldn&#8217;t have to call <code>GetWindowLong(GWL_STYLE)<\/code> all the time. This was back in the days of 4.77MHz CPUs, and a call to fetch the style of a window was quite a bit bigger and slower than just reading it out of your local data segment. <\/p>\n<p> Even if you decided &#8220;Forget it, I won&#8217;t bother with a cache; I&#8217;ll just call <code>GetWindowLong(GWL_STYLE)<\/code> to read the style each time I need it&#8221;, you open yourself up to a new problem: What if the style changes? A change in the style means that the characters in the edit box need to have their positions recalculated and the text redrawn. This is obviously not something you want to do at every single message (redrawing at every message is probably not a great idea), so you still need to cache the old style so you can avoid recalculating stuff that hasn&#8217;t changed. <\/p>\n<p> Even if you say, &#8220;Okay, fine, then instead of changing the style with <code>SetWindowLong(GWL_STYLE)<\/code>, let&#8217;s say that if you want to change the style of an edit control, you have to use a new message, something like <code>EM_CHANGESTYLE<\/code>.&#8221; That way, the edit control knows when its styles have changed and doesn&#8217;t have to keep polling for them. When it gets this message, it updates the styles and recalculates all its internal state based on the new style. <\/p>\n<p> Okay, sure it could&#8217;ve done this, but that&#8217;s code that <a href=\"http:\/\/blog.ryjones.org\/2005\/07\/12\/product-development\/\"> has to be written and tested<\/a>, for an extremely rare usage pattern. I mean, what is the scenario where somebody needs to change an edit control from left-aligned to right-aligned on the fly? You could count all the Windows&nbsp;1.0 applications on your fingers and toes (note: exaggeration), and they just decided in the dialog template which way the text should be aligned. It was determined not just at creation time; it was determined at <i>compile time<\/i>. <\/p>\n<p> If you want to change an edit control&#8217;s alignment on the fly, you can just destroy the old one and create a new one in its place with the same text. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Commenter Kevin Eshbach asks why you cannot programatically change the text centering style of an edit control after the control has been created. The edit control is one of the original window manager controls, so it comes with a lot of history. First thing to note is that the WM_STYLECHANGED message did not exist until [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-18173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Commenter Kevin Eshbach asks why you cannot programatically change the text centering style of an edit control after the control has been created. The edit control is one of the original window manager controls, so it comes with a lot of history. First thing to note is that the WM_STYLECHANGED message did not exist until [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/18173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=18173"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/18173\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=18173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=18173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=18173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}