Placing the dialog bar
-
Please suggest me with ideas and code to place a dialog bar in the middle of a window.
-
Please suggest me with ideas and code to place a dialog bar in the middle of a window.
-
Use GetClientRect function | Microsoft Docs[^] to get the dimensions of the Window(s). Simple maths will calculate the X & Y points for the centre.
Thanks for the reply. Which function is used to reposition the dialogbar. SetWindowPos is not making any changes in the position for the dialogbar. Please advice.
-
Thanks for the reply. Which function is used to reposition the dialogbar. SetWindowPos is not making any changes in the position for the dialogbar. Please advice.
-
It's not doing any changes. m_SysWnd.MoveWindow(100,100,100,100,true); //New positions Am I missing anything?
-
It's not doing any changes. m_SysWnd.MoveWindow(100,100,100,100,true); //New positions Am I missing anything?
manoharbalu wrote:
Am I missing anything?
Without seeing all the relevant code, we would be guessing at best. What is
m_SysWnd
? Where are you callingMoveWindow()
from?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
It's not doing any changes. m_SysWnd.MoveWindow(100,100,100,100,true); //New positions Am I missing anything?
-
It's not doing any changes. m_SysWnd.MoveWindow(100,100,100,100,true); //New positions Am I missing anything?
Here is what I have:
RECT rectParent, rectDialog;
SIZE sizeParent, sizeDialog;
POINT point;// get the size of the dialog
GetWindowRect(hDialog, &rectDialog);
sizeDialog.cx = rectDialog.right - rectDialog.left;
sizeDialog.cy = rectDialog.bottom - rectDialog.top;// get the size of the parent Window's client area
GetClientRect(GetParent(hDialog), &rectParent);
sizeParent.cx = rectParent.right - rectParent.left;
sizeParent.cy = rectParent.bottom - rectParent.top;// parent's sizes minus dialog's sizes divided by 2
// give the coordinates of the centre position
point.x = (sizeParent.cx - sizeDialog.cx) / 2;
point.y = (sizeParent.cy - sizeDialog.cy) / 2;
// we need to make parent coordinates relative to the screen
ClientToScreen(GetParent(hDialog), &point);// now move the Window to the new position
MoveWindow(hDialog, point.x, point.y, sizeDialog.cx, sizeDialog.cy, true); -
Here is what I have:
RECT rectParent, rectDialog;
SIZE sizeParent, sizeDialog;
POINT point;// get the size of the dialog
GetWindowRect(hDialog, &rectDialog);
sizeDialog.cx = rectDialog.right - rectDialog.left;
sizeDialog.cy = rectDialog.bottom - rectDialog.top;// get the size of the parent Window's client area
GetClientRect(GetParent(hDialog), &rectParent);
sizeParent.cx = rectParent.right - rectParent.left;
sizeParent.cy = rectParent.bottom - rectParent.top;// parent's sizes minus dialog's sizes divided by 2
// give the coordinates of the centre position
point.x = (sizeParent.cx - sizeDialog.cx) / 2;
point.y = (sizeParent.cy - sizeDialog.cy) / 2;
// we need to make parent coordinates relative to the screen
ClientToScreen(GetParent(hDialog), &point);// now move the Window to the new position
MoveWindow(hDialog, point.x, point.y, sizeDialog.cx, sizeDialog.cy, true);Thanks for your reply. I have changed the same code which you had sent to MFC as shown below. But still its not working. Please help me what is wrong..? // MainFrm.h CSysWindow m_SysWnd; //CSysWindow derived from CDialogbar // MainFrm.CPP int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (!m_SysWnd.Create(this, IDD_SYS2,CBRS_TOP|CBRS_FLYBY|CBRS_TOOLTIPS, IDD_SYS2)) { TRACE0("Failed to create DlgBar\n"); return -1; // fail to create } m_SysWnd.SetBarStyle( m_SysWnd.GetBarStyle()| CBRS_ALIGN_TOP|CBRS_BORDER_TOP | CBRS_FLOAT_MULTI ); m_SysWnd.AttachCtrls(); //To Attach the Combo box to the control bar window RECT rectParent, rectDialog; SIZE sizeParent, sizeDialog; POINT pointNew; HWND hDialog = m_SysWnd.m_hWnd; CWnd * hParentWnd = m_SysWnd.GetParent(); // get the size of the dialog m_SysWnd.GetWindowRect(&rectDialog); sizeDialog.cx = rectDialog.right - rectDialog.left; sizeDialog.cy = rectDialog.bottom - rectDialog.top; // get the size of the parent Window's client area hParentWnd->GetClientRect(&rectParent); sizeParent.cx = rectParent.right - rectParent.left; sizeParent.cy = rectParent.bottom - rectParent.top; // parent's sizes minus dialog's sizes divided by 2 // give the coordinates of the centre position pointNew.x = (sizeParent.cx - sizeDialog.cx) / 2; pointNew.y = (sizeParent.cy - sizeDialog.cy) / 2; // we need to make parent coordinates relative to the screen hParentWnd->ClientToScreen(&pointNew); // now move the Window to the new position m_SysWnd.MoveWindow(pointNew.x, pointNew.y, sizeDialog.cx, sizeDialog.cy, true);
-
Thanks for your reply. I have changed the same code which you had sent to MFC as shown below. But still its not working. Please help me what is wrong..? // MainFrm.h CSysWindow m_SysWnd; //CSysWindow derived from CDialogbar // MainFrm.CPP int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (!m_SysWnd.Create(this, IDD_SYS2,CBRS_TOP|CBRS_FLYBY|CBRS_TOOLTIPS, IDD_SYS2)) { TRACE0("Failed to create DlgBar\n"); return -1; // fail to create } m_SysWnd.SetBarStyle( m_SysWnd.GetBarStyle()| CBRS_ALIGN_TOP|CBRS_BORDER_TOP | CBRS_FLOAT_MULTI ); m_SysWnd.AttachCtrls(); //To Attach the Combo box to the control bar window RECT rectParent, rectDialog; SIZE sizeParent, sizeDialog; POINT pointNew; HWND hDialog = m_SysWnd.m_hWnd; CWnd * hParentWnd = m_SysWnd.GetParent(); // get the size of the dialog m_SysWnd.GetWindowRect(&rectDialog); sizeDialog.cx = rectDialog.right - rectDialog.left; sizeDialog.cy = rectDialog.bottom - rectDialog.top; // get the size of the parent Window's client area hParentWnd->GetClientRect(&rectParent); sizeParent.cx = rectParent.right - rectParent.left; sizeParent.cy = rectParent.bottom - rectParent.top; // parent's sizes minus dialog's sizes divided by 2 // give the coordinates of the centre position pointNew.x = (sizeParent.cx - sizeDialog.cx) / 2; pointNew.y = (sizeParent.cy - sizeDialog.cy) / 2; // we need to make parent coordinates relative to the screen hParentWnd->ClientToScreen(&pointNew); // now move the Window to the new position m_SysWnd.MoveWindow(pointNew.x, pointNew.y, sizeDialog.cx, sizeDialog.cy, true);
MFC handles the docking state and sizing of control bars. Your code does not show that you are docking the control bar; are you? After creation of m_SysWnd: m_SysWnd.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); // CMainFrame enable docking DockControlBar(&m_wndDlgBar); // CMainFrame dock control bar As far as moving the bar to a specific location look at CMainFrame::FloatControlBar() which undocks the controlbar and allows for positioning. FloatControlBar(&m_SysWnd,CPoint(100,100)); In regards to resizing the bar, you will probably have to provide some implementation of CDialogBar::CalcFixedLayout() or CDialogBar::CalcDynamicLayout() override which provides size info. CSize CSysWindow::CalcFixedLayout(BOOL bStretch, BOOL bHorz) { if (bStretch) // if not docked stretch to fit { // This is a fixed size. You will have to provide your own implementation. return CSize(100,100); //return CSize(bHorz ? 32767 : m_sizeDefault.cx, // bHorz ? m_sizeDefault.cy : 32767); } else return m_sizeDefault; } A Google search on these functions may help. Best regards.
-
Thanks for your reply. I have changed the same code which you had sent to MFC as shown below. But still its not working. Please help me what is wrong..? // MainFrm.h CSysWindow m_SysWnd; //CSysWindow derived from CDialogbar // MainFrm.CPP int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (!m_SysWnd.Create(this, IDD_SYS2,CBRS_TOP|CBRS_FLYBY|CBRS_TOOLTIPS, IDD_SYS2)) { TRACE0("Failed to create DlgBar\n"); return -1; // fail to create } m_SysWnd.SetBarStyle( m_SysWnd.GetBarStyle()| CBRS_ALIGN_TOP|CBRS_BORDER_TOP | CBRS_FLOAT_MULTI ); m_SysWnd.AttachCtrls(); //To Attach the Combo box to the control bar window RECT rectParent, rectDialog; SIZE sizeParent, sizeDialog; POINT pointNew; HWND hDialog = m_SysWnd.m_hWnd; CWnd * hParentWnd = m_SysWnd.GetParent(); // get the size of the dialog m_SysWnd.GetWindowRect(&rectDialog); sizeDialog.cx = rectDialog.right - rectDialog.left; sizeDialog.cy = rectDialog.bottom - rectDialog.top; // get the size of the parent Window's client area hParentWnd->GetClientRect(&rectParent); sizeParent.cx = rectParent.right - rectParent.left; sizeParent.cy = rectParent.bottom - rectParent.top; // parent's sizes minus dialog's sizes divided by 2 // give the coordinates of the centre position pointNew.x = (sizeParent.cx - sizeDialog.cx) / 2; pointNew.y = (sizeParent.cy - sizeDialog.cy) / 2; // we need to make parent coordinates relative to the screen hParentWnd->ClientToScreen(&pointNew); // now move the Window to the new position m_SysWnd.MoveWindow(pointNew.x, pointNew.y, sizeDialog.cx, sizeDialog.cy, true);
Sorry, this is the correct code: CSize CSysWindow::CalcFixedLayout(BOOL bStretch, BOOL bHorz) { if (bStretch) // if not docked stretch to fit { return CDialogBar::CalcFixedLayout(bStretch, bHorz); } else // This is a fixed size. You will have to provide your own implementation. return CSize(100,100); }
-
Thanks for your reply. I have changed the same code which you had sent to MFC as shown below. But still its not working. Please help me what is wrong..? // MainFrm.h CSysWindow m_SysWnd; //CSysWindow derived from CDialogbar // MainFrm.CPP int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (!m_SysWnd.Create(this, IDD_SYS2,CBRS_TOP|CBRS_FLYBY|CBRS_TOOLTIPS, IDD_SYS2)) { TRACE0("Failed to create DlgBar\n"); return -1; // fail to create } m_SysWnd.SetBarStyle( m_SysWnd.GetBarStyle()| CBRS_ALIGN_TOP|CBRS_BORDER_TOP | CBRS_FLOAT_MULTI ); m_SysWnd.AttachCtrls(); //To Attach the Combo box to the control bar window RECT rectParent, rectDialog; SIZE sizeParent, sizeDialog; POINT pointNew; HWND hDialog = m_SysWnd.m_hWnd; CWnd * hParentWnd = m_SysWnd.GetParent(); // get the size of the dialog m_SysWnd.GetWindowRect(&rectDialog); sizeDialog.cx = rectDialog.right - rectDialog.left; sizeDialog.cy = rectDialog.bottom - rectDialog.top; // get the size of the parent Window's client area hParentWnd->GetClientRect(&rectParent); sizeParent.cx = rectParent.right - rectParent.left; sizeParent.cy = rectParent.bottom - rectParent.top; // parent's sizes minus dialog's sizes divided by 2 // give the coordinates of the centre position pointNew.x = (sizeParent.cx - sizeDialog.cx) / 2; pointNew.y = (sizeParent.cy - sizeDialog.cy) / 2; // we need to make parent coordinates relative to the screen hParentWnd->ClientToScreen(&pointNew); // now move the Window to the new position m_SysWnd.MoveWindow(pointNew.x, pointNew.y, sizeDialog.cx, sizeDialog.cy, true);
I cannot see anything obvious in your code, although you may need to put that code at a later point; perhaps in the toolbar's OnCreate method. Looking at the documentation it suggests that a CToolBar should include the
WS_CHILD
style at creation, but you do not include that. Unfortunately I do not use MFC so I cannot recreate your situation. If it still does not work then you will need to use your debugger to see what is happening. -
Sorry, this is the correct code: CSize CSysWindow::CalcFixedLayout(BOOL bStretch, BOOL bHorz) { if (bStretch) // if not docked stretch to fit { return CDialogBar::CalcFixedLayout(bStretch, bHorz); } else // This is a fixed size. You will have to provide your own implementation. return CSize(100,100); }
Thanks for your kind reply. Please help me to create a single window that has the following in the client area: 1. Dialog bar on top (used as a toolbar) 2. A view to display a graph trend (Updated every cycle) 3. After the above view, a Dialog bar to be placed. (Used as a tool bar) 4. A Grid control to display set of values which will be updated every cycle. As I am not an expert into this, Please guide me steps to create the above window and update it every cycle as defined.
-
Thanks for your kind reply. Please help me to create a single window that has the following in the client area: 1. Dialog bar on top (used as a toolbar) 2. A view to display a graph trend (Updated every cycle) 3. After the above view, a Dialog bar to be placed. (Used as a tool bar) 4. A Grid control to display set of values which will be updated every cycle. As I am not an expert into this, Please guide me steps to create the above window and update it every cycle as defined.
Sorry, the group does not write code for you. See How to Ask a Question at the top. If you are not familiar with these tasks, you need to get training on programming in MFC. I've learned a lot just by using Google to search for tasks I want to perform in MFC and in many cases finding example code that helped me get the task done. If you write code to perform a task and it doesn't work as expected, then ask the group for help with the particular section of your code that isn't working and they will help you; like you did with your current question. Best regards
-
Sorry, the group does not write code for you. See How to Ask a Question at the top. If you are not familiar with these tasks, you need to get training on programming in MFC. I've learned a lot just by using Google to search for tasks I want to perform in MFC and in many cases finding example code that helped me get the task done. If you write code to perform a task and it doesn't work as expected, then ask the group for help with the particular section of your code that isn't working and they will help you; like you did with your current question. Best regards
Sorry for that. As you said I will refer google. Thanks