Utils.cs :  » GUI » LongBar » Slate » Utilities » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » LongBar 
LongBar » Slate » Utilities » Utils.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Slate.Utilities{
    public class Utils
    {
        public static Process PriorProcess()
        // Returns a System.Diagnostics.Process pointing to
        // a pre-existing process with the same name as the
        // current one, if any; or null if the current process
        // is unique.
        {
            Process curr = Process.GetCurrentProcess();
            Process[] procs = Process.GetProcessesByName(curr.ProcessName);
            foreach (Process p in procs)
            {
                if ((p.Id != curr.Id) &&
                    (p.MainModule.FileName == curr.MainModule.FileName))
                    return p;
            }
            return null;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public class DISPLAY_DEVICE
        {
            public int cb = 0;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string DeviceName = new String(' ', 32);
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string DeviceString = new String(' ', 128);
            public int StateFlags = 0;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string DeviceID = new String(' ', 128);
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string DeviceKey = new String(' ', 128);
        }

        [DllImport("user32.dll")]
        public static extern bool EnumDisplayDevices(string lpDevice,
                                                     int iDevNum, [In, Out] DISPLAY_DEVICE lpDisplayDevice, int dwFlags);

        public static string[] GetScreenFriendlyNames()
        {
            List<string> screenNames = new List<string>();
            foreach (Screen screen in Screen.AllScreens)
            {
                int dwf = 0;
                DISPLAY_DEVICE info = new DISPLAY_DEVICE();
                string monitorname = null;
                info.cb = Marshal.SizeOf(info);
                if (EnumDisplayDevices(screen.DeviceName, 0, info, dwf))
                {
                    monitorname = info.DeviceString;
                }
                if (monitorname == null)
                {
                    monitorname = "";
                }
                screenNames.Add(monitorname);
            }
            return screenNames.ToArray();
        }

        public static Screen GetScreenFromFriendlyName(string monitorname)
        {
            if (monitorname == "Primary")
                return Screen.PrimaryScreen;

            foreach (Screen screen in Screen.AllScreens)
            {
                int dwf = 0;
                DISPLAY_DEVICE info = new DISPLAY_DEVICE();
                info.cb = Marshal.SizeOf(info);
                if (EnumDisplayDevices(screen.DeviceName, 0, info, dwf))
                    if (info.DeviceString != null)
                        if (info.DeviceString == monitorname)
                            return screen;
            }
            return Screen.PrimaryScreen;
        }

        public static Screen GetScreenFromName(string monitorname)
        {
            if (monitorname == "Primary")
                return Screen.PrimaryScreen;

            foreach (Screen screen in Screen.AllScreens)
                if (screen.DeviceName == monitorname)
                    return screen;
            return Screen.PrimaryScreen;
        }

        internal static int CalculatePos(General.Sidebar.Side side)
        {
            int pos = 0;
            Screen[] screens = Screen.AllScreens;
            switch (side)
            {
                case General.Sidebar.Side.Left:
                    pos = SystemInformation.VirtualScreen.Left;
                    foreach (Screen scr in screens)
                        if (scr == General.Sidebar.screen)
                            break;
                        else
                            pos += scr.Bounds.Width;
                    break;

                case General.Sidebar.Side.Right:
                    pos = SystemInformation.VirtualScreen.Right;
                    for (int i = screens.Length - 1; i > 0; i--)
                        if (screens[i] == General.Sidebar.screen)
                            break;
                        else
                            pos -= screens[i].Bounds.Width;
                    break;
            }
            return pos;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.