5,427,303 members and growing! (19,720 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Windows Forms     Intermediate License: The Code Project Open License (CPOL)

The MFC unclickable button ported to Managed C++

By Chris Maunder, Nishant Sivakumar

This article presents a port of the MFC Trick button to .NET using Managed C++ and Windows Forms
C++/CLI, C#, VC7, C++, Windows, .NET, .NET 1.0VS.NET2002, Visual Studio, Dev

Posted: 23 May 2002
Updated: 23 May 2002
Views: 80,262
Bookmarked: 9 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 4.94 Rating: 4.58 out of 5
1 vote, 16.7%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
5 votes, 83.3%
5

Introduction

This article presents a port of the MFC unclickable button to .NET using Managed C++ and Windows Forms. Nish did the original 1:1 port of the original MFC control to Managed C++ to demonstrate how easy it was to port. Chris then took this, rounded off some of the corners and wrapped the control up in an assembly in order to demonstrate the class in use in a C# application.

Porting to .NET

The MCTrickButton class is a fully managed Windows Form class derived from System::Windows::Forms::Button.. Moving from MFC to Windows Forms was relatively painless - and in some cases positively sweet. There were a couple of issues though

As in the original we override the OnMouseMove method. Initially an attempt was made to override OnSetFocus as well, but the .NET OnSetFocus won't tell you what the previous control was which had the focus. Thus WndProc has been overridden and WM_SETFOCUS has been handled directly. The source code is commented and mostly self-explanatory.

There were also small changes such as having the desktop window be a Screen object instead of a Control (the analogy to Win32 being that the desktop is just another HWND) meant a minor change to the code but apart from that it was smooth sailing

The control has been compiled to an assembly "CodeProject.WinForms" (CodeProject.WinForms.dll)

Issues

Our Managed C++ WinForms control does not work inside the VS.NET Forms designer. I'm not sure if that's simply a case of C++ not having RAD support or if it's merely a problem with our code. Suggestions welcome.

TrickButton Source listing

Header

// MCTrickButton.h


#pragma once

using namespace System::Windows::Forms;

namespace CodeProject
{
    namespace WinForms
    {
        public __gc class MCTrickButton : public Button
        {
            // Constructor

        public:
            MCTrickButton();

            // Properties

        public:
            __property int get_JumpDistance()            
                                    { return m_nJumpDistance; }
            __property void set_JumpDistance(int value) 
                                    { m_nJumpDistance = value; }
        
            // Member variables

        protected:
            int m_nJumpDistance; // Pixels to jump


        protected:
            virtual void OnMouseMove(MouseEventArgs* e);
            virtual void WndProc(Message *pmsg);
        };
    }
}

Implementation

#include "StdAfx.h"


using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;

#include "MCTrickButton.h"


using namespace CodeProject::WinForms;


MCTrickButton::MCTrickButton()
{
     m_nJumpDistance = 5;
     TabStop = false;
}

// Called when the mouse moves over the button

void MCTrickButton::OnMouseMove(MouseEventArgs* e)
{
    // Get the current mouse position from the MouseEventArgs

    // object and convert to screen coords    

    Point point(e->X,e->Y);
    point = PointToScreen(point);

    // All pixels are used in parent client coordinates

    // So convert screen coords parent relative coords 

    Control *pParent = Parent;
    if (pParent != NULL)
        point = pParent->PointToClient(point);

    // Get the client area of the parent (so we know the bounds

    // of movement for the button

    Drawing::Rectangle ParentRect;
    if (pParent == NULL)
    {
        // use the current screen if no parent

        Screen *currentScreen = Screen::FromPoint(point);
        ParentRect = currentScreen->WorkingArea;
    }
    else
        ParentRect = pParent->ClientRectangle;

    // Get the dimensions of this button and convert to parent

    // coords

    Drawing::Rectangle ButtonRect = RectangleToScreen(ClientRectangle);
    if (pParent != NULL)
        ButtonRect = pParent->RectangleToClient(ButtonRect);

    Drawing::Point Center((ButtonRect.Right+ButtonRect.Left)/2,
                          (ButtonRect.Bottom+ButtonRect.Top)/2);


    // We now check where the mouse is relative to the center

    // of the button. We need to know from which direction the 

    // mouse is coming from so we can move the button away from 

    // the mouse


    Drawing::Rectangle NewButtonRect = ButtonRect;

    // mouse attack from the right side

    if (point.X  > Center.X)
    {
        if (ButtonRect.Left > ParentRect.Left + ButtonRect.Width + 
                             m_nJumpDistance)
        {
            NewButtonRect.X -= ButtonRect.Right - point.X + 
                                   m_nJumpDistance;
        }
        else
        {
            NewButtonRect.X += point.X - ButtonRect.Left + 
                                   m_nJumpDistance;
        }
    }
    // mouse attack from the left

    else if (point.X < Center.X) 
    {
        if (ButtonRect.Right < ParentRect.Right - ButtonRect.Width - 
                                   m_nJumpDistance)
        {
            NewButtonRect.X += point.X - ButtonRect.Left + 
                                   m_nJumpDistance;
        }
        else
        {
            NewButtonRect.X -= ButtonRect.Right - point.X + 
                                   m_nJumpDistance;
        }
    }
    // mouse attack from below

    if (point.Y > Center.Y)
    {
        if (ButtonRect.Top > ParentRect.Top + ButtonRect.Height + 
                                   m_nJumpDistance)
        {
            NewButtonRect.Y -= ButtonRect.Bottom - point.Y + 
                                   m_nJumpDistance;
        }
        else
        {
            NewButtonRect.Y += point.Y - ButtonRect.Top + 
                                   m_nJumpDistance;
        }
    }
    // attack from above

    else if (point.Y < Center.Y)
    {
        if (ButtonRect.Bottom < ParentRect.Bottom - ButtonRect.Height - 
                                   m_nJumpDistance)
        {
            NewButtonRect.Y += point.Y - ButtonRect.Top + 
                                   m_nJumpDistance;
        }
        else
        {
            NewButtonRect.Y -= ButtonRect.Bottom - point.Y + 
                                   m_nJumpDistance;
        }
    }

    // now we move the button to the new point

    Location = Point(NewButtonRect.X, NewButtonRect.Y);

    Button::OnMouseMove(e);
}

void MCTrickButton::WndProc(Message *pMsg)
{
     // Call the base class function first

    Button::WndProc(pMsg);

    // If the button is recieving the focus then pass

    // it back to the window that gave it the focus

    if (pMsg->Msg == WM_SETFOCUS)
    {
        Control *pOldWnd = FromHandle(pMsg->WParam); 
        if (pOldWnd != NULL) 
            pOldWnd->Focus(); //set focus to previous control

    } 
}

A sample app

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using CodeProject.WinForms;

namespace TrickButtonDemo
{
    /// Summary description for Form1.

    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button QuitButton;
        private CodeProject.WinForms.MCTrickButton TrickButton;

        /// Required designer variable.

        private System.ComponentModel.Container components = null;

        public Form1()
        {
            InitializeComponent();
        }

        /// Clean up any resources being used.

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()
        {
            this.QuitButton = new System.Windows.Forms.Button();
            this.TrickButton = new CodeProject.WinForms.MCTrickButton();
            this.SuspendLayout();
            // 

            // QuitButton

            // 

            this.QuitButton.Location = new System.Drawing.Point(208, 128);
            this.QuitButton.Name = "QuitButton";
            this.QuitButton.TabIndex = 0;
            this.QuitButton.Text = "Quit";
            this.QuitButton.Click += 
                          new System.EventHandler(this.QuitButton_Click);
            // 

            // TrickButton

            // 

            this.TrickButton.Location = new System.Drawing.Point(100, 60);
            this.TrickButton.Name = "TrickButton";
            this.TrickButton.Text = "Click Me!";
            this.TrickButton.Click += 
                          new System.EventHandler(this.TrickButton_Click);
            // 

            // Form1

            // 

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 165);
            this.Controls.AddRange(new System.Windows.Forms.Control[] { 
                                     this.TrickButton, this.QuitButton });
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void QuitButton_Click(object sender, System.EventArgs e)
        {
            Close();    
        }

        private void TrickButton_Click(object sender, System.EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("You clicked me!");
        }
    }
}

License

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

About the Authors

Chris Maunder


Sitebuilder, Editor, Staff, Admin
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.
Occupation: Founder
Company: The Code Project
Location: Canada Canada

Nishant Sivakumar


Sitebuilder, Mvp
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.

Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.

Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.
Location: United States United States

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralYour code ported to C#membergeorani11:55 21 Apr '05  
GeneralYour Code Ported to VB (It works)membergeorani10:18 21 Apr '05  
GeneralI want to ask a question!memberUtopiaLiu3:40 12 Jun '03  
GeneralWindows.Forms.Control in Browser:sussAnonymous22:30 29 Mar '03  
GeneralVC++sussAyman Rabee1:04 1 Aug '02  
GeneralRe: VC++editorNishant S1:08 1 Aug '02  
GeneralActually...memberTim Smith9:50 24 May '02  
GeneralRe: Actually...memberNish - Native CPian18:15 24 May '02  
GeneralThe quit button should be unclickable ?memberDaniel Turini9:36 24 May '02  
GeneralRe: The quit button should be unclickable ?memberNish - Native CPian18:16 24 May '02  
GeneralA request from users ...memberBlackCat8:36 24 May '02  
GeneralRe: A request from users ...memberShog912:59 24 May '02  
GeneralRe: A request from users ...memberbrian141516:03 24 May '02  
GeneralRe: A request from users ...memberShog917:24 24 May '02  

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

PermaLink | Privacy | Terms of Use
Last Updated: 23 May 2002
Editor: Chris Maunder
Copyright 2002 by Chris Maunder, Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project