none
trigger event 一問 RRS feed

  • 問題

  • 各位前輩,你好!有trigger event 一問.

    第一個class

    public TrackRubberBand r = new TrackRubberBand();

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {                       
          Point pt = new Point(e.X, e.Y);             
          r.RubberBand(this, pt);
    }

     private void Form1_MouseUp(object sender, MouseEventArgs e)
     {
           MessageBox.Show("hihi");
     }

    第二個class

     public bool RubberBand(Control control, Point point)        
            {
                bool ok = true;

                rect = new Rectangle(point.X, point.Y, point.X, point.Y);

                Graphics g = control.CreateGraphics();

                Point pt1, pt2;
                pt1 = control.PointToScreen(point);
                pt2 = pt1;

                DrawRectangle(pt1, pt2);

                WinAPI.SetCapture(control.Handle);
                while (true)
                {
                    MSG msg = new MSG();
                   

                    WinAPI.GetMessage(msg, IntPtr.Zero, 0, 0);

                    switch (msg.message)
                    {
                        case WM_MOUSEMOVE:
                            {
                                DrawRectangle(pt1, pt2);                          
                               
                                pt2 = new Point(msg.x, msg.y);
                                  
                                pt2 = AdjustPt2(pt1, pt2);

                                DrawRectangle(pt1, pt2);

                                break;
                            }

                        case WM_RBUTTONUP:
                        case WM_MBUTTONUP:
                        case WM_LBUTTONUP:
                            {
                                goto ExitLoop;
                            }

                        case WM_KEYDOWN:
                            {
                                if (msg.wParam == VK_ESCAPE)
                                {
                                    ok = false;
                                    goto ExitLoop;
                                }
                                break;
                            }

                        case WM_LBUTTONDOWN:
                        case WM_MBUTTONDOWN:
                        case WM_RBUTTONDOWN:
                            break;
                    }
                }

            ExitLoop:
                {
                    DrawRectangle(pt1, pt2);                         
                }

                WinAPI.ReleaseCapture();

                g.Dispose();

                if (ok)
                    rect = new Rectangle(pt1, new Size(pt2.X - pt1.X, pt2.Y - pt1.Y));

                return ok;    
            }

    I go into "Form1_MouseDown", call "r.RubberBand(this, pt);", then it goes into another class. In TrackRubberBand() class, it has another "mousemove","mouseup" event.

    So, when I mouses down in form1, it calls "TrackRubberBand() ", then I mouses move and then mouse up in "TrackRubberBand() ".  But I can not trigger the event "MessageBox.Show("hihi");" immedately. I need to click the form1
    one time, then it show the messgae box.

    How can I trigger the "Form1_MouseUp" event without clicking the form1 immedately?

    thx for your help!!


    2008年7月31日 上午 08:20

解答

所有回覆

  • HI,

     

    The source code you post was incomplete, so can't get the problem you encounter

     

    tihs

    2008年8月1日 上午 03:34
  • Hi,

    sorry, My part of the program is shown below.


    form1.cs

    public TrackRubberBand r = new TrackRubberBand();
    public Point prevLeftClick = new Point(0, 0);
    public Point nextLeftClick = new Point(0, 0);

    public class CustomButton
            {
                public Button test_button;
                public bool IsBound;
                public RectTracker CSharpTracker;
                public bool Inpanel;
                public int panel_no;

                public CustomButton()
                {
                    test_button = new Button();
                    CSharpTracker = new RectTracker();
                }
            }

            public CustomButton[] m_CustomButton;
            public int CustomButton_valid = 5;

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
                for (int i = 0; i < CustomButton_valid; i++)
                {
                    m_CustomButtonIdea.test_button.Text = "yyy";                                   //change the text      
                  
                }

                Point pt = new Point(e.X, e.Y);


      r.RubberBand(this, pt);                                                                      // draw TrackRubberBand(虛線框框)

                prevLeftClick = r.temp_pt1;
                nextLeftClick = r.temp_pt2;  


      for (int i = 0; i < CustomButton_valid; i++)
                {
                    m_CustomButtonIdea.test_button.Text = "XXX";                                
    //change the text
                }     
    }

     TrackRubberBand.cs
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Drawing.Drawing2D;

    namespace testing
    {
        /// <summary>
        ///
        /// </summary>
        public class TrackRubberBand
        {

            #region private attribute
            private Rectangle rect;
            public Point pt1, pt2;
            public Point temp_pt1, temp_pt2;
            private Size minSize;
         
            private const uint WM_MOUSEFIRST = 0x0200;
            private const uint WM_MOUSEMOVE = 0x0200;
            private const uint WM_LBUTTONDOWN = 0x0201;
            private const uint WM_LBUTTONUP = 0x0202;
            private const uint WM_LBUTTONDBLCLK = 0x0203;
            private const uint WM_RBUTTONDOWN = 0x0204;
            private const uint WM_RBUTTONUP = 0x0205;
            private const uint WM_RBUTTONDBLCLK = 0x0206;
            private const uint WM_MBUTTONDOWN = 0x0207;
            private const uint WM_MBUTTONUP = 0x0208;
            private const uint WM_MBUTTONDBLCLK = 0x0209;
            private const uint WM_KEYDOWN = 0x0100;
            private const uint VK_ESCAPE = 0x1B;

            #endregion

           
            public TrackRubberBand()
            {
                minSize = new Size(10, 10);
            }

            public bool RubberBand(Control control, Point point)         //form1, pt, true
            {
                bool ok = true;

                rect = new Rectangle(point.X, point.Y, point.X, point.Y);

                Graphics g = control.CreateGraphics();

                Point pt1, pt2;
                pt1 = control.PointToScreen(point);
                pt2 = pt1;

                DrawRectangle(pt1, pt2);

                WinAPI.SetCapture(control.Handle);
                while (true)
                {
                    MSG msg = new MSG();
                   

                    WinAPI.GetMessage(msg, IntPtr.Zero, 0, 0);

                    switch (msg.message)
                    {
                        case WM_MOUSEMOVE:
                            {
                                DrawRectangle(pt1, pt2);                          
                               
                                pt2 = new Point(msg.x, msg.y);
                                  
                                pt2 = AdjustPt2(pt1, pt2);

                                DrawRectangle(pt1, pt2);

                                break;
                            }

                        case WM_RBUTTONUP:
                        case WM_MBUTTONUP:
                        case WM_LBUTTONUP:
                            {
                                goto ExitLoop;
                            }
                           
                        case WM_KEYDOWN:
                            {
                                if (msg.wParam == VK_ESCAPE)
                                {
                                    ok = false;
                                    goto ExitLoop;
                                }
                                break;
                            }

                        case WM_LBUTTONDOWN:
                        case WM_MBUTTONDOWN:
                        case WM_RBUTTONDOWN:
                            break;
                    }
                }

            ExitLoop:
                {
                    DrawRectangle(pt1, pt2);
                   
                 //   MessageBox.Show(pt1.X + " " + pt1.Y + " " + pt2.X + " " + pt2.Y);

                    POINTAPI pt_1 = new POINTAPI();
                    POINTAPI pt_2 = new POINTAPI();

                    pt_1.api = pt1;
                    pt_2.api = pt2;

                   
                    WinAPI.ScreenToClient(IntPtr.Zero, ref pt_1);
                    WinAPI.ScreenToClient(IntPtr.Zero, ref pt_2);

                    temp_pt1 = control.PointToClient(pt_1.api);
                    temp_pt2 = control.PointToClient(pt_2.api);

                 //   MessageBox.Show(temp_pt1.X + " " + temp_pt1.Y + " " + temp_pt2.X + " " + temp_pt2.Y);
                             
                }

                WinAPI.ReleaseCapture();

                g.Dispose();

                if (ok)
                    rect = new Rectangle(pt1, new Size(pt2.X - pt1.X, pt2.Y - pt1.Y));

                return ok;

             
            }

            private Point AdjustPt2(Point pt1, Point pt2)
            {
                int dx = pt2.X - pt1.X;
                int dy = pt2.Y - pt1.Y;
                if (Math.Abs(dx) < minSize.Width)
                {
                    if (dx < 0)
                        dx = -minSize.Width;
                    else
                        dx = minSize.Width;
                }
                if (Math.Abs(dy) < minSize.Height)
                {
                    if (dy < 0)
                        dy = -minSize.Height;
                    else
                        dy = minSize.Height;
                }
                Size sz = new Size(dx, dy);
                pt2 = pt1 + sz;

                return pt2;

            }

            private void DrawRectangle(Point pt1, Point pt2)
            {
                Rectangle rc = new Rectangle(pt1, new Size(pt2.X - pt1.X, pt2.Y - pt1.Y));
                ControlPaint.DrawReversibleFrame(rc, Color.Black, FrameStyle.Dashed);    //draw虛線 frame 及 重新resize frame

            }

            #region Windows API
            [StructLayout(LayoutKind.Sequential)]
            internal class MSG
            {
                internal UIntPtr hwnd;
                internal UInt32 message;
                internal UInt32 wParam;
                internal UInt32 lParam;
                internal UInt32 time;
                internal Int32 x;
                internal Int32 y;       
               
            }

            public struct POINTAPI
            {       
                internal Point api;

            }
          
            internal class WinAPI
            {
                public static bool ScreenToClient(IntPtr hwnd, ref POINTAPI lpPoint)
                {
                    return Convert.ToBoolean(_ScreenToClient(hwnd, ref lpPoint));
                }

                public static bool GetMessage(MSG msg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax)
                {
                    return Convert.ToBoolean(_GetMessage(msg, hWnd, wMsgFilterMin, wMsgFilterMax));
                }

                public static IntPtr SetCapture(IntPtr hWnd)
                {
                    return _SetCapture(hWnd);
                }

                public static bool ReleaseCapture()
                {
                    return Convert.ToBoolean(_ReleaseCapture());
                }

              
                [DllImport("user32.dll", EntryPoint = "GetMessageA", SetLastError = true)]
                private static extern int _GetMessage(MSG msg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);

                [DllImport("user32.dll", EntryPoint = "SetCapture", SetLastError = true)]
                private static extern IntPtr _SetCapture(IntPtr hWnd);

                [DllImport("user32.dll", EntryPoint = "ReleaseCapture", SetLastError = true)]
                private static extern int _ReleaseCapture();

                [DllImport("user32.dll", EntryPoint = "ScreenToClient", SetLastError = true)]
                public static extern int _ScreenToClient(IntPtr hwnd, ref POINTAPI lpPoint);

            }
            #endregion

        }
    }

    The exact problem is that when TrackRubberBand(虛線框框) selects the button by mouse down, it changes the text "xxx". After that, when  mouse is  down again , the text does not changed to "yyy".

    The logic flow is "
    change the text "yyy", then "draw TrackRubberBand(虛線框框)",then ""change the text "xxx". But the program can not "change the text "yyy",

    各位前輩,thx for your help!


    2008年8月1日 上午 04:22
  • thx a lot. The problem is solved

     

    2008年8月1日 下午 12:43