Monday 28 November 2011

How do you interrupt a Windows program? in C programming

How do you interrupt a Windows program?

As a user of Microsoft Windows, you might already know that you can terminate a Windows program in many ways. Here are just a few methods:

  •  Choose File | Exit from the pull-down menu.
  •   Choose Close from the control box menu (-) located to the left of the title bar.
  •   Double-click the mouse on the control box.
  •  Press Ctrl-Alt-Delete.
  •   Choose End Task from the Windows Task Manager.
  •   Exit Windows.

This list includes the more typical ways users exit their Windows programs. As a Windows developer, how
can you provide a way for users to interrupt your program?

If you have used many DOS programs, you might remember the key combination Ctrl-Break. This combination was often used to break out of programs that were hung up or that you could not figure a way to get out of. Often, DOS programs would not trap the Ctrl-Break combination, and the program would be aborted. DOS programs could optionally check for this key combination and prevent users from breaking out of programs by pressing Ctrl-Break. 

Under Windows, the Ctrl-Break sequence is translated into the virtual key VK_CANCEL (see FAQ XXI.17 for an explanation of virtual keys). One way you can trap for the Ctrl-Break sequence in your Windows program is to insert the following code into your event (message) loop:
...
switch (message)
{
case WM_KEYDOWN:
if (wParam == VK_CANCEL)
{
/* perform code to cancel or
interrupt the current process */
}
}
...
In the preceding example program, if the wParam parameter is equal to the virtual key code VK_CANCEL, you know that the user has pressed Ctrl-Break. This way, you can query the user as to whether he wants to cancel the current operation. This feature comes in handy when you are doing long batch processes such as printing reports.

Cross Reference:

None.

No comments:

Post a Comment