विंडोज़ फॉर्म एप्लिकेशन में हाइड विंडो कैसे दिखाएं


मैं शॉर्टकट कुंजी का उपयोग करके विंडोज़ फॉर्म एप्लिकेशन में हाइड विंडो दिखाने का प्रयास कर रहा हूं। मैं छिपाने में सक्षम हूं लेकिन दिखाने में असमर्थ हूं।
मुझे सुझाव दें कि मैं कैसे करूंगा।

धन्यवाद
प्रफुल्ल

समाधान 2

पहला उत्तर

मुझे नहीं पता कि मैं आपको सही ढंग से समझ पाया या नहीं, लेकिन यहां कुछ लिंक हैं:

http://stackoverflow.com/questions/8210095/c-sharp-show-hidden-window[^]
http://www.techotopia.com/index.php/Hiding_and_Showing_Forms_in_C_Sharp[^]

[^]

दूसरा उत्तर

अपने फॉर्म में कुंजी दबाने को कैसे कैप्चर करें:
http://stackoverflow.com/questions/3001237/how-to-catch-key-press-on-a-form-c-sharp-net[^]
http://www.dreamincode.net/forums/topic/61509-capture-key-pressed/[^]
http://www.daniweb.com/software-development/csharp/threads/29646/capturing-keypresses[^]

[^]

आशा है कि उन तीन तरीकों में से कोई भी आपकी मदद करेगा…

समाधान 3

प्रिय
इन लिंक्स को ब्राउज़ करें, आपको कोड मिलेगा

C# के साथ विंडो छिपाना[^]

http://www.techotopia.com/index.php/Hiding_and_Showing_Forms_in_C_Sharp[^]

समाधान 4

ये कोशिश करें:
उपयोग ऑनकीडाउन[^] आयोजन:

सी#
private void OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
     if (e.Shift && e.KeyCode == Keys.H) 
     {
         WindowState = FormWindowState.Minimized;
     }        
}

यह पाया यहाँ[^]

समाधान 5

आपको Win32 एपीआई में एक हुक प्राप्त करने की आवश्यकता है। जैसे ही आप अपना .net फॉर्म छिपाते हैं, इवेंट और इवेंट सब्सक्राइबर काम करना बंद कर देते हैं क्योंकि वे केवल फॉर्म के कंटेनर में मौजूद होते हैं। आपको ऐसा कुछ चाहिए.

सी#
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Input;

public class GlobalHotKey : IDisposable
{
  
    public static bool RegisterHotKey(string aKeyGestureString, Action aAction)
    {
        var c = new KeyGestureConverter();
        KeyGesture aKeyGesture = (KeyGesture)c.ConvertFrom(aKeyGestureString);
        return RegisterHotKey(aKeyGesture.Modifiers, aKeyGesture.Key, aAction);
    }

    public static bool RegisterHotKey(ModifierKeys aModifier, Key aKey, Action aAction)
    {
        if (aModifier == ModifierKeys.None)
        {
            throw new ArgumentException("Modifier must not be ModifierKeys.None");
        }
        if (aAction is null)
        {
            throw new ArgumentNullException(nameof(aAction));
        }

        System.Windows.Forms.Keys aVirtualKeyCode = (System.Windows.Forms.Keys)KeyInterop.VirtualKeyFromKey(aKey);
        currentID = currentID + 1;
        bool aRegistered = RegisterHotKey(window.Handle, currentID,
                                    (uint)aModifier | MOD_NOREPEAT,
                                    (uint)aVirtualKeyCode);

        if (aRegistered)
        {
            registeredHotKeys.Add(new HotKeyWithAction(aModifier, aKey, aAction));
        }
        return aRegistered;
    }

    public void Dispose()
    {
        // unregister all the registered hot keys.
        for (int i = currentID; i > 0; i--)
        {
            UnregisterHotKey(window.Handle, i);
        }

        // dispose the inner native window.
        window.Dispose();
    }

    static GlobalHotKey()
    {
        window.KeyPressed += (s, e) =>
        {
            registeredHotKeys.ForEach(x =>
            {
                if (e.Modifier == x.Modifier && e.Key == x.Key)
                {
                    x.Action();
                }
            });
        };
    }

    private static readonly InvisibleWindowForMessages window = new InvisibleWindowForMessages();
    private static int currentID;
    private static uint MOD_NOREPEAT = 0x4000;
    private static List<HotKeyWithAction> registeredHotKeys = new List<HotKeyWithAction>();

    private class HotKeyWithAction
    {

        public HotKeyWithAction(ModifierKeys modifier, Key key, Action action)
        {
            Modifier = modifier;
            Key = key;
            Action = action;
        }

        public ModifierKeys Modifier { get; }
        public Key Key { get; }
        public Action Action { get; }
    }

    // Registers a hot key with Windows.
    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
    // Unregisters the hot key with Windows.
    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private class InvisibleWindowForMessages : System.Windows.Forms.NativeWindow, IDisposable
    {
        public InvisibleWindowForMessages()
        {
            CreateHandle(new System.Windows.Forms.CreateParams());
        }

        private static int WM_HOTKEY = 0x0312;
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == WM_HOTKEY)
            {
                var aWPFKey = KeyInterop.KeyFromVirtualKey(((int)m.LParam >> 16) & 0xFFFF);
                ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
                if (KeyPressed != null)
                {
                    KeyPressed(this, new HotKeyPressedEventArgs(modifier, aWPFKey));
                }
            }
        }

        public class HotKeyPressedEventArgs : EventArgs
        {
            private ModifierKeys _modifier;
            private Key _key;

            internal HotKeyPressedEventArgs(ModifierKeys modifier, Key key)
            {
                _modifier = modifier;
                _key = key;
            }

            public ModifierKeys Modifier
            {
                get { return _modifier; }
            }

            public Key Key
            {
                get { return _key; }
            }
        }


        public event EventHandler<HotKeyPressedEventArgs> KeyPressed;

        #region IDisposable Members

        public void Dispose()
        {
            this.DestroyHandle();
        }

        #endregion
    }
}

अब कॉल करके और प्रकार के कॉल बैक में पास करके S और H को Alt + Shift के साथ पंजीकृत करें Action.

RegisterHoitKey.

अपने कॉल बैक के लिए एक एक्शन वायर करें जो आपका फॉर्म दिखाएगा या छिपा देगा।

समाधान 6

शॉर्टकट कुंजी का उपयोग करके विंडोज फॉर्म एप्लिकेशन में एक विंडो दिखाने और छिपाने के लिए, आप शॉर्टकट कुंजी दबाए जाने पर पता लगाने के लिए कीडाउन इवेंट को संभाल सकते हैं। फिर, आप तदनुसार फ़ॉर्म की दृश्यता को टॉगल कर सकते हैं। आप इसे कैसे प्राप्त कर सकते हैं इसका एक बुनियादी उदाहरण यहां दिया गया है:

सी#
using System;
using System.Windows.Forms;

namespace YourNamespace
{
    public partial class MainForm : Form
    {
        private bool isVisible = true; // Flag to track the visibility of the form

        public MainForm()
        {
            InitializeComponent();
            this.KeyPreview = true; // Enable key events to be captured by the form
            this.KeyDown += MainForm_KeyDown; // Subscribe to the KeyDown event
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            // Check if the shortcut key (e.g., Ctrl + H) is pressed
            if (e.Control && e.KeyCode == Keys.H)
            {
                // Toggle the visibility of the form
                isVisible = !isVisible;
                
                if (isVisible)
                {
                    // Show the form
                    this.Show();
                }
                else
                {
                    // Hide the form
                    this.Hide();
                }
            }
        }
    }
}

इस उदाहरण में:

हमने कंस्ट्रक्टर में फॉर्म के कीडाउन इवेंट की सदस्यता ले ली है।
जब शॉर्टकट कुंजी (इस मामले में, Ctrl + H) दबाया जाता है, तो MainForm_KeyDown ईवेंट हैंडलर को कॉल किया जाता है।
इवेंट हैंडलर के अंदर, हम दृश्यमान ध्वज और शो() और छुपाएं() विधियों का उपयोग करके फॉर्म की दृश्यता को टॉगल करते हैं।
अपने एप्लिकेशन के वास्तविक नेमस्पेस के साथ YourNamespace को बदलना सुनिश्चित करें। आप KeyDown ईवेंट हैंडलर में स्थिति को संशोधित करके शॉर्टकट कुंजी संयोजन (इस उदाहरण में Ctrl + H) को भी अनुकूलित कर सकते हैं।

コメント

タイトルとURLをコピーしました