C++Language FeatureLearn C++

This Is How To Make Your Non-Rectangular Apps Draggable

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition for students, beginners and startups.

Borderless forms

Normally we can easily drag application windows by using top bar of the application window. In some applications you may select borderless Form designs (BorderStyle = bsNone). In that time, generally you should allow users to drag your application by touching an image or any UI element (visual components).

Sometimes applications don’t use a regular rectangular form for their main window, they don’t need a form design and they may have different specific shapes (video and music player skins are like these examples), If we want to develop these kind of applications, we must hide window base and borders. In C++ Builder, we can easily develop these kind of transparent FMX applications. FireMonkey (FMX) framework supports moderns ways to manipulate transparency of each UI elements, bitmaps, canvas drawings, and allows you to set pixel colors in ARGB form. Here again users may want to move your transparent applications.

Overriding default Window behavior in C++

In C++ Builder, basically we can move forms by changing their Left and Top positions to a new X and Y values as below,

We can also drag applications by using OnMouseMove() OnMouseDown() and OnMouseUp() events of a visual component. In our example we will use Image (TImage) component as a visual image, so we can add any image (with alpha color) as a skin of our application. Let’s see how we can drag an application by using image on it.

How can we make a form draggable when the mouse is over an image?

  1. Create a new Multi-Device FMX Application Project, save all project and unit files into a folder.
  2. Add a Image (TImage), load an image (may be a .png image with alpha color), arrange your Image, or set Align to Client
  3. If you are in code display press F12 to Form Design view
  4. Click on the Form Unit and from the Object Inspector set it’s Transparency property checked. (Let’s do a transparent app)
  5. We need a Boolean variable drag_form to check if dragging by mouse is started. And we need last x and y positions LX and LY before dragging. So let’s add these to our application as below

6. Now we should add OnMouseDown(), OnMouseMove() and OnMouseUp() events of TImage to check if we start to drag , if we are dragging and if we stop dragging. To do this, select your Image (TImage) component, in Object Inspector panel go to events tab and double click to OnMouseDown(), OnMouseMove() and OnMouseUp() events to create these procedures automatically.

In OnMouseDown() event we should set LX and LY to mouse X and Y position on the Image and we should set drag_form to true.
In OnMouseMove() event we should check if we started to drag and we should set position of form to changes in mouse coordinates (X-LX and Y-LY).
In OnMouseUp() event we should set drag_form false, that means we stop dragging when the mouse is up.

How to trap mouse movements in C++

7. Now you can run your application by pressing F9 or Run Button, That’s all.

As you see it is very easy to make your apps draggable by using this method.


RAD Studio C++ Builder is a great environment for learning to use C++ and is also powerful enough for all your development needs. Why not download and try C++ builder today?

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

C++Introduction to C++Language FeatureLearn C++

How To Include Or Exclude Trailing Backslashes On Windows

C++Introduction to C++Language FeatureLearn C++

How To Check Characters of File Names and Paths on Windows

C++Introduction to C++Language FeatureLearn C++

How to use SameFileName method on Windows

en_USEnglish