当前位置:编程学习 > C#/ASP.NET >>

关于winform移动控件

我用的是

private bool isMouseMove = false;//获取鼠标是否游动
        private Point mouseOffset; //记录鼠标指针的坐标

protected void temp_MouseDown(object sender, MouseEventArgs e)
        {
            //当鼠标点击左键 执行对应操作
            if (e.Button == MouseButtons.Left)
            {
               isMouseMove = false;
            }

protected void temp_MouseMove(object sender, MouseEventArgs e)
        {
            //Bitmap img = new Bitmap(temp.Image);
            //temp.DrawToBitmap(img, temp.Bounds);

            if (isMouseMove == false)
            {
                mouseOffset.X = e.X;
                mouseOffset.Y = e.Y;
                isMouseMove = true;
                return;
            }
            if (isMouseMove == true)
            {
                int left = temp.Left + e.X - mouseOffset.X;
                int top = temp.Top + e.Y - mouseOffset.Y;
                temp.Location = new Point(left, top);
            }
        }


来移动一个 picturebox控件 但是 因为程序需要把部分控件设置了Transparent属性 导致移动的过程 picturebox移动的过程总是有重影

我想用API的方式进行移动

[DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        private void frmMain_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, 0x0112, 0xF012, 0);
        }


它是一个 MouseDown 鼠标点击着控件才能移动,我把frmMain_MouseDown 这个方法关联到 mousemove 事件中,但是不起效

所以想请大家帮帮忙或者有哪些好的办法?急,万分感谢! winform C# --------------------编程问答-------------------- 把你移动temp的代码改了一下,这是随鼠标移动。
        private Point mouseOffset; //记录鼠标指针的坐标
        protected void temp_MouseDown(object sender, MouseEventArgs e)
        {
            mouseOffset.X = e.X;
            mouseOffset.Y = e.Y;
        }
        protected void temp_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int left = temp.Left + e.X - mouseOffset.X;
                int top = temp.Top + e.Y - mouseOffset.Y;
                temp.Location = new Point(left, top);
            }
        }
如果,你打算在MouseUp的时候再执行移动,可以:
预先绘制一个rectangleShape1
        private Point mouseOffset; //记录鼠标指针的坐标
        protected void temp_MouseDown(object sender, MouseEventArgs e)
        {
            mouseOffset.X = e.X;
            mouseOffset.Y = e.Y;
        }
        protected void temp_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int left = temp.Left + e.X - mouseOffset.X;
                int top = temp.Top + e.Y - mouseOffset.Y;
                rectangleShape1.Visible = true;
                rectangleShape1.Size = new Size(temp.Width, temp.Height);
                rectangleShape1.Location = new Point(left, top);
            }
        }
        private void temp_MouseUp(object sender, MouseEventArgs e)
        {
            rectangleShape1.Visible = false;
            temp.Location = rectangleShape1.Location;
        }
至于你说的阴影,还要看你其它控件的设置方式,背景的实现方式等等。
--------------------编程问答-------------------- 一楼的意思是,帮我解决移动控件不能支持透明的办法吗?我主要想只要,用API的那种方式移动,会不会有阴影,而且具体怎么用 --------------------编程问答-------------------- API的方式
新建工程,在Form1上放入PictureBox1
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.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("user32")]
        private static extern bool ReleaseCapture();

        [DllImport("user32")]
        private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0Xf010;
        public const int HTCAPTION = 0x0002;

        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.pictureBox1.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
    }
} --------------------编程问答-------------------- 你自己那段代码是移动窗口。区别在于SendMessage传入的第一个参数是谁的句柄。 --------------------编程问答-------------------- 那意思是 SendMessage 这个必须依赖 MouseDown 这个事件?换成 MouseMove 是行不通?可怜我只是个新手,什么都理解有限 --------------------编程问答--------------------
引用 5 楼 u011495883 的回复:
那意思是 SendMessage 这个必须依赖 MouseDown 这个事件?换成 MouseMove 是行不通?可怜我只是个新手,什么都理解有限

呵呵,SendMessage 是发送系统消息,第一个参数是窗口的句柄,指向发送的目标。
你的代码是用的窗体的句柄(this.Handle),就是把开始移动窗口的消息发给了窗体,所以是移动窗体。
你现在要移动的是图片框,所以应该用图片框的句柄(this.pictureBox1.Handle)。

这段代码应当放在MouseDown中。因为正常的操作都是点击对象时,指示该对象可拖动。 --------------------编程问答-------------------- 嗯嗯,之前我也是用 this.pictureBox1.Handle 是能移动 pictureBox1 不过刚才试了一下,移动的重影没有了,API 这种方式还是挺好的 --------------------编程问答-------------------- 对了,还有一个问题想请教的,在窗体移动picturebox的时候,它都不支持透明还是底层的颜色
我放的图片是支持透明的web png --------------------编程问答-------------------- --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,