Solved

Make Window invisible

Posted on 2001-06-06
8
315 Views
Last Modified: 2013-11-20
I want to create a program that when executed, the dialog box does not appear.  I unchecked the visible box in the propeties, but it is still visible.

Brez
0
Comment
Question by:Brez
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 4

Expert Comment

by:captainkirk
ID: 6160972
how are you creating the dialog? Are you calling ShowWindow() or DoModal()?
0
 
LVL 32

Expert Comment

by:jhance
ID: 6160980
If you call ShowWindow(SW_HIDE) it will be invisible.
0
 

Expert Comment

by:Dak_Programmer
ID: 6161236
Go into the dialogs initdialog function and call ShowWindow(SW_HIDE)
0
 
LVL 32

Expert Comment

by:jhance
ID: 6161252
Gosh there is a nasty echo on this question.....

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Brez
ID: 6161539
I used the wizard to create a dialog based application. What I want is to make the default dialog box invisible upon startup.  I only want to see an icon in the Status Area of the taskbar.  Only after clicking on the icon do I want the dialog box to appear.  ShowWindow(SW_HIDE) is no good because the application still appears in the taskbar. I tried to use ShowWindow(FALSE) in the Initdialog, but it will not become invisible.  If I call ShowWindow(FALSE) after the Initdialog is complete, then the window will become invisible. I hope this make sense.
0
 

Accepted Solution

by:
Dak_Programmer earned 50 total points
ID: 6161557
MFC dialog-based apps default to using a modal dialog, which causes problems if your application requires to be invisible. This situation most commonly occurs when developing applications which place icons in the system tray, and therefore need their main dialog to be invisible until the user interacts with the icon. You can make a modeless dialog version of your app as follows :

  Make your dialog resource invisible using the styles tab in the dialog properties.
  Remove all the DoModal gunk from your InitInstance, so you end up with something like this :
  BOOL CMyApp::InitInstance()
{
   #ifdef _AFXDLL
    Enable3dControls(); // using MFC in a shared DLL
   #else
    Enable3dControlsStatic();    // linking to MFC statically
   #endif

   m_pMainWnd = new CMyModelessDlg;

   if (m_pMainWnd)
      return TRUE;
   else
      return FALSE;
}
 
   
  I place the call to Create (...) in my dialog constructor and make it public. Other people organise things differently but it works for me... curse the OOP thought police <g> :
  CMyModelessDlg::CMyModelessDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMyModelessDlg::IDD, pParent)
{
   //{{AFX_DATA_INIT(CMyModelessDlg)
   //}}AFX_DATA_INIT
   m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

   Create (IDD, pParent); // Create modeless dialog.
}
 
   
  You will need to handle PostNcDestroy and delete the dialog object yourself (ClassWizard can set up the handler for you) :
  void CMyModelessDlg::PostNcDestroy()
{
   delete this;
}
 
   
  You may find that if you call DestroyWindow from within your dialog in order to shut down, you will have to call your own PostNcDestroy in order to avoid a leak. Watch the debug output from your app running in debug mode - if this is the case MFC will helpfully give you a warning there. Here's the normal WM_CLOSE handler:
void CMyModelessDlg::OnClose()
{
   if (m_bOkToClose)
   {
      CleanUp ();
      DestroyWindow ();
   }
   else
   {
      ShowWindow (SW_HIDE);
   }
}

I use the m_bOkToClose flag to distinguish when I'm permitting the user to close down and when I want that action to simply hide the window. Cleanup is a function that does any necessary CloseHandles, removes any tray icons and deletes any allocated memory.
 

Now you have a dialog based application that can be shown or hidden as necessary.


0
 

Expert Comment

by:Dak_Programmer
ID: 6161576
. How do I put an icon in the system tray ?


 This loads the systray icon, giving it an ID because I have more than one
tray icon. The private message is also specified.

void CMainFrame::AddMyIcon (void)
{
   NOTIFYICONDATA tnid;
   CString csTip ;

   tnid.cbSize = sizeof(NOTIFYICONDATA);
   tnid.hWnd = GetSafeHwnd();
   tnid.uID = SYSTRAY_QUICK_ID; // personal ID
   tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
   tnid.uCallbackMessage = PRIV_SYSTRAY_ICON;
   tnid.hIcon = m_hIconPhone ;

   if (csTip.LoadString (IDS_QUICKMENU))
      strncpy (tnid.szTip, csTip, sizeof(tnid.szTip));
   else
      tnid.szTip[0] = '\0';

   Shell_NotifyIcon(NIM_ADD, &tnid);
}

 This appears in the mainframe message map, and specifies a message sent when the user
interacts with the icon. Note that the message ID matches that specified in the uCallbackMessage
structure member.

ON_MESSAGE (PRIV_SYSTRAY_ICON, OnSysTrayIconClick)

 This handles the click message declared above.

afx_msg LONG CMainFrame::OnSysTrayIconClick (WPARAM wParam, LPARAM lParam)
{
   switch (wParam)
   {
      // bunch of other code omitted here. Note that the
      // ID here matches that specified when the icon
      // was inserted, in the uID structure member.

      case SYSTRAY_QUICK_ID:

           switch (lParam)
           {
              case WM_LBUTTONDOWN:
              case WM_RBUTTONDOWN:
                   ShowQuickMenu ();
                   break ;
           }
           break ;

      default:
           DebugMessage ("......");
           break ;
   }

   return TRUE ;
}

 This shows the menu for the systray icon. The popup menu is loaded elsewhere (and it MUST be a popup, or you'll get a bizarre minimal-width empty menu appearing). You can create a single bar menu with several popups, and use GetSubMenu to choose the one you want for any particular circumstance.

void CMainFrame::ShowQuickMenu ()
{
   POINT CurPos ;

   GetCursorPos (&CurPos);
   SetForegroundWindow ();// Bodge to get around bug in Explorer

   // Display the menu. This menu is a popup loaded elsewhere.

   TrackPopupMenu (m_hQuickMenu,
                   TPM_LEFTBUTTON,
                   CurPos.x,
                   CurPos.y,
                   0,
                   GetSafeHwnd(),
                   NULL);

   PostMessage (WM_NULL, 0, 0);// Bodge to get around bug in Explorer
}



0
 

Author Comment

by:Brez
ID: 6161968
Thanks you very much.  That was just what I was looking for.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 31
xyBalance chalenge 58 88
ffmpeg - "rtsp://...... Operation not permitted" 4 69
Sed question 2 48
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now