Click here to Skip to main content
6,595,854 members and growing! (18,503 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate License: The Code Project Open License (CPOL)

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

By Dr.Sai

This article is a continuation to my previous article where I present two more methods to do the same
C#, .NET, WinXP, VistaVS2005, Dev
Posted:10 Jun 2008
Updated:4 Jul 2008
Views:13,280
Bookmarked:29 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 2.81 Rating: 4.67 out of 5

1

2

3
1 vote, 25.0%
4
3 votes, 75.0%
5

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)

About the Author

Dr.Sai


Member

Location: United States United States

Other popular GDI+ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Generalwtf PinmemberPeterTheGreat12:47 10 Jun '08  
QuestionRe: wtf PinmemberT-luv15:56 10 Jun '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Jul 2008
Editor: Deeksha Shenoy
Copyright 2008 by Dr.Sai
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project