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
#pragma once
using namespace System::Windows::Forms;
namespace CodeProject
{
namespace WinForms
{
public __gc class MCTrickButton : public Button
{
public:
MCTrickButton();
public:
__property int get_JumpDistance()
{ return m_nJumpDistance; }
__property void set_JumpDistance(int value)
{ m_nJumpDistance = value; }
protected:
int m_nJumpDistance;
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;
}
void MCTrickButton::OnMouseMove(MouseEventArgs* e)
{
Point point(e->X,e->Y);
point = PointToScreen(point);
Control *pParent = Parent;
if (pParent != NULL)
point = pParent->PointToClient(point);
Drawing::Rectangle ParentRect;
if (pParent == NULL)
{
Screen *currentScreen = Screen::FromPoint(point);
ParentRect = currentScreen->WorkingArea;
}
else
ParentRect = pParent->ClientRectangle;
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);
Drawing::Rectangle NewButtonRect = ButtonRect;
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;
}
}
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;
}
}
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;
}
}
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;
}
}
Location = Point(NewButtonRect.X, NewButtonRect.Y);
Button::OnMouseMove(e);
}
void MCTrickButton::WndProc(Message *pMsg)
{
Button::WndProc(pMsg);
if (pMsg->Msg == WM_SETFOCUS)
{
Control *pOldWnd = FromHandle(pMsg->WParam);
if (pOldWnd != NULL)
pOldWnd->Focus(); }
}
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
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button QuitButton;
private CodeProject.WinForms.MCTrickButton TrickButton;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.QuitButton = new System.Windows.Forms.Button();
this.TrickButton = new CodeProject.WinForms.MCTrickButton();
this.SuspendLayout();
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);
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);
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
[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!");
}
}
}
Chris Maunder is the co-founder of
CodeProject, DeveloperMedia and ContentLab, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.
In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project,
CodeProject.AI.
In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. While at CodeProject, Chris' roles included Architecture and coding, Product Development, Content Creation, Community Growth, Client Satisfaction and Systems Automation, and many, many sales meetings. All while keeping his sense of humour.
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.
Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.
Nish's Technology Blog :
voidnish.wordpress.com