示例#1
0
        private string GetCaption(uint index)
        {
            uint length = (uint)SendMessage(this._handle, SB_GETTEXTLENGTH, index, 0);

            // Low part is the count. High part is the window type. Mask out the high bits.
            // The returned text will also be unicode so double the length to accomodate our buffer
            length = (length & 0x0000ffff) * 2;

            IntPtr hProcess  = IntPtr.Zero;
            IntPtr allocated = IntPtr.Zero;

            try
            {
                hProcess = StatusHelper.OpenProcess(ProcessAccessTypes.PROCESS_ALL_ACCESS, false, this.OwningPID);
                if (hProcess != IntPtr.Zero)
                {
                    // Allocate memory in the remote process
                    allocated = StatusHelper.VirtualAllocEx(hProcess, IntPtr.Zero, length, (VirtualAllocExTypes.MEM_COMMIT_OR_RESERVE), AccessProtectionFlags.PAGE_READWRITE);

                    if (allocated != IntPtr.Zero)
                    {
                        uint   bytesRead = 0;
                        byte[] buffer    = new byte[length];

                        // SB_GETTEXT tells the remote process to write out text to the remote memory we allocated.
                        StatusHelper.SendMessage(this._handle, SB_GETTEXT, (IntPtr)index, allocated);

                        // Now we need to read that memory from the remote process into a local buffer.
                        bool success = StatusHelper.ReadProcessMemory(hProcess, allocated, buffer, length, out bytesRead);

                        if (success)
                        {
                            // Each char takes 2 bytes.
                            char[] characters = new char[length / 2];

                            for (int i = 0; i < buffer.Length; i = i + 2)
                            {
                                // Even though the second byte will probably always be 0 for en-us let's so a bit shift
                                // then "or" the first and second bytes together before casting to char.
                                uint a = (uint)buffer[i];
                                uint b = (uint)buffer[i + 1] << 8;

                                characters[i / 2] = (char)(a | b);
                            }

                            return(new string(characters));
                        }
                    }
                }
            }
            finally
            {
                if (hProcess != IntPtr.Zero)
                {
                    if (allocated != IntPtr.Zero)
                    {
                        // Free the memory in the remote process
                        StatusHelper.VirtualFreeEx(hProcess, allocated, 0, VirtualAllocExTypes.MEM_RELEASE);
                    }

                    // Close the process handle
                    StatusHelper.CloseHandle(hProcess);
                }
            }

            return(string.Empty);
        }
            // public bool FlatCalcDone = false;
            //   public bool SlaveFlatCalcDone = false;
            private void CalculateFlatExp()//calcflatexphere
            {

            if (IsSlave())
                textBox41.Clear();
            toolStripStatusLabel1.Text = "Calculating Flat Exposure";
            this.Refresh();
            if ((IsSlave()) & (FlatExp == 0))//allows second set of flats to start at calculated time
                FlatExp = Convert.ToDouble(textBox40.Text) / 1000;
            if (FlatExp == 0)
                FlatExp = (double)numericUpDown34.Value / 1000;
            MaxADU = 0;

            fileSystemWatcher4.EnableRaisingEvents = false;
            /*
            if (NebVNumber == 3)
                CaptureTime3 = (double)CaptureTime / 1000;//need to allow fractions for flats.
               */
            // CaptureTime3 = FlatExp;

            tol = 1 + (Convert.ToDouble(textBox39.Text.ToString()) / 100);
            FlatGoal = Convert.ToInt32(textBox38.Text.ToString()) * 1000;
            //    if (NebListenOn == false)
            NebListenStart(Handles.NebhWnd, SocketPort);
            while ((MaxADU > FlatGoal * tol) || (MaxADU < FlatGoal / tol))
            {
                textBox41.Refresh();

                //  toolStripStatusLabel1.Text = "Capturing";
                //   this.Refresh();
                string prefix = textBox19.Text.ToString();
                //  NetworkStream serverStream;
                if (IsSlave())
                    serverStream = clientSocket2.GetStream();
                else
                    serverStream = clientSocket.GetStream();
                int subs = 1;
                string name = "FlatCalc";
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes("setname " + prefix + name + "\n" + "setbinning " + CaptureBin + "\n" + "SetShutter 0" + "\n" + "SetDuration " + FlatExp + "\n" + "Capture " + subs + "\n");
                try
                {
                    Capturing = true;
                    serverStream.Write(outStream, 0, outStream.Length);
                    Thread.Sleep(500);//wait to check until after starts capturing
                    while (Capturing == true)
                    {
                        int StatusstripHandle = FindWindowEx(Handles.NebhWnd, 0, "msctls_statusbar32", null);

                        //    from   http://www.pinvoke.net/default.aspx/user32/SB_GETTEXT.html
                        IntPtr statusHandle = new IntPtr(StatusstripHandle);
                        StatusHelper sh = new StatusHelper(statusHandle);
                        string[] captions = sh.Captions;
                        if (captions[0] == "Sequence done")
                            Capturing = false;
                    }

                }

                catch
                {
                    MessageBox.Show("Error sending command", "scopefocus");
                    return;

                }

                serverStream.Flush();
                //  Thread.Sleep(5000);//depends on download time.  may be a way to wait for change of MaxADU
                //   Thread.Sleep((int)FlatExp * 1000);

                FindADU();
                Log("MaxADU " + MaxADU.ToString());
                if ((MaxADU < FlatGoal * tol) && (MaxADU > FlatGoal / tol))//this gets out fo the loop before adusting exp time
                {
                    if (IsSlave())
                    {
                        textBox41.Text = "Slave FlatCalc Complete";
                        textBox41.Refresh();
                    }
                    Log("Flat Calc Complete--MaxADU: " + MaxADU.ToString() + "   Exposure Time: " + FlatExp.ToString());

                    CaptureTime3 = FlatExp;
                    FlatCalcDone = true;
                    break;
                }
                ADUratio = (double)FlatGoal / (double)MaxADU;
                FlatExp = FlatExp * ADUratio;

            }
            /*
            if ((IsSlave()) & (checkBox21.Checked == true))
            {
                textBox41.Text = "Slave FlatCalc Complete";
                //textBox41.Refresh();
            }
            */
            if (IsServer())
            {

                while (working == true)
                {

                    StringBuilder sb = new StringBuilder(1024);
                    SendMessage(Handles.SlaveStatushwnd, WM_GETTEXT, 1024, sb);
                    //  GetWindowText(camera, sb, sb.Capacity);
                    //  Log("SlaveStatus= " + sb.ToString());
                    if (sb.ToString() == "Slave FlatCalc Complete")
                        working = false;
                    Thread.Sleep(20);
                }
                working = true;
            }

            //  fileSystemWatcher4.EnableRaisingEvents = true;  //rem'd 5-19

            //******below moved to before break above*************
            /*
            Log("Flat Calc Complete--MaxADU: " + MaxADU.ToString() + "   Exposure Time: " + FlatExp.ToString());
            CaptureTime3 = FlatExp;
            FlatCalcDone = true;
             */
            //***************************************
            //    NebCapture();****rem'd 5-19
            //   NebListenStop();
            //  return;
            //   SlaveFlatCalcDone = false;

            }
            private void WaitForSequenceDone(string Statusstring, int handle)
            {

            //Thread.Sleep(500);
            // StatusLabel = Statusstring;
            //  Workerhwnd = handle;

            //  Log("Waiting for Slave");
            //   while (working)
            //   {

            StatusstripHandle = FindWindowEx(handle, 0, "msctls_statusbar32", null);

            //    from   http://www.pinvoke.net/default.aspx/user32/SB_GETTEXT.html
            IntPtr statusHandle = new IntPtr(StatusstripHandle);
            StatusHelper sh = new StatusHelper(statusHandle);
            captions = sh.Captions;
            //    Log("Caption[0]= " + captions[0].ToString());
            if (captions[0] == Statusstring)
            {
                working = false;
            }

            this.Refresh();
            /* *****remd 6-20

                         int StatusstripHandle = FindWindowEx(NebhWnd, 0, "msctls_statusbar32", null);

                               //    from   http://www.pinvoke.net/default.aspx/user32/SB_GETTEXT.html
                               IntPtr statusHandle = new IntPtr(StatusstripHandle);
                               StatusHelper sh = new StatusHelper(statusHandle);
                               string[] captions = sh.Captions;
                               while (captions[0] != "Sequence done")
                               {
                                   Thread.Sleep(100);
                               }
                        */
            //  }
            //  Log("Slave process complete");

            }
            private void FindNebCamera()
            {
            try
            {

                int StatusstripHandle = FindWindowEx(Handles.NebhWnd, 0, "msctls_statusbar32", null);

                IntPtr statusHandle = new IntPtr(StatusstripHandle);
                StatusHelper sh = new StatusHelper(statusHandle);
                // string[] captions = sh.Captions;
                if (sh.Captions[0] == "" || sh.Captions[0].Substring(0, 6) == "No cam") //"" if non selected after opening neb, the later is no camera specifically selected
                    NoCameraSelected();
                else
                {
                    Log(sh.Captions[0]);

                    /*
                    ******2-12-14 this no longer works for some reason.  changes to use statusbar instead  ********
                                    int panel3 = FindWindowByIndexName(Handles.NebhWnd, 4, "panel");// try panelhwnd
                                    //  Log("panel3 found" + panel3.ToString());
                                    int panel4 = FindWindowByIndexName(panel3, 1, "panel");//panel 4 is small one in panel 3, camera is 1st child in this
                                    //   Log("found panel4" + panel4);
                                    int camera = FindWindowByIndex(panel4, 1, null);
                                    //    Log("Found camera" + camera.ToString());
                                    StringBuilder sb = new StringBuilder(1024);
                                    SendMessage(camera, WM_GETTEXT, 1024, sb);
                                    //  GetWindowText(camera, sb, sb.Capacity);
                                    Log("Camera " + sb.ToString());
                     */
                    if (Handles.NebVNumber == 4)
                    {
                        int length = sh.Captions[0].Length;
                        string sb3 = sh.Captions[0].Remove(length - 10, 10);
                        GlobalVariables.Nebcamera = sb3.ToString();
                        textBox22.Text = GlobalVariables.Nebcamera.ToString();
                    }
                    else
                    {
                        string sb1 = sh.Captions[0].Remove(0, 7);
                        int indexofSpace = sb1.IndexOf(' ');
                        string sb2 = sb1.Substring(0, indexofSpace);
                        GlobalVariables.Nebcamera = sb2.ToString();
                        textBox22.Text = GlobalVariables.Nebcamera.ToString();
                    }
                }
                /*

                if (GlobalVariables.Nebcamera == null)
                {
                    NoCameraSelected();
                }
                 */

            }
            catch (Exception ex)
            {
                Log("Error finding Neb Camera");
                //  Send("FindNebCamera Error" + ex.ToString());
                FileLog("FindNebCamera Error" + ex.ToString());

            }
            }
            private void MonitorNebStatus()
            {
            try
            {
                int StatusstripHandle = FindWindowEx(Handles.NebhWnd, 0, "msctls_statusbar32", null);

                //    from   http://www.pinvoke.net/default.aspx/user32/SB_GETTEXT.html
                IntPtr statusHandle = new IntPtr(StatusstripHandle);
                StatusHelper sh = new StatusHelper(statusHandle);
                string[] captions = sh.Captions;
                if (captions[0] != "")
                {
                    if (captions[0] == "Sequence done")
                    {
                        Capturing = false;
                        backgroundWorker2.CancelAsync();
                    }
                    if (captions[0].Length > 8)
                    {
                        if ((captions[0].Substring(0, 10) == "Requesting") || (captions[0].Substring(0, 10) == "DITHER: Wa") && (!flipCheckDone))
                            CheckForFlip();
                        if ((captions[0].Substring(0, 10) == "Requesting") && (flipCheckDone))
                            toolStripStatusLabel1.Text = "Waiting for Dither";
                        if (captions[0].Substring(0, 8) == "Sequence")
                            toolStripStatusLabel1.Text = "Capturing";
                    }
                }

            }
            catch (Exception e)
            {
                Log("NebStatusMonitor error " + e.ToString());
                FileLog("NebStatusMonitor error " + e.ToString());
            }
            }