Changing the drag cursor in C#
While implementing drag and drop operation, it might be necessary to change the drag cursor. There could be several reasons for doing so. For example, you might want to have different cursors for different categories of nodes dragged from a tree. Similarly, you might want different cursors in the different regions of the same target component.
The most important thing that should be understood while doing so is that, it is always the source component/control that is responsible for changing the drag cursor. Many programmers do not know this fact, and try to write the cursor changing code based on the drop target control. However, for the source to change the cursor, it needs some instructions/conditions which the target control can provide. Of course, in the absence of explicit instruction from target, there is always a default provision that governs the cursor.
In other words, suppose a node is being dragged from a tree to a panel. And, we want to different cursors for different regions within the panel. In this case, we check the mouse over coordinates and based on this, instruct the source tree to change the drag cursor.
For this, we need to handle ‘GiveFeedback’ event for the source control. As per above supposition, this source control is a tree. Then, we might have something like below.
myTree.GiveFeedback += MyTree_GiveFeedback;
Next, we write the code for changing the default drag cursor inside the handler for this event. The condition for changing cursor is derived from DragDropEffects type parameter. An example is shown below.
void MyTree_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
if (e.Effect == DragDropEffects.Move)
{
e.UseDefaultCursors = false;
Cursor.Current = Cursors.Hand;
}
else if (e.Effect == DragDropEffects.Copy)
{
e.UseDefaultCursors = false;
Cursor.Current = Cursors.Cross;
}
else if (e.Effect == DragDropEffects.None)
{
e.UseDefaultCursors = false;
Cursor.Current = Cursors.No;
}
else
{
e.UseDefaultCursors = true;
}
}
Now, at the target side, we add a handler for the event ‘DragOver’, i.e. something like below.
myPanel.DragOver += MyPanel_DragOver;
And inside the handler, we put the logic to change the value of DragDropEffects parameter.
void MyPanel_DragOver(object sender, DragEventArgs e)
{
if ( condition1() )
{
e.Effect = DragDropEffects.Move;
return;
}
if ( condition2() )
{
e.Effect = DragDropEffects.Copy;
return;
}
if ( condition3() )
{
e.Effect = DragDropEffects.None;
return;
}
e.Effect = DragDropEffects.All;
}
And thats it. With the conjuction of logic in target’s DragOver event handler and source’s GiveFeedback event handler, it is very easy to change the drag cursor based upon the conditions in the target control.
Filed under: .NET, C Sharp | Leave a Comment
Tags: changing drag cursor, Drag and drop, DragOver, GiveFeedback
Handling System Commands in C#
Recently, at my work, I had to trap few system commands in C#. I had to disable the movement of Form and few other basic operations such as minimize, maximize, close while the buttons for them still kept visible.
Doing this is very simple. All you need to do is ‘override’ the ‘WndProc’ method inside the Form class.
A sample override is shown below. This checks for System Command corresponding to Form’s movement (i.e SC_MOVE) and if found, returns from the method without calling the base’s WndProc method. As a result, SC_MOVE is pre filtered out.
protected override void WndProc(ref Message message)
{
const int WM_SYSCOMMAND = 0x0112;
switch(message.Msg)
{
case WM_SYSCOMMAND:
int command = message.WParam.ToInt32() & 0xfff0;
//Disable the movement of the Form
if (command == (int)SystemCommands.SC_MOVE)
return;
}
base.WndProc(ref message);
}
Above code uses an enumeration called ‘SystemCommands’. This enumeration is not inbuilt. You will have to define it youself.
A sample of this enumeration can be found at following location.
http://msdn.microsoft.com/en-us/library/ms646360(VS.85).aspx
Following sample is the same as the one in above location.
public enum SystemCommands
{
SC_SIZE = 0xF000,
SC_MOVE = 0xF010,
///
/// Sent when form minimizes
///
SC_MINIMIZE = 0xF020,
///
/// Sent when form maximizes
///
SC_MAXIMIZE = 0xF030,
///
/// Sent when form maximizes because of doubcle click on caption
///
SC_MAXIMIZE2 = 0xF032,
SC_NEXTWINDOW = 0xF040,
SC_PREVWINDOW = 0xF050,
///
/// Sent when form maximizes because of doubcle click on caption
///
SC_CLOSE = 0xF060,
SC_VSCROLL = 0xF070,
SC_HSCROLL = 0xF080,
SC_MOUSEMENU = 0xF090,
SC_KEYMENU = 0xF100,
SC_ARRANGE = 0xF110,
///
/// Sent when form is maximized from the taskbar
///
SC_RESTORE = 0xF120,
///
/// Sent when form maximizes because of doubcle click on caption
///
SC_RESTORE2 = 0xF122,
SC_TASKLIST = 0xF130,
SC_SCREENSAVE = 0xF140,
SC_HOTKEY = 0xF150,
SC_DEFAULT = 0xF160,
SC_MONITORPOWER = 0xF170,
SC_CONTEXTHELP = 0xF180,
SC_SEPARATOR = 0xF00F
}
Filed under: .NET, C Sharp | Leave a Comment
Tags: Disable Form Movement, SC_MOVE, System Commands
Recent Entries
- Changing the drag cursor in C#
- Handling System Commands in C#
- Why Comment your code?
- Hide properties inherited from Base class in the PropertyGrid
- Rubber Band / Marquee Selection tool in C#
- A Simple “Print Preview” and “Print” approach in .NET and Java
- How to put combo-boxes into toolbars in Eclipse RCP/Plugin ?
- Alternative to StringBufferInputStream
- How to deal with annoying Server Busy dialog box ?
- BSTR to CString and CString to BSTR Conversion
- Programming Linux Sockets
Categories
- .NET (5)
- C (3)
- C plus plus (6)
- C Sharp (4)
- COM (5)
- E-Commerce (3)
- Eclipse (1)
- Java (10)
- Java – Distributed (1)
- Java – Swing (2)
- JFace (1)
- Linux Network Programming (2)
- security (2)
- SIP (1)
- SWT (1)
- Uncategorized (3)
- Visual Studio (1)