任何人都可以逐行解释这个代码 [英] Can anyone explain this code line by line

查看:80
本文介绍了任何人都可以逐行解释这个代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过鼠标移动无边框Windows窗体应用程序。最初我试过这段代码:



I am trying to make a borderless Windows form application movable via mouse. initially I tried this code:

bool mousedown = false;

mouse_down_event_handler()
{
mousedown=true;
}

mouse_up_event_handler()
{
mousedown=false;
}

mouse_move_event_handler()
{

this.location = e.location; // "e" is the mouse pointer and "this" is the windows form
}







------------------- -------------------------------------------------- ---->



它似乎无法正常工作。所以我花了一些时间寻找更好的代码并最终得到这个:






------------------------------------------------------------------------->

It didn't seem to work properly. So I spent some time searching for a better code and ended up with this:

protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x84:
                    base.WndProc(ref m);
                    if ((int)m.Result == 0x1)
                        m.Result = (IntPtr)0x2;
                    return;
            }
            base.WndProc(ref m);

        }





____________But我不明白一行.__________





我在搜索如何从我的c#应用程序中更改桌面背景时看到过这类代码。



(1)这些代码是什么意思?

(2)这些代码是我们在使用windows时使用的代码吗?

组件?



---------------------------------- ------

请逐行解释代码

--------------------- -------------------



先谢谢,(^ _ ^)



____________But I didn't understand a single line.__________


I have seen such types of codes while I was searching for " How to change desktop background from my c# application ".

(1) What does these code mean ?
(2)Are these the codes that we should use while working with windows'
components ?

----------------------------------------
Please explain the code line by line
----------------------------------------

Thanks in Advance,(^_^)

推荐答案

您是否知道逐行解释代码的工作量是多少?

每一行都需要一段解释!例如:

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();



创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。



可以你想象我们需要多长时间才能解释一个像你的例子一样的非常短的代码片段,一行一行?



不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 您是否希望坐下45分钟并且没有充分的理由键入逐行描述?


Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?


查看对您不起作用的代码逐行通常是浪费时间。



重要的是分析代码并理解它的作用......或者不做什么。 ..现在。



以MouseDown和MouseMove为例:当你这样做时:



这个.location = e.location;



然后你说:将接收MouseMove事件的Control的左上角放在鼠标现在的位置在控制上。请记住,e.Location返回的是基于Control的坐标,而不是表单上位置的坐标!因此,如果e.Location报告您是100,100,您将在表格坐标中将控件的位置设置为100,100!



WndProc示例只是一个错误;而且我会忘记它:有工作的WndProc解决方案可以做到这一点,例如:[ ^ ],但是,在这个简单的例子中你不需要它们。



例如:



1.当鼠标在控件中关闭时,将布尔标志设置为'true;当鼠标启动时将其设置为'false。



a。当鼠标按下时,将两个整数变量mx,my设置为MouseDown处理程序的e.X和e.Y.这将在控件坐标中记录鼠标在控件上的位置。



2.在MouseMove EventHandler中:



a。如果鼠标已经启动...根据旗帜的价值...退出



b。如果鼠标停机:



1.根据从当前鼠标位置e.X中减去Mousedown mx的初始值,创建一个水平偏移量;创建一个类似的垂直偏移量。



2.将水平偏移量添加到Control的Left属性中;将垂直偏移添加到'Top属性。
Going over code that doesn't work for you line-by-line is generally a waste of time.

What's important is for you to analyze the code and understand what it does ... or doesn't do ... now.

Take the example using MouseDown and MouseMove: when you do this:

this.location = e.location;

Then you are saying: make the top-left corner of the Control that is receiving the MouseMove Event be positioned where the Mouse is now on the Control. Remember that what e.Location returns are co-ordinates based on the Control, not co-ordinates of a Location on the Form ! So, if e.Location reports you are at 100,100, you will set the Location of the Control to 100,100 in Form co-ordinates !

The WndProc example is just a mistake; and I'd forget about it: there are working WndProc solutions that will do this, like: [^], but, in this simple case you don't need them.

Example:

1. set a boolean flag to 'true when the Mouse goes down in the Control; set it to 'false when the Mouse is up.

a. when the Mouse goes down set two integer variables mx,my to the e.X and e.Y of the MouseDown handler. This records the location on the Control where the Mouse went down, in Control co-ordinates.

2. in the MouseMove EventHandler:

a. if the Mouse is up ... based on the value of the flag ... exit

b. if the Mouse is down:

1. create a horizontal offset based on the subtracting the initial value of the Mousedown mx from the current mouse location e.X; create a similar vertical offset.

2. add the horizontal offset to the 'Left property of the Control; add the vertical offset to the 'Top property.
private bool isMouseUp = true;

private int mx, my;

private void SomeControl_MouseDown(object sender, MouseEventArgs e)
{
    isMouseUp = false;
   
    mx = e.X;
    my = e.Y;
}

private void SomeControl_MouseUp(object sender, MouseEventArgs e)
{
    isMouseUp = true;
}

private void SomeControl_MouseMove(object sender, MouseEventArgs e)
{
    if(isMouseUp) return;

    SomeControl.Left += e.X - mx;
    SomeControl.Top += e.Y - my;
}

现在这是非常简单的事情...当你理解控制坐标和表格(或其他容器控制)坐标之间的区别时。



为了让这更有趣,我挑战你弄清楚当用户移动Control或Form ...时总是确保它的所有区域都在屏幕上,或在其集装箱控制范围内。提示:ClientRectangle,Rectangle.Intersect。

Now this is really simple stuff ... when you understand the difference between Control co-ordinates, and Form (or other Container Control) co-ordinates.

To make this more interesting, I challenge you to figure out how ... when the user moves the Control, or Form ... to always make sure all of its area is on-screen, or within the bounds of its Container Control. Hints: ClientRectangle, Rectangle.Intersect.


查看相应的MSDN文档。

您复制的代码是魔术数字不良影响的一个很好的例子:你没有机会去寻找他们的意思。



互联网上的其他资源提供了更好的例子。



C#中一个完美的工作版本是:
See the respective MSDN documentation.
Your copied code is a good example of the bad effect of "magic" numbers: you have no chance to search for their meaning.

Other sources in the internet give better examples.

A perfectly working version in C# is this:
namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        private const int WN_NCHITTEST = 0x0084;
        private const int HTCLIENT     = 0x0001;
        private const int HTCAPTION    = 0x0002;

        public Form1()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WN_NCHITTEST:
                    base.WndProc(ref m);
                    if (m.Result == (IntPtr)HTCLIENT) m.Result = (IntPtr)HTCAPTION;
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

    }
}

在MSDN中查找WndProc函数,WN_NCHITTEST消息的含义,最后是HTCLIENT和消息结构的HTCAPTION结果值告诉你

Looking up the WndProc function in MSDN, the meaning of the WN_NCHITTEST message, and finally the HTCLIENT and HTCAPTION result values of the Message structure tell you



  1. WndProc 重写的方法流程Windows消息
  2. WN_NCHITTEST 消息询问窗口的哪个部分对应于特定的屏幕坐标
  3. HTCLIENT 结果告诉坐标在客户区
  4. HTCAPTION 结果告诉坐标在标题中bar

  1. the WndProc overridden method processes windows messages
  2. the WN_NCHITTEST message asks what part of the window corresponds to a particular screen coordinate
  3. the HTCLIENT result tells that the coordinate is in the client area
  4. the HTCAPTION result tells that the coordinate is in the title bar



全部放在一起:此代码将所有客户区点击重定向到标题栏。即从窗口管理器的角度来看,窗口的行为就好像是一个标题栏而没有鼠标点击的客户区域。

干杯

Andi


Putting all together: This code re-directs all client area clicks to the title bar. I.e. from a window manager point of view, the window behaves as if it was one title bar and no client area for what concerns mouse clicks.
Cheers
Andi


这篇关于任何人都可以逐行解释这个代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆