{"id":87,"date":"2010-08-20T14:51:11","date_gmt":"2010-08-20T09:21:11","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=87"},"modified":"2011-01-26T14:51:49","modified_gmt":"2011-01-26T09:21:49","slug":"edit-boxes-and-default-text-selection","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2010\/08\/20\/edit-boxes-and-default-text-selection\/","title":{"rendered":"Edit boxes and default text selection"},"content":{"rendered":"<p align=\"justify\">While working on an application of mine, I had to display &lsquo;Help&rsquo; information. I chose to use a read-only multiline edit box along with <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa922540.aspx\">SetWindowText()<\/a> to display the text, sounds simple enough. But I ran into a small but annoying problem. The text displayed was all selected, and I thought why would the text be selected by default!?<\/p>\n<p align=\"justify\">Here is a sample program to demonstrate this,<\/p>\n<div class=\"le-cah-container\">\n<div style=\"background: none repeat scroll 0% 0% rgb(221, 221, 221); overflow: auto;\">\n<ol style=\"padding: 0px 0px 0px 5px; margin: 0px 0px 0px 3em; background: none repeat scroll 0% 0% rgb(255, 255, 255);\">\n<li><span style=\"color: rgb(0, 0, 255);\">#include<\/span> <span style=\"color: rgb(163, 21, 21);\">&lt;windows.h&gt;<\/span><\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(0, 0, 255);\">#include<\/span> <span style=\"color: rgb(163, 21, 21);\">&lt;aygshell.h&gt;<\/span><\/li>\n<li><span style=\"color: rgb(0, 0, 255);\">#include<\/span> <span style=\"color: rgb(163, 21, 21);\">&quot;resource.h&quot;<\/span><\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;<\/li>\n<li>HINSTANCE g_hInst;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;<\/li>\n<li>BOOL CALLBACK EditBoxSelTextDlgProc(HWND, UINT, WPARAM, LPARAM);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(0, 0, 255);\">void<\/span> AlignComponents(HWND hDlg);<\/li>\n<li><span style=\"color: rgb(0, 0, 255);\">void<\/span> InitializeComponents(HWND hDlg);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;<\/li>\n<li><span style=\"color: rgb(0, 0, 255);\">int<\/span> WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LPTSTR lpCmdLine, <span style=\"color: rgb(0, 0, 255);\">int<\/span> nCmdShow)<\/li>\n<li>{<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; g_hInst = hInst;<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; DialogBox(hInst, MAKEINTRESOURCE(IDD_PPC_EDITBOXTEXTSEL), NULL, EditBoxSelTextDlgProc);<\/li>\n<li>&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">return<\/span> 0;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">}<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">BOOL CALLBACK EditBoxSelTextDlgProc(HWND hDlg, UINT uMessage, WPARAM wParam, LPARAM lParam)<\/li>\n<li>{<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">int<\/span> wmID, wmEvent;<\/li>\n<li>&nbsp;&nbsp;&nbsp; PAINTSTRUCT ps;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; HDC hdc;<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">switch<\/span>(uMessage)<\/li>\n<li>&nbsp;&nbsp;&nbsp; {<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">case<\/span> WM_INITDIALOG:<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SHINITDLGINFO shidi;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SHMENUBARINFO mbi;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; memset(&amp;shidi, 0, <span style=\"color: rgb(0, 0, 255);\">sizeof<\/span>(shidi));<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; memset(&amp;mbi, 0, <span style=\"color: rgb(0, 0, 255);\">sizeof<\/span>(mbi));<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shidi.dwMask = SHIDIM_FLAGS;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shidi.hDlg = hDlg;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SHInitDialog(&amp;shidi);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbi.cbSize = <span style=\"color: rgb(0, 0, 255);\">sizeof<\/span>(mbi);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbi.hwndParent = hDlg;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbi.nToolBarId = IDR_MENU_EDITBOXTEXTSEL;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbi.hInstRes = g_hInst;<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">if<\/span>(!SHCreateMenuBar(&amp;mbi))<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(<span style=\"color: rgb(163, 21, 21);\">&quot;Error creating menu bar, errcode:0x%x\\n&quot;<\/span>, GetLastError());<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AlignComponents(hDlg);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InitializeComponents(hDlg);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">return<\/span> TRUE;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">case<\/span> WM_COMMAND:<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wmID = LOWORD(wParam);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wmEvent = HIWORD(wParam);<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">switch<\/span>(wmID)<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">case<\/span> IDM_EXIT:<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EndDialog(hDlg, uMessage);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">break<\/span>;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">break<\/span>;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">case<\/span> WM_PAINT:<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hdc = BeginPaint(hDlg, &amp;ps);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EndPaint(hDlg, &amp;ps);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">break<\/span>;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; }<\/li>\n<li>&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">return<\/span> FALSE;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">}<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(0, 0, 255);\">void<\/span> AlignComponents(HWND hDlg)<\/li>\n<li>{<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; HWND hTemp = NULL;<\/li>\n<li>&nbsp;&nbsp;&nbsp; RECT rect = {0, 0, 0, 0};<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">int<\/span> x=0, y=0, width=0, height=0;<\/li>\n<li>&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">const<\/span> <span style=\"color: rgb(0, 0, 255);\">int<\/span> insetX = 3;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">const<\/span> <span style=\"color: rgb(0, 0, 255);\">int<\/span> insetY = 3;<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; GetClientRect(hDlg, &amp;rect);<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; hTemp = GetDlgItem(hDlg, IDC_EDITBOX);<\/li>\n<li>&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">if<\/span> (hTemp)<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; {<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = rect.left + insetX;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y = rect.top + insetY;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width = (rect.right &#8211; rect.left &#8211; 2*insetX);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; height = (rect.bottom &#8211; rect.top &#8211; 2*insetY);<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MoveWindow(hTemp, x, y, width, height, FALSE);<\/li>\n<li>&nbsp;&nbsp;&nbsp; }<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">}<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(0, 0, 255);\">const<\/span> TCHAR message[] = TEXT(<span style=\"color: rgb(163, 21, 21);\">&quot;Lorem ipsum dolor sit amet, consectetur adipisicing elit, \\<\/span><\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(163, 21, 21);\">sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \\<\/span><\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(163, 21, 21);\">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris \\<\/span><\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(163, 21, 21);\">nisi ut aliquip ex ea commodo consequat.\\<\/span><\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(163, 21, 21);\">\\r\\n\\r\\n\\<\/span><\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(163, 21, 21);\">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum \\<\/span><\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(163, 21, 21);\">dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non \\<\/span><\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(163, 21, 21);\">proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\\<\/span><\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(163, 21, 21);\">&quot;<\/span>);<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\"><span style=\"color: rgb(0, 0, 255);\">void<\/span> InitializeComponents(HWND hDlg)<\/li>\n<li>{<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; HWND hTemp = GetDlgItem(hDlg, IDC_EDITBOX);<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">if<\/span> (hTemp)<\/li>\n<li>&nbsp;&nbsp;&nbsp; {<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetWindowText(hTemp, message);<\/li>\n<li>&nbsp;&nbsp;&nbsp; }<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p align=\"justify\">This is usual stuff. Display a dialog box from <font color=\"#000080\">WinMain()<\/font>, initialize the dialog and the menubar in dialog proc under the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa931243.aspx\">WM_INITDIALOG<\/a> message. The functions of interest would be <font color=\"#000080\">AlignComponents()<\/font> and <font color=\"#000080\">InitializeComponents()<\/font>. The dialog contains a single multiline edit box, <font color=\"#000080\">AlignComponents()<\/font> resizes the edit box so it occupies the entire screen, and <font color=\"#000080\">InitializeComponents()<\/font> calls <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa922540.aspx\">SetWindowText()<\/a> to set the text on the edit box control.<\/p>\n<p align=\"justify\">If you run this program, here is how it&rsquo;ll look,<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"307\" height=\"470\" border=\"0\" src=\"http:\/\/geekswithblogs.net\/images\/geekswithblogs_net\/TechTwaddle\/WindowsLiveWriter\/Editboxesandthedefaulttextselection_10F44\/image_8c9cbb0e-a7eb-4762-af87-5fc13a1b4875.png\" alt=\"image\" title=\"image\" style=\"border: 0px none; display: inline;\" \/><\/p>\n<p align=\"justify\">So you see the problem there. Skimming through the edit box messages, I came across <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa932540.aspx\">EM_SETSEL<\/a> message. This message selects a range of characters in an edit control, and if you pass &ndash;1 to it, any current selection is removed. So after the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa922540.aspx\">SetWindowText()<\/a> call in <font color=\"#000080\">InitializeComponents()<\/font>, I put this line,<\/p>\n<p>SendMessage(hTemp, EM_SETSEL, (WPARAM)-1, (LPARAM)0);<\/p>\n<p align=\"justify\">but if it was this simple I wouldn&rsquo;t be writing this post, would I? In short, it didn&rsquo;t work.<\/p>\n<p align=\"justify\">I thought maybe I am sending the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa932540.aspx\">EM_SETSEL<\/a> message too early, even before the edit control is ready to process it perhaps? So the next step was to find the right place from where the message could be sent. So, one by one, I went through all the messages sent to dialog proc until the dialog is shown and found <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa922525.aspx\">WM_ACTIVATE<\/a> could be the right place. And it indeed was.<\/p>\n<p align=\"justify\">Adding the following switch case to dialog proc did the trick,<\/p>\n<div class=\"le-cah-container\">\n<div style=\"background: none repeat scroll 0% 0% rgb(221, 221, 221); overflow: auto;\">\n<ol style=\"padding: 0px 0px 0px 5px; margin: 0px 0px 0px 2em; background: none repeat scroll 0% 0% rgb(255, 255, 255);\">\n<li><span style=\"color: rgb(0, 0, 255);\">case<\/span> WM_ACTIVATE:<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; SendMessage(GetDlgItem(hDlg, IDC_EDITBOX), EM_SETSEL,<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (WPARAM)-1, (LPARAM)0);<\/li>\n<li style=\"background: none repeat scroll 0% 0% rgb(243, 243, 243);\">&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(0, 0, 255);\">break<\/span>;<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p align=\"justify\">The edit control now shows up without any selected text.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"304\" height=\"475\" border=\"0\" src=\"http:\/\/geekswithblogs.net\/images\/geekswithblogs_net\/TechTwaddle\/WindowsLiveWriter\/Editboxesandthedefaulttextselection_10F44\/image_5b6ac149-6ef8-429c-8947-99063f68f704.png\" alt=\"image\" title=\"image\" style=\"border: 0px none; display: inline;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working on an application of mine, I had to display &lsquo;Help&rsquo; information. I chose to use a read-only multiline edit box along with SetWindowText() to display the text, sounds simple enough. But I ran into a small but annoying problem. The text displayed was all selected, and I thought why would the text be &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2010\/08\/20\/edit-boxes-and-default-text-selection\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Edit boxes and default text selection<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-1p","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/87"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":1,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":88,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions\/88"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}