Windows Develop Bookmark and Share   
 index > Windows Forms General > keeping focus on the main window
 

keeping focus on the main window

I have a borderless window that I want to be able to show and hide from the main app window. When I show it (it's called from a menu item) the focus shifts to the borderless window. To close it, I have to click to get focus on the main window, then click again to open the menu so I can select the hide item. The borderless window is set to stay on top of the main app window.

How do I keep focus on the main app window even while the borderless window is open?

The code for showing/hiding the borderless window:
private: System::Void previewMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
               if (!previewWnd->open) {
                    previewWnd->open = true;
                    previewWnd->Visible = true;
                    previewMenuItem->Enabled = false;
                    stopPreviewMenuItem->Enabled = true;
               }
          }
private: System::Void stopPreviewMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
               if (previewWnd->open) {
                    previewWnd->open = false;
                    previewWnd->Visible = false;
                    previewMenuItem->Enabled = true;
                    stopPreviewMenuItem->Enabled = false;
               }
          }

 

The code for adding the borderless window to the main app:
     public ref class Form1 : public System::Windows::Forms::Form      {
     private:
               Preview ^previewWnd;

     public:
          Form1(void)
          {
               InitializeComponent();
               previewWnd = gcnew Preview;
               //
               //TODO: Add the constructor code here
               //
          }

I'm not sure what to put in the destructor, since Preview.h has a destructor in it already:

     protected:
          ///
          /// Clean up any resources being used.
          ///

          ~Form1()
          {
               if (components)
               {
                    delete components;
               }
          }
     .
     .
     .
     }
Wayne.C  Monday, November 27, 2006 1:24 AM

You will need to override the CreateParams property and set the WS_EX_TOOLWINDOW style on the window (you may want to consider making this a TOPMOST window as well if you're not already setting this style).

protected override CreateParams CreateParams

{

get

{

CreateParams cp = base.CreateParams;

cp.ExStyle |= (int)(0x00000080);

return cp;

}

}

Thanks,

Scott McCraw  Wednesday, December 06, 2006 4:46 AM

You can use google to search for other answers

Custom Search

More Threads

• How to make a description appear when cursor is over a button?
• Information of installed font - more than InstalledFontsCollection.
• Passing Form object as paramter to a function in a class
• calculating dateTime (now and endDate)
• Control Cannot flow??
• using threading for high performance
• Mouse events entering and leaving forms
• Invoke Procedures
• customize menustrip
• SelectNextControl backwards in nested user controls