窗口框架編程小結

註冊窗口類,主要修改圖標,背景和光標

在BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)中註冊,分別修改框架類或者窗口類。

 

WNDCLASS wndcls;

wndcls.cbClsExtra=0;

wndcls.cbWndExtra=0;

wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);

wndcls.hCursor=LoadCursor(NULL,IDC_HELP);

wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);

wndcls.hInstance=AfxGetInstanceHandle();

wndcls.lpfnWndProc=::DefWindowProc;

wndcls.lpszClassName="Persuper";

wndcls.lpszMenuName=NULL;

wndcls.style=CS_HREDRAW | CS_VREDRAW;

 

RegisterClass(&wndcls);

 

cs.lpszClass="Persuper";

 

 

還有一種方法修改圖標,光標,背景顏色。

 

cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,0,0,

        LoadIcon(NULL,IDI_WARNING));

 

或者 cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW);

 

在創建後修改背景和圖標

SetClassLong(m_hWnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(BLACK_BRUSH));

SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)LoadCursor(NULL,IDC_HELP));

 

動態修改圖標,背景的方法:在定義圖標數組,在Oncreate函數中加載圖標

m_hIcons[0]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1));

m_hIcons[1]=LoadIcon(theApp.m_hInstance,MAKEINTRESOURCE(IDI_ICON2));

m_hIcons[2]=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON3));

其中包含了3中取得當前程序的實例,使用theApp需要添加說明

extern CStyleApp theApp;

表示在外部源文件中定義的變量。

最後設置定時器 SetTimer(1,1000,NULL);

 

另外:

void CMainFrame::OnTimer(UINT nIDEvent)

{

static int index=1;

SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons[index]);

index=++index%3;

 

CFrameWnd::OnTimer(nIDEvent);

}

 

 

刪除工具欄上的按鈕,左鍵按住,拖出工具欄區域即可!

工具欄停靠設置

m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);  工具欄自己可以停靠

EnableDocking(CBRS_ALIGN_ANY);  本對象可以被停靠

DockControlBar(&m_wndToolBar);設置該工具欄被停靠

 

判斷工具欄狀態,在菜單事件響應中添加:

if(m_newToolBar.IsWindowVisible())

{

m_newToolBar.ShowWindow(SW_HIDE);

}

else

{

m_newToolBar.ShowWindow(SW_SHOW);

}

RecalcLayout();

DockControlBar(&m_newToolBar);

}

 

另外一種顯示工具欄的方法:

ShowControlBar(&m_newToolBar,!m_newToolBar.IsWindowVisible(),FALSE);

這種方式更好。

 

爲菜單項添加複選標記

void CMainFrame::OnUpdateViewNewtool(CCmdUI* pCmdUI)

{

// TODO: Add your command update UI handler code here

pCmdUI->SetCheck(m_newToolBar.IsWindowVisible());

}

 

狀態欄

狀態指示器:

static UINT indicators[] =

{

ID_SEPARATOR,           // status line indicator

IDS_TIMER,

IDS_PROGRESS,

ID_INDICATOR_CAPS,

ID_INDICATOR_NUM,

ID_INDICATOR_SCRL,

};

 

CTime t=CTime::GetCurrentTime();

CString str=t.Format("%H:%M:%S");

CClientDC dc(this);

CSize sz=dc.GetTextExtent(str);

int index=0;

index=m_wndStatusBar.CommandToIndex(IDS_TIMER);

m_wndStatusBar.SetPaneInfo(index,IDS_TIMER,SBPS_NORMAL,sz.cx);

m_wndStatusBar.SetPaneText(index,str);

或者

 

CTime t=CTime::GetCurrentTime(); 

CString str=t.Format("%H:%M:%S");

CClientDC dc(this);

CSize sz=dc.GetTextExtent(str); // 得到字符串在面板上顯示的長度

m_wndStatusBar.SetPaneInfo(1,IDS_TIMER,SBPS_NORMAL,sz.cx);

m_wndStatusBar.SetPaneText(1,str);

 

判斷句柄是否有值

void CMainFrame::OnPaint()

{

CPaintDC dc(this); // device context for painting

 

// TODO: Add your message handler code here

CRect rect;

m_wndStatusBar.GetItemRect(2,&rect);

if(!m_progress.m_hWnd)

m_progress.Create(WS_CHILD | WS_VISIBLE ,//| PBS_SMOOTH,

rect,&m_wndStatusBar,123);

else

m_progress.MoveWindow(rect);

m_progress.SetPos(50);

// Do not call CFrameWnd::OnPaint() for painting messages

}

 

狀態欄文本設置

 

CString str;

str.Format("x=%d,y=%d",point.x,point.y);

//((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str);

//((CMainFrame*)GetParent())->SetMessageText(str);

//((CMainFrame*)GetParent())->GetMessageBar()->SetWindowText(str);

GetParent()->GetDescendantWindow(AFX_IDW_STATUS_BAR)->SetWindowText(str);

 

 

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章