开发者

Control resizing mathematics

Ok... So I've got this pretty killer Screenshot system I've been using for a while... It pretty much involves creating a somewhat transparent Form that auto-sizes to fit all monitors... Which gives the screen(s) a "dark-out" effect... I then have a hidden "button" control whos position is set upon mouse-down... Then, until mouse-up, the button will resize in real-time to create a sort of "selection" area... The Button is granted full transparency to implement this effect.

Source Example: http://db.tt/LWxfDB6 [Also posted below]

I'm having two issues that are directly related, I believe.

1.) The coordinates are off for multimonitors, it properly returns the coordinates that are clicked upon (highlighted), but the highlight box (effect) is often on the wrong monitor.

2.) You can only select from top-left ---> bottom-right and not universal.

Sorry if I didn't explain it well, the source should explain better. Thanks, in advance, for any and all help received. :)

:MarkRect.cs:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace TW_Media_Chat_ { public partial class MarkRect : Form { Point Point1; Point Point2; public MarkRect() { InitializeComponent(); // Programatically maximize to all monitors Screen[] Screens = Screen.AllScreens; int AllWidth = 0; int AllHeight = 0; for (int index = 0; index < Screens.Length; index++) { AllWidth += Screens[index].Bounds.Width; AllHeight += Screens[index].Bounds.Height; } this.Width = AllWidth; this.Height = AllHeight;

    }

    private void Transparency_MouseDown(object sender, MouseEventArgs e)
    {
        button1.Visible = true;
        button1.Location = Cursor.Position;
        Point1 = Cursor.Position;
    }
    private void Transparency_MouseUp(object sender, MouseEventArgs e)
    {
        this.Visible = false;
        Point2 = Cursor.Position;
        AjaxChatBridge.AjaxVars.Point1 = Point1;
        AjaxChatBridge.AjaxVars.Point2 = Point2;
        this.Close();
    }

    private void MarkRect_MouseMove(object sender, MouseEventArgs e)
    {
        this.button1.Width = Cursor.Position.X - this.button1.Left;
        this.button1.Height = Cursor.Position.Y - this.button1.Top;
    }
}

}

:MarkRect.Designer.cs:


namespace TW_Media_Chat_
{
    partial class MarkRect
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

    /// <summary>开发者_JS百科
    /// 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.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.BackColor = System.Drawing.Color.Lime;
        this.button1.ForeColor = System.Drawing.Color.Lime;
        this.button1.Location = new System.Drawing.Point(220, 172);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(19, 18);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = false;
        this.button1.Visible = false;
        // 
        // MarkRect
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.Black;
        this.ClientSize = new System.Drawing.Size(490, 406);
        this.Controls.Add(this.button1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "MarkRect";
        this.Opacity = 0.5D;
        this.Text = "Transparency";
        this.TopMost = true;
        this.TransparencyKey = System.Drawing.Color.Lime;
        this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Transparency_MouseDown);
        this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MarkRect_MouseMove);
        this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Transparency_MouseUp);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Button button1;
}

}


  1. You should implement control resizing logic easier:

    void MarkRect_MouseMove(...) { var mouseX = Cursor.Position.X; var originalMouseX = Point1.X;

    button1.Left = Math.Min(mouseX, originalMouseX);
    button1.Width = Math.Abs(mouseX - originalMouseX);
    
    // the same for Y
    

    }

  2. You should post that code here. We are to lazy to go to your dropbox to see the code. There are easier questions to answer here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜