Windows错误框更上一层楼


15

您知道那些Windows盒子只能做一件事吗?

Windows错误框:继续将删除硬盘驱动器中的内容。 你想让我做什么? <继续> <删除>

让我们将其带入一个新的高度!

挑战

创建一个带有按钮的对话框,该按钮可随您的光标移动到任何地方!

眼镜

不可输入。可能输出到STDOUT或STDERR。您必须使用一个按钮打开一个窗口(不需要文本,但是文本可能包含在窗口标题,对话框或按钮中)。该按钮必须是可单击的,并且必须始终位于光标下方,以便您只能单击它。单击后,它可以做任何您想做的事,但是请不要使它崩溃,因为我将对此进行测试...只要不按下按钮,对话框就必须保持打开状态,但是它不需要关闭按钮时关闭。

你可以做的假设

  • 您可以假定光标将停留在绘图画布中。
  • 您可以假定窗口将保持焦点,但是只要不按下按钮,就不能使窗口失去焦点。

伪代码示例

这是一些示例伪代码:

Open Dialog Box
Add Button to Dialog Box at position X, Y
WHILE True:
    SET Dialog Box to CursorX - X, CursorY - Y

无法为该挑战提供测试用例

这是一个代码高尔夫球挑战赛,因此3月14日(Pi Day)最短的有效提交将获胜!

Answers:


9

MATLAB,86 63字节

set(warndlg,'WindowButtonM',@(s,e)movegui(get(0,'Po')-[99,20]))

该解决方案利用了MATLAB的(通常很烦人的)能力来使用部分属性名称,只要提供的部分仅对感兴趣的属性是唯一的即可。

该解决方案使用内置函数warndlg来创建带有“确定”按钮的警告对话框。该函数返回一个figure句柄,然后我们可以使用它来设置WindowButtonMotionFcn回调(使用短名称'WindowButtonM')。

每当在窗口内移动光标时都会评估的回调将获得光标的当前位置(使用根图形对象PointerLocation属性,使用它的简称)。然后,我们在应用偏移量后使用并指定图形的新位置来更新图形的,以便将光标放置在按钮上。'Po'Positionmovegui[99, 20]

最后单击该按钮时,该图将被删除,所有回调将自动释放。

enter image description here


您能否提供一个链接来测试此代码?我没有Matlab软件,并且此代码不适用于Octave(因为它仅与某些Matlab代码兼容)。谢谢!
HyperNeutrino

3
@AlexL。不幸的是,没有免费的在线实用程序来显示此代码的工作方式。即使有,UI控件也不会在Web界面中呈现。我已包含GIF来演示代码的工作原理。
Suever

好的。那我就不用打扰了。干得好,+ 1。
HyperNeutrino

Windows R2014a上的几个问题。您可以通过以下方式停止对话框:1.将鼠标(以及对话框)移动到任务栏(我的任务栏在屏幕的右侧),对话框将停止跟随鼠标;2.足够快地移动鼠标,以使鼠标在对话框的任何框中都位于对话框的外部,这时该框将停止跟随鼠标。对话框的样式显然还取决于其他内容...使用当前代码,我的鼠标几乎没有错过OK按钮,并且我必须将99更改为120才能正常工作。无论如何,辛苦了!
Frenzy Li

是我的观点。我的鼠标在那个红色十字准线附近。
Frenzy Li

4

C#(Windows窗体应用程序),200个 114字节

void F(){var p=Cursor.Position;MouseMove+=(r,t)=>{Controls.Add(new Button());Location=new Point(p.X-30,p.Y-40);};}

未打高尔夫球

void F()
{
     Controls.Add(new Button());

     MouseMove += (r, t) =>
     {
        var p = Cursor.Position;
        Location = new Point(p.X - 30, p.Y - 40);
     };
}

旧的200字节解决方案:

public void F(){var t=this;var b=new Button();b.Click+=delegate{t.Close();};t.Controls.Add(b);t.Show();for(;;){Application.DoEvents();t.Location=new Point(Cursor.Position.X-30,Cursor.Position.Y-40);}}

未打高尔夫球

    public void F()
    {
        var t = this;
        var b = new Button();

        b.Click += delegate
        {
            t.Close();
        };

        t.Controls.Add(b);
        t.Show();

        for (;;)
        {
            Application.DoEvents();
            t.Location = new Point(Cursor.Position.X - 30, Cursor.Position.Y - 40);
        }
    }

enter image description here


1
做得好!+1 :)
HyperNeutrino

1
等待...。因此在C#中,您可以通过执行添加事件监听器Event += listener?太棒了:-)
ETHproductions '17

@ETHproductions能大概做,在C ++太多,如果你超载+运营商。还是很简洁。
Carcigenicate's

1

AutoHotkey,122 115字节

MsgBox
WinWait,%A_ScriptName%
Loop{
MouseGetPos,x,y
WinGetPos,a,b
ControlGetPos,c,d,,,Button1
WinMove,x+a-c,y+b-d
}

Demo


2
漂亮又简单。做得好!+1
HyperNeutrino

1

Java 7中,294个 289 286 264 220字节

import java.awt.*;public class B extends java.applet.Applet{Button b;public B(){add(b=new Button());}public void paint(Graphics g){Point p=MouseInfo.getPointerInfo().getLocation();b.setLocation(p.x-9,p.y-65);repaint();}}

多亏了MouseInfo(从Zavada偷来的),所以-22个字节我不喜欢awt库>。>

我通过删除此处的main方法节省了44个字节。如果将其作为applet启动,则不需要main方法。这可以通过eclipse的“运行为Java Applet”或禁用安全管理器并使用JDK附带的appletviewer来实现(除非您仍然能够在Web浏览器中查看applet。我认为chrome不允许这样做) 。

popup


干得好,+ 1!我同意,awt库有点丑陋,但它更具高尔夫球手性。
HyperNeutrino

您可以删除以下内容来节省24个字节import java.awt.event.*;
Zavada

@Zavada *通配符不会递归导入子包。我需要java.awt.eventMouseAdapterMouseEvent

抱歉,我的脑袋全乱了!
Zavada

1

Java中,172个 199个 235字节

打高尔夫球:

import java.awt.*;interface D{static void main(String[]z){new javax.swing.JDialog(){{setSize(9,99);setVisible(1>0);add(new Button());a();}void a(){for(Point p;;p=MouseInfo.getPointerInfo().getLocation(),setLocation(p.x-9,p.y-70));}};}}

取消高尔夫:

import java.awt.*;
interface D{
    static void main(String[]z){
        new javax.swing.JDialog(){
            {
                setSize(9,99);
                setVisible(1>0);
                add(new Button());
                a();
            }
            void a(){

                for(;;) {
                    Point p = MouseInfo.getPointerInfo().getLocation();
                    setLocation(p.x-9,p.y-70);
                }
            }
        };

    }
}

说明:我在声明新JDialog时使用了双括号初始化。通过排除JDialog的扩展名来保存字节(允许我保存publicfrom中的字节main)。在JDialog的匿名子类中,我使其可见(使用1>0代替true)和call a(),这是必要的,因为如果将循环正常放置在其中,则初始化器会出现编译时错误。我选择使用MouseInfo,而不是使用与添加鼠标侦听器相关的所有多余代码。

编辑:要增加27增加计数add(new java.awt.Button());。我本来以为JDialogs有一个隐含的按钮,但是我似乎错了。

编辑2:必须添加setSize和偏移鼠标位置以使按钮可单击。

对话


这不是很有效。我不能按下按钮,因为光标一直在窗口的左上角。请解决此问题!:)
HyperNeutrino

抱歉,现在应该修复!
Zavada '17

是的 现在工作!+1
HyperNeutrino

我不知道/记得MouseInfo是一回事。这是一个很好的答案。

1
@戳谢谢。我看到了您使用applet的答案,并且对applet产生了不合理的仇恨,所以我觉得挥杆做起来很可爱。您的答案虽然不错:)
Zavada

0

改性处理的js 102 108个字节

draw=function(){background(0);rect(mouseX,mouseY,9,9);rect(mouseX,mouseY,4,4);if(mouseIsPressed){fill(9);}};

在线尝试!刚刚更新了我的链接!

它只是绘制一个跟随鼠标的矩形,并在其中单击一个较小的矩形,该矩形变为红色。它有效,但并不令人惊讶。这个版本如果对括号和所有这些都非常严格:(


您可以删除填充周围的括号
Kritixi Lithos

@KritixiLithos我实际上不能:(
Christopher

这是严格类型的语言版本
Christopher

您可以使用其他类型的处理来保存字节,然后
Kritixi Lithos

@KritixiLithos实际上,我还必须设置屏幕大小,这会增加字节。(附带说明,我刚刚因为“内容不当,为什么不知道呢?”而被踢出了聊天。chat.stackexchange.com/messages/35631789/history是为什么
Christopher

0

Clojure,525字节

(ns d(:require[quil.core :as q][quil.middleware :as m]))(def w 500)(def h 200)(def t 30)(def n 200)(def m 100)(q/defsketch d :size[999 999]:setup(fn[](q/stroke 0 0 0)(q/text-font(q/create-font""(* t 1.3))){:x 0 :y 0}):draw(fn[{x :x y :y}](let[r q/rect f q/fill o(- x(/ w 2))p(- y (/ h 2))](q/background 99 99 99)(f 0 0 255)(r o p w h)(f 200 200 200)(r o(+ p t)w(- h t))(f 255 0 0)(r(-(+ o w)t)p t t)(f 255 255 255)(q/text"X"(- (+ o w) t)(+ p t))(r(- x(/ n 2))(- y (/ m 2))n m))):mouse-moved(fn[_ e]e):middleware[m/fun-mode])

难道不是创建一个真正的Windows对话框。取而代之的是,它会构建一个假的按钮,并在中间创建一个虚拟的按钮(不起作用)。

OP在评论中允许这样做。

使用Quil库。

(ns bits.golf.following-dialog
  (:require [quil.core :as q]
            [quil.middleware :as m]))

(def width 500)
(def height 200)

(def top-bar-height 30)

(def b-width 200)
(def b-height 100)

(defn -main []
  (q/defsketch d
    :size [999 999]
    :setup (fn []
             ; Set the border color
             (q/stroke 0 0 0)

             ; Set the font size
             (q/text-font (q/create-font "" (* top-bar-height 1.3)))

             ; The initial state
             {:x 0 :y 0})

    :draw (fn [{x :x y :y}]
            (let [r q/rect ; Shortcut functions for brevity
                  f q/fill

                  ; The top-left coordinates of the window
                  window-x  (- x (/ width 2))
                  window-y (- y (/ height 2))]

              ; Wipe the previous screen
              (q/background 99 99 99)

              ; Blue top bar
              (f 0 0 255)
              (r window-x
                 window-y
                 width height)

              ; Grey window background
              (f 200 200 200)
              (r window-x
                 (+ window-y top-bar-height)
                 width (- height top-bar-height))

              ; Red top right "button"
              (f 255 0 0)
              (r (- (+ window-x width)
                    top-bar-height)
                 window-y
                 top-bar-height top-bar-height)

              ; The X
              (f 255 255 255)
              (q/text "X" (- (+ window-x width) top-bar-height)
                          (+ window-y top-bar-height))

              ; The main "button"
              (r (- x (/ b-width 2))
                 (- y (/ b-height 2))
                 b-width
                 b-height)))

    ; When the mouse is moved, set the current state to the event object, which
    ;  conveniently has :x and :y properties
    :mouse-moved (fn [_ e] e)

    ; Needed for ease of state management. May try to factor out.
    :middleware [m/fun-mode]))

假对话框


我很想不赞成这个,因为您的代表是1000,而且看起来很好。。。我将其视为有效的解决方案(只是因为我已经验证了一个不会创建“真实”窗口),因此请设置+1。:P
HyperNeutrino

@HyperNeutrino谢谢,即使这对于任务来说是过大的杀伤力,甚至1k代表也很好。我玩得开心!也许我也将创建一个AWT版本以供比较。大概一半的长度。
Carcigenicate's

您只需等待2k即可!:D
HyperNeutrino

0

Pug / Slim + CSS / SCSS / LESS / Stylus + ES6 Javascript(98字节)

在线尝试!

帕格(10 UTF-8字节)

button#a X

触控笔(18 UTF-8字节)

#a{position:fixed}

JS(70 UTF-8字节)

onmousemove=e=>{with(a.style){top=e.clientY+'px';left=e.clientX+'px'}}

反编译,反Golfed和解释的代码段:

document.addEventListener('mousemove', function(e) {
  // Get element with an id of "id"
  var el = document.getElementById("id");
  // Set the vertical position
  el.style.top = e.clientY + 'px';
  // Set the horizontal position
  el.style.left = e.clientX + 'px';
})
/* Get element with an id of "id" */

#id {
  /* Position it relative to the viewport*/
  position: fixed;
}
<!-- A basic HTML button with an ID of "id" -->
<button id='id'>
  X
</button>


0

Mathematica 226字节

前端会跟踪相对于屏幕的鼠标位置,并且每当鼠标移动时,对话框笔记本的窗口位置就会移动。很简单,但是命令在字母方面非常冗长,就像Mathematica一样。单击确定将关闭对话框。

{s,d,w}={SetOptions,Dynamic,WindowMargins->m};With[{p=d@MousePosition["ScreenAbsolute"]},
m=d@{{p[[1]]-50,0},{0,p[[2]]-35}}];s[$FrontEndSession,FrontEndEventActions->{"MouseMoved":>s[a,w]}];a=CreateDialog[{DefaultButton[]},w];

PS完成测试后,请运行此命令删除前端选项。

SetOptions[$FrontEndSession,FrontEndEventActions->None]

屏幕动画


您可以添加gif来证明功能吗?我无法测试Mathematica。谢谢!
HyperNeutrino

-1

Javascript(ES6)+ HTML + CSS,139个字节

HTML:

<d id=d><button>X

CSS:

d{background:red;padding:9px;position:absolute

Javascript:

onmousemove=e=>{with(d.style){top=e.clientY-20+'px';left=e.clientX-20+'px'}}

由于您无法定位警报对话框,因此我在HTML中创建了自定义的超级对话框。javascript在窗口对象上注册事件处理程序,并将位置设置为事件的坐标。

在线尝试:https//jsfiddle.net/1yohswje/


我要说这是无效的,因为它不会创建类似于合法“窗口”的任何内容,而且您也无法分辨它是带有按钮的窗口。但是,这是一个有趣的解决方案!
HyperNeutrino

@HyperNeutrino我已经更新了答案。你觉得这怎么样?
corvus_192 '17

好。现在符合我的标准。+1
HyperNeutrino
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.