Friday, March 20, 2020

Handling Windows Messages the Delphi way

Handling Windows Messages the Delphi way Delphi, youve got message to handle! One of the keys to traditional Windows programming is handling the messages sent by Windows to applications. Simply put, a message is some information sent from one place to another. For the most part, Delphi makes message handling easy through its use of events, an event is usually generated in response to a Windows message being sent to an application. However, someday you may want to process some uncommon messages like: CM_MOUSEENTER which happens (is posted by Windows) when mouse cursor enters the client area of some component (or form). Handling messages on their own requires a few extra programming techniques, this article is here to help us find the right way through the message river and grap needed information. Strategies to Manipulate Windows Messages With Delphi Drag a Window: No title bar! How can you drag such a window? Its easy and fun: lets make a Delphi form move by clicking (and dragging) in its client area. The main idea is to get your hands on the wm_NCHitTest windows message.How to send information (String, Image, Record) between two Delphi applications (WM_CopyData): Learn how to send the WM_CopyData message between two Delphi applications to exchange information and make two applications communicate. The accompanying source code demonstrates how to send a string, record (complex data type) and even graphics to another application.Sticky Windows: This strategy allows you to dock your Delphi forms to the edges of your desktop screen.Monitoring Registry Changes: Need to get notified about changes to the attributes or contents of a specified Registry key? Then you are ready for this your Delphi code toolkit.Sending Messages to Non-Windowed Applications: This strategy is used to send messages (signals) to non-windowed applications by u sing AllocateHWND and DefWindowProc. You should understand what Delphi does in the background to intercept Windows messages, how can you write your own message handler for a windowed application and how to obtain a unique message identifier that you can safely use in your applications. There is also a small bug in the Delphi DeallocateHWND procedure that you can fix along the way. Controlling the Number of Application Instances: In this article youll learn how to run-once enable a Delphi application that can check for its previous (running) instance. Along the process, several techniques of implementing such a check will be discussed; as well as how to bring your already running application to the foreground, if a user tries to run it one more time. By the end of the article youll have a copy-to-go code to control the behavior of your applications multiple instances: with the option to limit the number of running instances.How to Handle System Time Change Using Delphi Code: If you need to react when system date time has changed you can handle the WM_TimeChange Windows message.How to Draw Custom Text on a Delphi Forms Caption Bar: If you want to add some custom text on the caption bar of a form, without changing the Caption property of the form you need to handle one special Windows message: WM_NCPAINT (along with WM_NCACTIVATE).How to Display Menu Item Hints: By (Windows) design, in Delphi applications, hints assigned to menu items do not get displayed in the popup tooltip window (when the mouse hovers over a menu). Get, Set, and Handle Display Device Modes (Screen Resolution and Color Depth): This strategy allows you to change the Windows display mode settings (resolution and color depth) from Delphi code. You can also handle the WM_DISPLAYCHANGE Windows message sent to all windows when the display resolution has changed.Get Current URL From IE: There is a Delphi tactic to retrieve the full URL of all opened Internet Explorer instances.Detecting and Preventing Windows Shut Down: You can use Delphi to programmatically cancel Windows shut down action.Display a Password Dialog: Suppose you have a data-critical type of application where you would not want a non-authored user to work with the data. What if you need to display a password dialog *before* the application is restored to make sure an authorized user is accessing it.Remove the Windows Constraint on Minimum Form Size: By Windows design, a form (window) has a size constraint that sets the minimum form height to the height of the caption bar and the width to 112 pixels (118 in XP theme). How to Detect a TPopupMenus OnClose (OnPopDown) Event: Unfortunately, the TPopupMenu does not expose an event you can handle that will fire when the menu gets closed - either after a user has selected an item from the menu or has activated some other UI element.Trapping Messages Sent to an Application: ...Delphi surfaces the OnMessage event for the Application object. The OnMessage event handler is supposed to allow you trap every message sent to your application...

Wednesday, March 4, 2020

Aftershocks Are Not Afterthoughts

Aftershocks Are Not Afterthoughts Aftershocks, those who live through major earthquakes often say, are worse than the main shock in their own way. At least the main shock took them by surprise and was over fairly soon, in less than a minute usually. But with aftershocks, people are stressed already, dealing with disrupted lives and cities. They expect aftershocks at any minute, day or night. When a building is damaged by the main shock, aftershocks can take it down- maybe when youre inside cleaning it up. No wonder Susan Hough, the government seismologist who gets in the news whenever temblors do, calls aftershocks ghosts of earthquakes past. The Duration of Aftershocks I can show you some aftershocks right now: just look at the map of recent earthquakes for the San Simeon area of California. In any given week, there are aftershocks there from the 2003 San Simeon earthquake. And east of Barstow  you can still see a trickle of aftershocks from the October 1999 Hector Mine earthquake. Indeed, some scientists argue that aftershocks may last for centuries in places, like continental interiors, where plate motions that build up stresses in the crust are very slow. This makes intuitive sense, but careful studies using long historical catalogs will need to be done. The Trouble with Aftershocks Two things about aftershocks make them troublesome. First, they arent restricted to the spot where the main shock occurred, but can strike tens of kilometers away- and, say, if a magnitude 7 quake was centered out beyond the suburbs but one of its magnitude 5 aftershocks happened right underneath City Hall, the littler one might be the worse of the two. This was the case with the Christchurch, New Zealand earthquake of September 2010 and its large aftershock five months later. Second, aftershocks dont necessarily get smaller as time passes. They get fewer, but sizable ones can happen long after most of the little ones have ended. In Southern California, this phenomenon aroused so much concern after the Northridge quake of 17 January 1994 that Hough wrote an op-ed piece for the Los Angeles Times on the subject three full years later. Scientific Uses of Aftershocks Aftershocks are scientifically interesting because they are good ways to map the underground fault zone that ruptured in the main shock. (Heres how they look for the cases of Northridge.) In the case of the 28 September 2004 Parkfield quake, you can see that the first hour of aftershocks alone outlines the ruptured zone quite well. Aftershocks are also interesting because theyre fairly well behaved- meaning that they have a detectable pattern, unlike all other quakes. The definition that scientists use for an aftershock is any seismic event occurring within one rupture-zone length of a main shock and within the time it takes for seismicity to fall off to what it was before the main shock. This body of quakes fits three mathematical rules, more or less. The first is the Gutenberg-Richter relation, which says that as you go down one magnitude unit in size, aftershocks increase in number by about ten times. The second is called Baths law, which says that the largest aftershock is, on average, 1.2 magnitude units smaller than the main shock. And finally, Omoris law states that aftershock frequency decreases by roughly the reciprocal of time after the main shock. These numbers differ a bit in different active regions depending on their geology, but theyre close enough for government work as the saying goes. So seismologists can advise the authorities immediately after a large earthquake that a certain area can expect X probabilities of aftershocks of Y sizes for Z period of time. The U.S. Geological Surveys STEP project produces a daily map of California with the current risk of strong aftershocks for the next 24 hours. Thats as good a forecast as we can make, and probably the best possible given that earthquakes are inherently unpredictable. Aftershocks in the Quiet Zones Still to be determined is how much Omoris law varies beyond active tectonic settings. Large earthquakes are rare away from plate boundary zones, but a 2000 paper in Seismological Research Letters by John Ebel showed that aftershocks of these intra-plate earthquakes could last for several centuries. One of those was the 1663 Charlevoix, Quebec, earthquake; another was the 1356 earthquake in Basel, Switzerland. In the American Midwest, those would be prehistoric events. In 2009 Seth Stein and Mian Liu argued in Nature that these quiet settings seem to slow everything down, with stress increasing slowly and aftershock sequences lasting longer. They also noted that where the historical record is short, such as in the United States, it may be a mistake to judge the degree of earthquake hazard from events that are actually aftershocks rather than background seismicity. This knowledge may not help you cope with your nerves if you live in an aftershock zone. But it does give you some guidelines as to how bad things will be. And more concretely, it can help engineers judge how probable it is that your new building will be hit by significant aftershocks over the next few years and plan accordingly. PS: Susan Hough and her colleague Lucy Jones wrote an article on this subject for Eos, the house journal for the American Geophysical Union, in November 1997. The U.S. Geological Survey scientists closed by saying that we would like to propose that the phrase just an aftershock be hereafter banned from the English language. Tell your neighbors.