2015-01-21 51 views
1

我是新的MFC。我需要創建一個浮動工具欄(CToolBar),沒有停靠選項並保存並恢復其最後的位置。MFC浮動工具欄始終有效

工具欄也應始終處於活動狀態,但它不是。 當我從主機打開一個新的子窗口(例如對話框)時,浮動工具欄變得不活動(我不能點擊它的按鈕或拖動它等等)。

在過去,我使用CDiaolog與重疊樣式,它是浮動的,並始終根據我的需要活躍。是否可以對我的浮動工具欄做同樣的事情?由於

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{  
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1) 
    return -1; 

    toolbarIconSize.cx = toolbarIconSize.cy = TOOLBAR_MAIN_ICON_SIZE; 
    if (!m_wndMyFloatingToolbar.Create(this,m_wndMyFloatingToolbar.GetBarStyle() |WS_EX_PALETTEWINDOW | WS_EX_TOPMOST |CBRS_FLOATING | WS_VISIBLE) || 
    !m_wndMyFloatingToolbar.LoadToolBar(IDR_GENERAL_TOOLBAR, toolbarIconSize)) 
    { 
     TRACE0("Failed to create My Floating Toolbar\n"); 
     return -1;  // fail to create 
    } 

    m_wndMyFloatingToolbar.EnableDocking(0); 
    EnableDocking(0); 

    if (!CreateCtrlBar()) 
    { 
     TRACE0("Failed to create ctrl toolbar\n"); 
     return -1;  // fail to create 
    } 

    // ... 
    //... 
    return 0; 
} 

void CMainFrame::OnViewToolBar() 
{ 
    // ... 
    //... 

    CPoint Pos = MyFloatingToolbarGetLastPosition(); \\Get last pos 
    FloatControlBar(&m_wndMyFloatingToolbar, Pos, CBRS_ALIGN_LEFT); 
    MyFloatingToolbarSetIsVisible(); 
    FloatControlBar(&m_wndMyFloatingToolbar, Pos, CBRS_ALIGN_LEFT); 
} 
void CMainFrame::MyFloatingToolbarSetIsVisible() 
{ 
    WINDOWPLACEMENT wp; 
    m_wndMyFloatingToolbar.GetParent()->GetParent()->GetWindowPlacement(&wp); 
    wp.showCmd = SW_SHOW; 
    m_wndMyFloatingToolbar.GetParent()->GetParent()->SetWindowPlacement(&wp); 

    m_wndMyFloatingToolbar.GetParent()->GetWindowPlacement(&wp); 
    wp.showCmd = SW_SHOW; 
    m_wndMyFloatingToolbar.GetParent()->SetWindowPlacement(&wp); 

    m_wndMyFloatingToolbar.GetWindowPlacement(&wp); 
    wp.showCmd = SW_SHOW; 
    m_wndMyFloatingToolbar.SetWindowPlacement(&wp); 
} 
void CWJToolBar::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos) 
{ 
    CToolBar::OnWindowPosChanging(lpwndpos); 

    if (GetBarStyle() & CBRS_FLOATING) 
    { 
     if((lpwndpos->flags & SWP_HIDEWINDOW) && ((this->GetParentFrame())->m_hWnd !=(this->GetTopLevelFrame())->m_hWnd)) 
     { 
      CMainFrame* mf = (CMainFrame*)(AfxGetApp()->GetMainWnd()); 
      mf->MyFloatingToolbarSavePosition();   
     } 
    } 
} 
+1

您應該在使用ClientToScreen函數將x和y轉換爲屏幕座標之前,先存儲它們,或者從文件中讀取它們。 – 2015-01-21 11:50:53

+0

爲什麼不使用CMFCToolbar? – 2018-02-09 10:18:41

回答

1
  1. 您可能需要調試,查看其座標,如果他們設置正確。獨立。 :P根據您當前的張貼代碼
  2. ,我沒有看到你所存儲的數據點,試試這個

    • 隱藏工具欄
    • 保存的位置數據
    • 改變你的父窗口位置和
    • 重新載入您保存的座標。

保存的數據變得不正確的值即可。

我建議你捕捉你想要添加工具欄的位置live。這使您的工具欄應用程序更通用。 所以,

  1. 保存工具欄的IE左上角的距離,以它的父窗口,而不是它的座標
  2. 讓你的父窗口座標
  3. 刷新根據保存的距離工具欄

當然還有其他方法可以做到這一點,但我認爲這樣做可能會更加簡單,以實現您可能要查找的內容。

0

使用CMFCToolBar(而不是CToolBar),那麼你只需要2個命令來實現這一點。

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{ 
    if (CMDIFrameWndEx::OnCreate(lpCreateStruct) == -1) 
    return -1; 

    : 
    m_wndToolBar.SetPermament(TRUE); // it removes CloseButton (=always active) 

    CRect rect; 
    GetClientRect(&rect); 
    ClientToScreen(rect); 
    rect.OffsetRect(100, 20); 
    m_wndToolBar.FloatPane(rect);  // Float and move it to your wished coordinates 
    : 

}