Sunday, November 16, 2014

Making a Dialog Topmost and Resizable.


You may have seen windows that stay on top of all others no matter what is placed on top of them (these are often tool boxes and palettes), and you may wonder how that works, The answer is that the window has been made a topmost window, and we can make our window topmost with the click of a button or initially. Besides we can hide a portion of a dialog boxes unless they are needed. The only problem with resizing our dialog window is that we might start thinking in terms of pixel measurements of widths and heights—but that means different things on different screens. In other words, if we pugged numbers into SetWindowPos() like this to contract our dialog window, those pixel measurements’ would mean different things on different resolution screens:

Dialog boxes themselves are designed in nits that attempt to avoid this problem. If you look in xxx.RC, you’ll find our main window designed this way in the resource templates:

IDD_XXX_DIALOG DIALOGEX 0, 0, 208, 105    

   STYLE DS_MODALFRAME|WS_POPUP|WS_VISIBLE|WS_CAPTION |WS_SYSMENU

EXSTYLE WS_EX_APPWINDOW

CAPTION “Your Dialog caption”

FONT 8, “MS Sans Serif”

BEGIN

       DEFPUSHBUTON    “OK”, IDOK, 110, 10, 48, 17
       PUSHBUTTON      “Cancel” IDCANCEL, 110, 25, 48, 17

END

 This measurements are in special dialog-box units instead of pixels. The horizontal unit is one quarter of the average character width for the dialog box’s font, and the vertical unit is one eighth of the average character height. In this way, dialog boxes adjust themselves to the screen on which they appear.

OK now start the tutorial.

       1.       Create an MFC Dialog based Program
      2.       Add following controls (as on the picture). Or any other control you want.


      3.       Add 3 private member variable to “BlogTest1Dlg.h” file as follow

private:
       CRect m_rectWindow;
       CRect m_rectButton;
       bool m_bResized = false;
      4.       Now double click on Resize & Make Topmost button and add the following code.

void CBlogTest1Dlg::OnBnClickedButton1()
{
   if(!m_bResized)
   {
      // Get Windows Rectangle: x, y, cx, cy
      GetWindowRect(&m_rectWindow); 
      // Get Buttons Rectangle: x, y, cx, cy
      GetDlgItem(IDC_BUTTON1)->GetWindowRect(&m_rectButton);  
      SetWindowPos(&CWnd::wndTopMost, // it can be NULL as well.
                     /*int x*/m_rectWindow.left,
                     /*int y*/m_rectWindow.top,
                     /*int cx*/m_rectButton.right -                        m_rectWindow.left +(m_rectButton.left -
                       m_rectWindow.left),
                     /*int cy*/m_rectButton.bottom -
                       m_rectWindow.top + (m_rectButton.left -
                       m_rectWindow.left),
                       SWP_NOMOVE | SWP_FRAMECHANGED);
       // change the boolean value to true for the next click to
       // change back the window normal position.
         m_bResized = true;
       }
       else
       {
          SetWindowPos(&CWnd::wndNoTopMost, m_rectWindow.left,
             m_rectWindow.top, m_rectWindow.right-m_rectWindow.left,
             m_rectWindow.bottom - m_rectWindow.top, 
             SWP_NOMOVE|SWP_FRAMECHANGED);
          SendMessage(DM_REPOSITION, 0, 0);
          // change the boolean value to false for the next click to
          // change window to compact position.
          m_bResized = false;
       }
}

 

5.       Now press F5 and Run the program.

When you click the Button “Resize & Make Topmost” It will change the window like below.



Set the window on its original size as the button clicked again.
 

No comments:

Post a Comment