How could I make a label able to move my whole form?
Thanks
How could I make a label able to move my whole form?
Thanks
Would the label on the form be capable of dragging itself?
Uhhh.. I don't know.
Probably not since I can't drag it or the form that way.
Would you happen to be able to give me the code to be able to drag on the label and drag the whole form?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure WMNCHitTest(var Msg: TWMNCHitTest) ; message WM_NCHitTest;
procedure FormCreate(Sender: TObject);
procedure Label1MouseEnter(Sender: TObject);
procedure Label1MouseLeave(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest) ;
var
MausPos: TPoint;
X, Y: integer;
hit: boolean;
begin
GetCursorPos(MausPos);
X := MausPos.X - Form1.Left - 3; {magic number}
Y := MausPos.Y - Form1.Top - 23; {magic number}
hit := (X <= Label1.Left + Label1.Width) AND
(X >= Label1.Left) AND
(Y <= Label1.Top + Label1.Height) AND
(Y >= Label1.Top);
inherited;
if (Msg.Result = htClient) AND hit then Msg.Result := htCaption;
end;
end.
Ok, It's janky, but this should do the trick. The mouse events weren't working like I thought they would. This checks to see if the mouse is over the Label when dragging starts. If that is the case, then the drag event on the client area of the form is treated like a titlebar (traditional) drag. Otherwise, no dice. The "magic numbers" happened to be the pixel width of my window borders (3) and the pixel height of my titlebars (23), though this may vary from system to system. This solution is a bit incomplete because I don't know how to get that out of Delphi. Maybe someone else can refine this a bit.
Found most of this info on About.com, specifically http://delphi.about.com/od/windowsshellapi/a/dragnocaption.htm. Google and ye shall find :p
How would I do this in say... C#? :|
I'm a tard... Been at work all day...
Its ok :)
Its just frustrating how almost nobody knows how to do this..
does everyone seriously use default borders? >_>
here see the code below, and i also attach the working solution to this post.
Form1.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace dragUsingLabel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Point mouseCoordinates = new Point(0, 0);
bool mouseIsDown = false;
private void label1_MouseDown(object sender, MouseEventArgs e)
{
mouseIsDown = true;
mouseCoordinates = new Point(e.X, e.Y);
}
private void label1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseIsDown)
{
this.Left += (e.X - mouseCoordinates.X);
this.Top += (e.Y - mouseCoordinates.Y);
}
}
private void label1_MouseUp(object sender, MouseEventArgs e)
{
mouseIsDown = false;
}
}
}
also it is better to change the cursor to hand for the label :
namespace dragUsingLabel
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label1.Cursor = System.Windows.Forms.Cursors.Hand;
this.label1.Location = new System.Drawing.Point(89, 69);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(73, 13);
this.label1.TabIndex = 0;
this.label1.Text = "drag using me";
this.label1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.label1_MouseMove);
this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label1_MouseDown);
this.label1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.label1_MouseUp);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.