{"id":1954,"date":"2013-10-18T08:24:42","date_gmt":"2013-10-17T20:24:42","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=1954"},"modified":"2013-10-18T11:17:00","modified_gmt":"2013-10-17T23:17:00","slug":"hands-off-my-application-another-dpr-trick","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/1954\/","title":{"rendered":"Hands Off My Application! (Another DPR Trick)"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span><p>In a comment on my previous post, <a href=\"https:\/\/www.deltics.co.nz\/blog\/posts\/1945\/comment-page-1#comment-15082\">David Heffernan noted that the IDE messes around with other parts of the application DPR<\/a> in addition to the uses list.  He is right.  Another area that the IDE likes to muck about with is the code in the DPR <strong>begin .. end<\/strong> itself !<\/p>\n<p>Fortunately there is a simple way to avoid that also.<\/p>\n<p><!--more--><\/p>\n<h2>First, Recognise the Problem<\/h2>\n<p>Let&#8217;s see just one of the things that IDE does in this area (version used for this exercise: Delphi 2010.  Steps may vary slightly in any different version but should be easy enough to follow).<\/p>\n<p>Create a new VCL Forms application and View Project Source (the DPR).  You should see something like this:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nprogram Project1;\r\n\r\nuses\r\n  Forms,\r\n  Unit1 in 'Unit1.pas' {Form1};\r\n\r\n{$R *.res}\r\n\r\nbegin\r\n  Application.Initialize;\r\n  Application.MainFormOnTaskbar := True;\r\n  Application.CreateForm(TForm1, Form1);\r\n  Application.Run;\r\nend.\r\n<\/pre>\n<p>Now open the <strong>Project Options<\/strong> dialog, select <strong>Application<\/strong> settings and enter something as the <strong>Application Title<\/strong>:<\/p>\n<figure id=\"attachment_1955\" aria-describedby=\"caption-attachment-1955\" style=\"width: 866px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-18-at-09.01.47.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-18-at-09.01.47.png?resize=640%2C204&#038;ssl=1\" alt=\"Setting the Application Title\" width=\"640\" height=\"204\" class=\"size-full wp-image-1955\" srcset=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-18-at-09.01.47.png?w=866&amp;ssl=1 866w, https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-18-at-09.01.47.png?resize=300%2C95&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-18-at-09.01.47.png?resize=500%2C159&amp;ssl=1 500w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-1955\" class=\"wp-caption-text\">Setting the Application Title<\/figcaption><\/figure>\n<p>Press OK<\/p>\n<p>Before your very eyes the project DPR will change to:<\/p>\n<pre class=\"brush: delphi; highlight: [12]; title: ; notranslate\" title=\"\">\r\nprogram Project1;\r\n\r\nuses\r\n  Forms,\r\n  Unit1 in 'Unit1.pas' {Form1};\r\n\r\n{$R *.res}\r\n\r\nbegin\r\n  Application.Initialize;\r\n  Application.MainFormOnTaskbar := True;\r\n  Application.Title := 'Right Honourable Application';\r\n  Application.CreateForm(TForm1, Form1);\r\n  Application.Run;\r\nend.\r\n<\/pre>\n<p>This is just the first example of where the IDE re-writes your DPR code.  There are others (changing the Main Form and\/or the list of auto-created forms, for example), but many if not all of them can be defeated by one simple trick.  A clue to that trick can be found by removing the line from the DPR that sets <strong>Application.Title<\/strong> and going back to the <strong>Project Options<\/strong>.<\/p>\n<p><em>The <strong>Application Title<\/strong> is no longer set!<\/em><\/p>\n<p>Not only does the IDE apply these settings by re-writing your DPR code, it actually seems to get the current setting by reading the code too !<\/p>\n<p>For these features, the IDE relies on being able to recognise certain things in the code of your DPR, driven by the &#8220;boiler-plate&#8221; code created by the project wizards that it expects to find.  In this case, code that uses the <strong>Application<\/strong> variable (from the Forms unit).<\/p>\n<h2>If You Can&#8217;t Beat Them, Hide<\/h2>\n<p>Obviously you can put your own code in this <strong>begin\/end<\/strong> area.  Very often you have to, to perform initialisation or checks before launching into your UI.<\/p>\n<p>But precisely because you <em>can<\/em> add code the IDE does do it&#8217;s best to identify only those parts in that code which it feels it <em>should<\/em> be able to modify.  If the IDE cannot find the required &#8220;markers&#8221; that identify the pieces that the IDE wishes to modify then rather than risk doing damage it simply keeps itself to itself.<\/p>\n<p>So all we have to do is remove those markers.<\/p>\n<p>We can do this very simply by declaring our own variable to use in place of <strong>Application<\/strong>:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nvar\r\n  app: TApplication;\r\nbegin\r\n  app := Application;\r\n\r\n  app.Initialize;\r\n  app.MainFormOnTaskbar := True;\r\n  app.CreateForm(TForm1, Form1);\r\n  app.Run;\r\nend.\r\n<\/pre>\n<p>Once you&#8217;ve done this I don&#8217;t think the IDE will ever trouble your DPR again, at least not the parts between the <strong>begin<\/strong> and <strong>end<\/strong>.  Which is to say that in almost 20 years of Delphi, after making such a change to the DPR I don&#8217;t recall a single instance of the IDE ever trying to make unwelcome changes in this area.<\/p>\n<p>It does mean that you have to avoid using certain IDE features.<\/p>\n<p>They either simply won&#8217;t work or will complain.  Try changing the application title now and Delphi 2010 will complain that it cannot find a valid &#8220;<em>Application.CreateForm<\/em>&#8220;.  But more importantly, <em>it won&#8217;t try to make any changes in the DPR either<\/em>!<\/p>\n<p>As I mentioned, the other key area that this &#8220;breaks&#8221; is the <strong>Main Form<\/strong> and <strong>Auto-Create Forms list<\/strong> in the project options.<\/p>\n<h2>With Great Power Comes Great Responsibility<\/h2>\n<p>By denying the IDE the capability to manage your DPR code you have to take on the responsibility of managing things yourself.  But then, since the whole point is to avoid problems that the IDE management of this code can cause, you wouldn&#8217;t have wanted to continue using them anyway.  \ud83d\ude42<\/p>\n<p>In applications where you don&#8217;t need any bespoke changes in the DPR code you can of course just leave things as they are.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span>In a comment on my previous post, David Heffernan noted that the IDE messes around with other parts of the application DPR in addition to the uses list. He is right. Another area that the IDE likes to muck about with is the code in the DPR begin .. end itself ! Fortunately there is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4],"tags":[292,246],"class_list":["post-1954","post","type-post","status-publish","format-standard","hentry","category-delphi","tag-delphi","tag-tips-and-tricks"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-vw","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=1954"}],"version-history":[{"count":7,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1954\/revisions"}],"predecessor-version":[{"id":1963,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1954\/revisions\/1963"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=1954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=1954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=1954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}