Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

happen to open a menu while holding the Ctrl key, you may see some unexpected behavior. There are really four issues that should be addressed here:

1There is no feedback. The user cannot tell that an alternate behavior will occur when the Ctrl key is pressed. Aside from reading the nonexistent documentation, the user must somehow figure out that this feature is available.

2The context menu is displayed while the Ctrl key is pressed.

3If the Ctrl key is released outside of the form, the ctrlKeyHeld field is not reset.

4If the Ctrl key is released while displaying the menu, again the ctrlKeyHeld field is not reset.

For the first problem, we can fix this by using an alternate cursor when the Ctrl key is held. The second problem can be addressed by turning off the context menu when Ctrl is pressed. The other problems require that we handle the event that occurs in these situations. The following steps make these changes to finish our example.

MARK CTRL KEY RELEASED WHEN APPROPRIATE

 

Action

Result

 

 

 

9

Modify the OnKeyDown method so

protected override void OnKeyDown

 

that when the Ctrl key is pressed:

(KeyEventArgs e)

 

a. An alternate cursor is used.

{

 

. . .

 

b. The context menu is disabled.

case Keys.ControlKey:

 

ctrlKeyHeld = true;

 

 

 

 

pnlPhoto.Cursor = Cursors.SizeWE;

 

 

this.ContextMenu = null;

 

 

break;

 

 

. . .

 

 

 

10

Create a ReleaseCtrlKey method

private void ReleaseControlKey()

 

to encapsulate the logic now

{

 

required when the Ctrl key is

ctrlKeyHeld = false;

 

pnlPhoto.Cursor = Cursors.Default;

 

released.

 

this.ContextMenu = ctxtMenuView;

 

 

}

 

 

 

11

Use this new method in override of

protected override void OnKeyUp

 

the OnKeyUp method.

(KeyEventArgs e)

 

 

{

 

 

switch (e.KeyCode)

 

 

{

 

 

case Keys.ControlKey:

 

 

ReleaseControlKey();

 

 

break;

 

 

. . .

 

 

}

 

 

 

12

Override the OnDeactivate

protected override void OnDeactivate

 

method to release the Ctrl key

(EventArgs e)

 

when the Form is deactivated.

{

 

if (ctrlKeyHeld)

 

 

 

 

ReleaseControlKey();

 

 

base.OnDeactivate(e);

 

 

}

 

 

 

MOUSE EVENTS

391

able from the Control Panel.
Mouse Properties window avail
can be modified through the

MARK CTRL KEY RELEASED WHEN APPROPRIATE (continued)

 

Action

Result

 

 

 

13

Override the OnMenuStart

protected override void OnMenuStart

 

method to release the Ctrl key

(EventArgs e)

 

when a menu is selected.

{

 

if (ctrlKeyHeld)

 

 

 

 

ReleaseControlKey();

 

 

base.OnMenuStart(e);

 

 

}

 

 

 

Note how the form’s ContextMenu property is set to null and then back to ctxtMenuView to disable and then enable the context menu. The cursor to display for the Panel is modified using the Cursor property inherited from the Control class.

The Cursors class provides access to various mouse pointer cursors available in the operating system. The actual cursor for many of the properties in this class can be reconfigured by the user, so the actual images associated with a specific setting may change. While we could list the available properties in the Cursors class, it would not be very helpful without the graphics to go with it. Instead, figure 12.1 shows a sampling of the default graphics in Windows 2000 as seen in the Properties window of Visual Studio .NET. Check out this window in your

version of Visual Studio to see the cursors in use on your system.

Run the program to make sure it works as expected. There are now way too many ways to invoke the Next and Previous menus. We have the menus themselves, the access keys with the Alt+V and Alt+N/ P, the keyboard shortcuts Ctrl+Shift+N and Ctrl+Shift+P, the keyboard characters plus ‘+’ and minus ‘–’, the Page Down and Page Up keys, and finally the mouse buttons while holding down the Ctrl key. While this may be a bit overboard for many applications, it provides a good sampling of the various types of interfaces you might consider using in your applications. Consider your choices wisely, and document them well, and you may even put a smile on your customer’s face.

This brings us to the end of our keyboard and Figure 12.1 The cursor graphic mouse events discussion. Our next topic will be image

buttons.

392

CHAPTER 12 A .NET ASSORTMENT

Соседние файлы в папке c#