Click here to Skip to main content
Click here to Skip to main content

A C# Sample Code/Article Extending the Capabilities of GDI+ in C# (.NET) - Part II

, 3 Jul 2008
Rate this:
Please Sign up or sign in to vote.
This article is a continuation to my previous article where I present two more methods to do the same

Introduction

Here, in continuation to my previous article, I present two more methods to do the same.

Method 1 Project Name: ReversePaint

As suggested by a reader, we do have an equivalent to DrawDragRect(…) that is the public static void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) but the features like resizing the selection rectangle like how one resizes a normal window and Drag-Drop do require similar code as in my earlier sample code. As a POC, I present the sample code in the ReversePaint download. Please see the function public void DrawDragRect() for the only major difference. The CDrawDragRect can derive from both Panel and Form depending upon the coder’s choice.

Method 2 Project Name: WindowPosChanging

Here I take a totally new approach. As one can see, resizing and Dragging and Dropping the selection rectangle needs to behave like a normal window, then why not use a normal window to do the same? So Windows will handle resize and other features automatically. For this, the Window (which is actually going to be a form, let us call it the childLike form; see Figure below) will have to have a kind of hole in it so that the user can see through it for selecting a part of an image.

There is a Win32-API specifically for it, namely:

[DllImport("user32.dll")]
public static extern long SetLayeredWindowAttributes
    (IntPtr Handle, int Clr, byte transparency, int clrkey);

The fourth parameter int clrKey, is the key, When calling this API, I mention a particular color for the clrKey parameter and further I paint the childLike form with the same color. This will create a hole in the form’s client region.

But there is one down side to it - it will not create a hole in child windows (forms), this API will work only in a Pop-Up Window.

Now our childLike form will have to be a Pop-up Form, and not an MDI Child or Child window. If it is a Pop-up, it will move out of our main Window which host’s the Image. So the form movement needs to be restricted to the Main form. This is achieved by trapping the WM_WINDOWPOSCHANGING message in the form’s WndProc(ref Message m) function.

When handing this message, I just ensure that WINDOWPOS winPos (which I get through the m.Lparam) does not leave the Main form.

The handling of the message works two fold:

  1. The childLike form cannot be dragged outside the Mainform.
  2. When the main form is moved I just need to fire a WM_MOVE message for the childLike form and this ensures that the childLike form does not go outside the main form. Just take a look at the following code:
protected override void OnMove(EventArgs e)
{
    if (chFrm != null)
    {
         MoveWindow(chFrm.Handle, chFrm.Location.X, chFrm.Location.Y,
                            chFrm.Size.Width, chFrm.Size.Height, true);
         MinimumSize = new Size(chFrm.Size.Width + 100, chFrm.Size.Height);
    }

    base.OnMove(e);
}

Please note that the MoveWindow(...) call made inside the OnMove(...) function is a dummy.

This is kind of a free gift, just move the Main form all over your desktop and see how the childLike form follows it dutifully.

That's all for the resize feature, now for the Drag And Drop feature.

Since the form has a hole in it, a user can drag-drop the childLike form as shown in the image below:

This is achieved by overriding the Mouse-move function and inside it, I call the BestSelfMoveOnMoveNotRequired () function. This function performs the movement of the childLike form.

History

  • 10th June, 2008: Initial version
  • 2nd July, 2008: Article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dr.Sai

United States United States
No Biography provided

Comments and Discussions

 
Generalwtf PinmemberPeterTheGreat10-Jun-08 11:47 
QuestionRe: wtf PinmemberT-luv10-Jun-08 14:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 4 Jul 2008
Article Copyright 2008 by Dr.Sai
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid