1添加狀态欄圖檔
繼承CStatusBar類,重新DrawItem函數
void CBitmapStatusBar::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
UINT nID,nStyle;
int nWidth;
GetPaneInfo(lpDrawItemStruct->itemID, nID, nStyle, nWidth);
switch(nID)
{
// 如果是CAPS格則處理
case ID_INDICATOR_CAPS:
//從資源中選擇位圖
CBitmap pBitmap;
//加載位圖
pBitmap.LoadBitmap(IDB_MY_BITMAP);
//将狀态條附加到一個CDC對象
CDC dc,SourceDC;
dc.Attach(lpDrawItemStruct->hDC);
//得到狀态條窗格的大小和坐标
CRect rect(&lpDrawItemStruct->rcItem);
//将目前位圖放入相容CDC
SourceDC.CreateCompatibleDC(NULL);
CBitmap* pOldBitmap = SourceDC.SelectObject(&pBitmap);
dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),&SourceDC, 0, 0, SRCCOPY);
SourceDC.SelectObject(pOldBitmap);
pBitmap.DeleteObject();
dc.Detach();
return;
}
CStatusBar::DrawItem(lpDrawItemStruct);
}
将CMainFrame的CStatusBar,改為繼承的子類。
CMainFrame::OnCreate中添加
UINT nID,nStyle;
int nPane,nWidth;
nPane = m_wndStatusBar.CommandToIndex(ID_INDICATOR_CAPS);
m_wndStatusBar.GetPaneInfo(nPane, nID, nStyle, nWidth) ;
m_wndStatusBar.SetPaneInfo(nPane, nID, SBPS_OWNERDRAW|SBPS_NOBORDERS, nWidth + 30);
//m_wndStatusBar.SetPaneInfo(2,ID_SEPARATOR,SBPS_NORMAL,nWidth);
//m_wndStatusBar.SetPaneInfo(1,ID_SEPARATOR,SBPS_NORMAL,nWidth);
//m_wndStatusBar.SetPaneInfo(3,ID_INDICATOR_SCRL,SBPS_NORMAL,nWidth);
//m_wndStatusBar.SetPaneText(3, _T("0"), TRUE);
//m_wndStatusBar.SetPaneText(1, _T("1"), TRUE);
//m_wndStatusBar.SetPaneText(2, _T("2"), TRUE);
//nPane = m_wndStatusBar.CommandToIndex(ID_INDICATOR_SCRL);
//m_wndStatusBar.GetStatusBarCtrl().SetText(_T("hello"), 0, 0);
//m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
------------------------------------------------------------------------------
2添加菜單
CMenu *pMenu = AfxGetMainWnd()->GetMenu(); //得到主架構菜單
pMenu->GetSubMenu(2)->AppendMenu(MF_SEPARATOR);//添加菜單分隔符
pMenu->GetSubMenu(2)->AppendMenu(MF_STRING, IDM_ACTION_MENU, "動态菜單(&A)");//添加菜單
-------------------------------------------------------------------------------
3添加滾動條到狀态欄
繼承CStatusBar類(同1)
int CProgressStatusBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
VERIFY(CStatusBar::OnCreate(lpCreateStruct) ==0 );
VERIFY(m_ProgressCtrl.Create(WS_CHILD|WS_VISIBLE, CRect(0,0,0,0), this, IDC_PROGRESS));
return 0;
}
void CProgressStatusBar::OnSize(UINT nType, int cx, int cy)
{
CStatusBar::OnSize(nType, cx, cy);
CRect rect;
GetItemRect(1, &rect);
m_ProgressCtrl.MoveWindow(rect); //CProgressCtrl
}
-----------------------------------------------------------------------
4狀态欄中,滾動字幕
/在定時器中執行字幕程式
void CMainFrame::OnTimer(UINT nIDEvent)
{
static int strID = 0; //字元索引
//達到滾動字幕的末尾
if (strID >= (m_strStatusText.GetLength() - 1))
{
strID = 0;
}
m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(ID_SEPARATOR), ((LPCSTR) m_strStatusText) + strID);
//後移顯示的字幕,由于漢字占兩位,是以+2
strID = strID + 2;
CFrameWnd::OnTimer(nIDEvent);
}
-------------------------------------------------------------------------------------
5 圖檔 菜單
m_bitmap.LoadBitmap(IDB_BITMAP1); //加載位圖
CMenu *pMenu = GetMenu();//獲得菜單指針
//修改“位圖菜單”->“菜單一”為位圖菜單
pMenu->GetSubMenu(4)->ModifyMenu(ID_MENU_BMP, MF_BYCOMMAND, ID_MENU_BMP, &m_bitmap);
//ModifyMenu可以修改菜單的各種屬性,check 文字 ==
--------------------------------------------------------------------------------
6 右鍵菜單
WM_CONTENTMENU
CMenu contextMenu;
contextMenu.LoadMenu(IDR_CONTEXTMENU);//加載已有的菜單
CMenu *pPopupMenu=contextMenu.GetSubMenu(0);//獲得彈出菜單指針
ClientToScreen(&point);//轉換滑鼠坐标
//彈出菜單
pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd());
----------------------------------------------------------------------------------
7屏蔽系統菜單(隻能alt+F4,或者任務管理器退出)
CMainFrame::PreCreateWindow中 加入cs.style &= ~WS_SYSMENU;
----------------------------------------------------------------------------------
8截獲消息
使系統菜單失效
LRESULT CMFCDLGDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_SYSCOMMAND)
{
if (wParam == SC_MAXIMIZE || wParam == SC_MINIMIZE || wParam == SC_CLOSE)
{
return TRUE;
}
}
return CWnd::WindowProc(message, wParam, lParam);
}
-----------------------------------------------------------------------------
9全屏
void CMainFrame::FullScreen()
{
GetWindowPlacement(&m_oldWinPos);// 得到目前視窗的位置
CRect winRect;// 視窗區域
GetWindowRect(&winRect);// 得到視窗區域
CRect rectClient;
RepositionBars(0, 0xffff, AFX_IDW_PANE_FIRST, reposQuery, &rectClient); // 全屏時隐藏所有的控制條
ClientToScreen(&rectClient);// 将客戶坐标映射成螢幕坐标
//擷取螢幕分辨率
int fullWidth = GetSystemMetrics(SM_CXSCREEN);
int fullHeight = GetSystemMetrics(SM_CYSCREEN);
//得到全屏顯示的視窗位置
m_fullScreenRect.left = winRect.left - rectClient.left;
m_fullScreenRect.top = winRect.top - rectClient.top;
m_fullScreenRect.right = winRect.right - rectClient.right + fullWidth;
m_fullScreenRect.bottom= winRect.bottom - rectClient.bottom + fullHeight;
// 進入全屏顯示狀态
m_fullScreenFlag = TRUE;// 設定全屏标志
WINDOWPLACEMENT tmp;
tmp.length = sizeof(WINDOWPLACEMENT);
tmp.flags = 0;
tmp.showCmd = SW_SHOWNORMAL;
tmp.rcNormalPosition = m_fullScreenRect;
SetWindowPlacement(&tmp);// 将視窗設定到m_fullScreenRect位置上
}
// 結束程式視窗全屏顯示,恢複視窗原來大小
void CMainFrame::EndFullScreen()
{
if(m_fullScreenFlag)
{
m_fullScreenFlag = FALSE;// 設定全屏狀态為FALSE
SetWindowPlacement(&m_oldWinPos);// 退出全屏顯示, 恢複原視窗顯示
}
}
void CXXXXXView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(nChar == VK_F11)
{
// 擷取主架構視窗的指針
CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
if(pFrame->GetScreenState()) // 如果現在是全屏狀态
{
// 調用主視窗類的自定義函數 EndFullScreen,退出全屏顯示狀态
pFrame->EndFullScreen();
}
else // 不是全屏狀态
{
// 調用主視窗類的自定義函數 FullScreen ,進入全屏顯示狀态
pFrame->FullScreen();
}
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
---------------------------------------------------------------------------
10。隐藏菜單欄
void CHideMenuBarView::OnLButtonDown(UINT nFlags, CPoint point)
{
//隐藏菜單欄
::SetMenu(AfxGetMainWnd()->m_hWnd, NULL);
CView::OnLButtonDown(nFlags, point);
}
//右擊處理函數
void CHideMenuBarView::OnRButtonDown(UINT nFlags, CPoint point)
{
//顯示菜單欄
CMenu menu;
menu.LoadMenu(IDR_MAINFRAME);//加載菜單
HMENU hMenu = (HMENU)menu;//類型轉換
::SetMenu(AfxGetMainWnd()->m_hWnd, hMenu);//設定菜單
CView::OnRButtonDown(nFlags, point);
}
----------------------------------------------------------------------------
11.隐藏标題欄 更改視窗風格
void CMainFrame::OnMenuShowtitlebar()
{
LONG style = GetWindowLong(m_hWnd, GWL_STYLE); //得到視窗類型
if ((style | WS_CAPTION) == style)
{
MessageBox(_T("the same style"));
}
else
{
style |= WS_CAPTION;//修改視窗類型,顯示标題欄
SetWindowLong(m_hWnd, GWL_STYLE, style);//設定視窗類型
//移動視窗使顯示标題欄立即生效
RECT winRect;
GetWindowRect(&winRect);//得到視窗位置
winRect.top--;//視窗下移
MoveWindow(&winRect); //移動視窗
}
}
void CMainFrame::OnMenuHidetitlebar()
{
LONG style = GetWindowLong(m_hWnd, GWL_STYLE); //得到視窗類型
if ((style & (~(WS_CAPTION))) == style)
{
MessageBox(_T("the same style"));
}
else
{
style &= ~(WS_CAPTION);//修改視窗類型,顯示标題欄
SetWindowLong(m_hWnd, GWL_STYLE, style);//設定視窗類型
//移動視窗使隐藏标題欄立即生效
RECT winRect;
GetWindowRect(&winRect);//得到視窗位置
winRect.top++;//視窗上移
MoveWindow(&winRect);//移動視窗
}
}
--------------------------------------------------
12.隐藏狀态欄(隐藏工具欄類似)
void CMainFrame::OnShow()
{
//判斷是否狀态欄可見
if(!m_wndStatusBar.IsWindowVisible())
{
//如果是不可見的,設定為可見
m_wndStatusBar.ModifyStyle(0, WS_VISIBLE);
}
//使狀态欄狀态生效
SendMessage(WM_SIZE);
}
void CMainFrame::OnHide()
{
//判斷是否狀态欄可見
if(m_wndStatusBar.IsWindowVisible())
{
//如果是可見,設定為隐藏
m_wndStatusBar.ModifyStyle(WS_VISIBLE, 0);
}
//使狀态欄狀态生效
SendMessage(WM_SIZE);
}
------------------------------------------------------------
13.圖檔 菜單
BOOL C***MDIDoc::OnNewDocument()//MDI不能在Frame中設定菜單
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
m_bitmap.LoadBitmap(IDB_BITMAP1);
CMenu *pMenu;
pMenu = CMenu::FromHandle(((CMultiDocTemplate *)m_pDocTemplate)->m_hMenuShared); // 獲得文檔菜單
//給菜單 檔案->建立 添加圖示
pMenu->GetSubMenu(0)->SetMenuItemBitmaps(0, MF_BYPOSITION, &m_bitmap, &m_bitmap);
return TRUE;
}
--------------------------------------------------------------------------
14. 添加背景圖檔
BOOL CBgWnd::OnEraseBkgnd(CDC* pDC) //class CBgWnd : public CWnd
{
CRect rectClient;// 客戶區
CDC dcMen; // 記憶體裝置描述表
CBitmap bmp; // 位圖對象
bmp.LoadBitmap(IDB_BITMAP1);// 從資源加載位圖
BITMAP tmpBitmap;
bmp.GetObject(sizeof(BITMAP), &tmpBitmap);
CSize bmpSize(tmpBitmap.bmWidth, tmpBitmap.bmHeight);// 得到位圖的大小
dcMen.CreateCompatibleDC(pDC);// 建立相容的裝置描述表
CBitmap *pOld=dcMen.SelectObject(&bmp);// 選入新的位圖對象并儲存舊的位圖對象
GetClientRect(&rectClient);// 取得客戶區的大小
// 從記憶體向螢幕複制位圖對象
pDC->StretchBlt(rectClient.left, rectClient.top, rectClient.Width(), rectClient.Height(),
&dcMen, 0, 0, bmpSize.cx, bmpSize.cy, SRCCOPY);
dcMen.SelectObject(pOld);// 恢複舊的位圖對象
dcMen.DeleteDC();// 删除記憶體裝置描述表
return true;
}
void CBgWnd::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
Invalidate();
}
在MainFrame中定義CBgWnd變量,在MainFrame::OnCreate中,
// 把m_wndBg子類化為主視窗的客戶視窗
if(!m_wndBg.SubclassWindow(m_hWndMDIClient))
{
TRACE("Fail to subclass MDI client window\n");
return -1;
}
15.視窗預設 最大化(與7類似)
1.子視窗
在CChildFrame::PreCreateWindow中
cs.style = cs.style | WS_MAXIMIZE | WS_VISIBLE;
2主視窗
在APP類中,::InitInstance()中,pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
-----------------------------------------------
16 MDI預設 不建立文檔
http://blog.sina.com.cn/s/blog_4c9c4e2b0100aeol.html
CXXXXXApp::InitInstance()中,cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;//
17 OnNcHitTest(不能移動的窗體)
UINT CMainFrame::OnNcHitTest(CPoint point)
{
// 截獲滑鼠事件
UINT returnValue = CFrameWnd::OnNcHitTest(point);
// 判斷是否在拖動窗體的工具欄
if(returnValue == HTCAPTION)
{
// 如果是拖動工具欄則截獲後不傳遞消息
return FALSE;
}
else
{
// 否則直接傳遞消息
return returnValue;
}
}
-------------------------------------------------------------------------------
17輕按兩下 彈出打開文檔對話框
CMainFrame::OnCreate中:
// 為響應滑鼠輕按兩下客戶區的事件所增加的屬性:CS_DBLCLKS
HWND hMDIClientArea = GetWindow(GW_CHILD)->GetSafeHwnd();
::SetClassLong(hMDIClientArea, GCL_STYLE, ::GetClassLong(hMDIClientArea,GCL_STYLE) | CS_DBLCLKS);
void CMainFrame::OnLButtonDblClk(UINT nFlags, CPoint point)
{
AfxGetApp()->m_pDocManager->OnFileOpen();
CMDIFrameWnd::OnLButtonDblClk(nFlags, point);
}
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
// 如果是在主客戶區輕按兩下滑鼠,則發送wm_lbuttondblclk消息
// if( pmsg->message == wm_lbuttondblclk)
//postmessage(wm_lbuttondblclk);// 發送滑鼠輕按兩下消息
return CMDIFrameWnd::PreTranslateMessage(pMsg);
}
18.單選菜單
void CMainFrame::OnMenu1()
{
m_currMenu = 1; //目前選中菜單1
}
void CMainFrame::OnUpdateMenu1(CCmdUI* pCmdUI)
{
//标記菜單1
pCmdUI->SetRadio(m_currMenu == 1);
}
void CMainFrame::OnMenu2()
{
m_currMenu = 2; //目前選中菜單2
}
void CMainFrame::OnUpdateMenu2(CCmdUI* pCmdUI)
{
//标記菜單2
pCmdUI->SetCheck(m_currMenu == 2);
}
void CMainFrame::OnMenu3()
{
m_currMenu = 3; //目前選中菜單3
}
void CMainFrame::OnUpdateMenu3(CCmdUI* pCmdUI)
{
//标記菜單3
pCmdUI->SetRadio(m_currMenu == 3);
}
-=------------------------------------------------------------------
19.儲存、恢複視窗資訊
void CMainFrame::OnClose()
{
HKEY key; // 存放系統資料庫鍵句柄
CString appKey = "Software\\WolfeLee\\SaveWinState\\Settings";// 系統資料庫鍵值位置
CString keyName= "WinState";// 系統資料庫鍵名
// 打開系統資料庫并判斷是否在系統資料庫中已經有相關的鍵,如果沒有就建立
if (RegOpenKeyEx(HKEY_CURRENT_USER, appKey, 0, KEY_WRITE, &key) != ERROR_SUCCESS)
{
DWORD disposition;// 傳回建立後的資訊
RegCreateKeyEx(HKEY_CURRENT_USER, appKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &disposition);
}
WINDOWPLACEMENT winInfo;// 視窗資訊
GetWindowPlacement(&winInfo);// 獲得視窗資訊
// 将視窗資訊儲存到系統資料庫中
RegSetValueEx(key, keyName, 0, REG_BINARY, (LPBYTE)&winInfo, sizeof(WINDOWPLACEMENT));
RegCloseKey(key);// 關閉系統資料庫
CFrameWnd::OnClose();// 關閉視窗
}
BOOL CSaveWinStateApp::InitInstance()中:
// 以下為功能代碼
HKEY key; // 存放系統資料庫鍵句柄
DWORD keyType = REG_BINARY;// 鍵類型
DWORD keySize = sizeof(WINDOWPLACEMENT);// 鍵值長度
WINDOWPLACEMENT winInfo;// 視窗資訊
LONG regInfo; // 系統資料庫操作傳回碼
CString appKey = "Software\\WolfeLee\\SaveWinState\\Settings";// 系統資料庫鍵值位置
CString keyName= "WinState";// 系統資料庫鍵名
if((regInfo = RegOpenKeyEx(HKEY_CURRENT_USER, appKey, 0, KEY_READ, &key)) == ERROR_SUCCESS )
{
keyType = REG_BINARY;
keySize = sizeof(WINDOWPLACEMENT);
regInfo = RegQueryValueEx(key, keyName, 0, &keyType, (LPBYTE)&winInfo, &keySize);
RegCloseKey(key);
}
if (regInfo == ERROR_SUCCESS)
{
m_pMainWnd->SetWindowPlacement(&winInfo);
}
else
{
m_pMainWnd->ShowWindow(m_nCmdShow);
}
20.滾動條
void CXXXXXView::OnInitialUpdate()
{
......
......
// 計算整個視圖的大小
sizeTotal.cx = sizeTotal.cy = 1000;
SetScrollSizes(MM_TEXT, sizeTotal);
}
21固定視窗大小
WM_GETMINMAXINFO
void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
// 将最小跟蹤與最大跟蹤設定成相同的值使視窗大小不能改變
lpMMI->ptMinTrackSize.x = 500; // 設定最小跟蹤寬度
lpMMI->ptMinTrackSize.y = 400; // 設定最小跟蹤高度
lpMMI->ptMaxTrackSize.x = 500; // 設定最大跟蹤寬度
lpMMI->ptMaxTrackSize.y = 400; // 設定最大跟蹤高度
CFrameWnd::OnGetMinMaxInfo(lpMMI);
}
22分割視圖CSplitterWnd
具體見http://www.vckbase.com/document/viewdoc/?id=192
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
//建立2*2分割視窗
if(m_wndSplitter.Create(this,
2, 2,
CSize(1000, 1000), // 顯示最小号20 * 20
pContext))
{
//m_wndSplitter.SetWindowPos(NULL, 200, 0, 0, 0, SWP_NOSIZE);
return TRUE;
}
else
{
TRACE0("建立切分窗體失敗!");
return FALSE;
}
return CFrameWnd::OnCreateClient(lpcs, pContext);
}
23添加新的系統菜單
Frame::OnCreate中:
CMenu *pMenu = GetSystemMenu(FALSE);//得到系統菜單的指針
CString menuTitle="新菜單";//菜單标題
pMenu->AppendMenu(MF_SEPARATOR);//插入一個菜單分割符
pMenu->AppendMenu(MF_STRING, IDM_MESSAGEID, menuTitle);//添加菜單,ID随便在stringtable中新定義一個
響應WM_SYSCOMMAND
23标題欄資訊
int buttonSizeX = GetSystemMetrics(SM_CXSIZE);//獲得标題欄按鈕寬度
int buttonSizeY = GetSystemMetrics(SM_CYSIZE);//獲得标題欄按鈕高度
int tilteHeight = GetSystemMetrics(SM_CYCAPTION);//獲得标題欄高度
getWindowText //獲得标題
24圖檔 工具欄
首先。CMainFrame::OnCreate中 工具欄Create時要或上(|)TBSTYLE_TRANSPARENT(第二個參數)
//加載位圖圖像
HBITMAP m_bmpBack = (HBITMAP)LoadImage(AfxGetInstanceHandle(),
"bg.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
// 調用此成員函數來利用Windows rebar通用控件的機能來定制rebar
CReBarCtrl& rc = m_wndReBar.GetReBarCtrl();
REBARBANDINFO info;
memset(&info,0,sizeof(REBARBANDINFO));
info.cbSize = sizeof(info);
info.fMask = RBBIM_BACKGROUND;
info.hbmBack = (m_bmpBack != INVALID_HANDLE_VALUE? m_bmpBack : NULL);
rc.SetBandInfo(0, &info);
rc.UpdateWindow(); //更新資訊
25工具欄 鎖定
MainFrame::Create中 注釋掉
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
26視窗 置頂
// 設定目前主視窗為置頂顯示
::SetWindowPos(AfxGetMainWnd()->m_hWnd, HWND_TOPMOST, -1, -1, -1, -1, SWP_NOMOVE | SWP_NOSIZE);
27打開 檔案 對話框
char filter[] = "圖像 (*.bmp;*.tif;*.jpg)|*.bmp;*.tif;*.jpg|Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";
//檔案對話框
CFileDialog fdlg(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, filter);
fdlg.m_ofn.lpstrInitialDir = "c:\\"; //設定初始檔案目錄
//彈出對話框,如果單擊的是"OK"按鈕
if(fdlg.DoModal() == IDOK)
{
//得到打開的檔案的檔案名
CString fileName = fdlg.GetPathName();
//将檔案路徑顯示在對話框中
GetDlgItem(IDC_STATIC)->SetWindowText("打開檔案:"+fileName);
}
28馬賽卡 顯示
相關:雙緩沖http://www.vckbase.com/document/viewdoc/?id=1612
CScreenWnd* imageWnd = new CScreenWnd; //建立一個新視窗對象
imageWnd->CreatScreenWnd ();//建立視窗
imageWnd->CenterWindow ();//在螢幕中央
imageWnd->ShowWindow (SW_SHOW);//顯示視窗
imageWnd->UpdateWindow ();//更新視窗,激活OnPait函數
if (imageWnd != NULL)
{
imageWnd->SendMessage (WM_CLOSE); //關閉視窗
}
void CScreenWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
MemDC.CreateCompatibleDC(NULL);//建立一個和dc相容的記憶體DC放置位圖
old_bitmap=MemDC.SelectObject(&m_bitmap);//将建立的位圖選入記憶體DC
int i,j,stepx,stepy,dispnum,x,y;
int lwf[20][20]; //數組記錄已顯示過的資料組
for ( i=0; i<20; i++ )
for ( j=0; j<20; j++ )
lwf[i][j]=0;
stepx=bm.bmWidth/20;
stepy=bm.bmHeight/20;
srand( (unsigned)time( NULL ) );
dispnum=0;
//記錄已顯示過的資料組的個數
while(1)
{ x=rand() % 20;
y=rand() % 20;
if ( lwf[x][y] ) //如果為1,則已經顯示了,跳出循環。
continue;
lwf[x][y]=1; //顯示,設定為1
dc.StretchBlt(
x*stepx, y*stepy,
//目标裝置邏輯橫、縱坐标
stepx,stepy,
//顯示位圖的像素寬、高度
&MemDC,
//位圖記憶體裝置對象
x*stepx, y*stepy,
//位圖的起始橫、縱坐标
stepx,stepy,
//位圖的像素寬、高度
SRCCOPY);
dispnum++;
if ( dispnum >=400 )
break;
Sleep(10);
}
MemDC.SelectObject(old_bitmap);
// Do not call CWnd::OnPaint() for painting messages
}
29顔色對話框
COLORREF initColor = RGB(0,0,0);//初始顔色
CColorDialog dlg(0, CC_FULLOPEN, this);
if(dlg.DoModal() == IDOK)
{
initColor = dlg.GetColor();//獲得顔色
}
//更改對話框背景色
CClientDC *pDC = new CClientDC(this);//客戶DC
CRect rect;
GetClientRect(&rect);//得到對話框區域
CBrush brush(initColor);//初始化筆刷
pDC->FillRect(rect, &brush);//更改對話框顔色
delete pDC; //釋放指針
pDC = NULL;
30.響應右鍵菜單
http://www.vckbase.com/document/viewdoc/?id=1923
void CContextMenuWinDlg::OnMenuCopy()
{
// TODO: Add your command handler code here
bCopy = TRUE;
}
void CContextMenuWinDlg::OnUpdateMenuPaste(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(bCopy == FALSE);
}
void CContextMenuWinDlg::OnUpdateMenuCopy(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(bCopy == TRUE);
}
void CContextMenuWinDlg::OnMenuPaste()
{
bCopy = FALSE;
}
重寫OnInitMenuPopup才能使菜單更新
void CContextMenuWinDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
// TODO: Add your message handler code here
int nCount = pPopupMenu->GetMenuItemCount();
CCmdUI cmd;//建立自動更新類
cmd.m_pMenu = pPopupMenu;
cmd.m_nIndexMax = nCount;//MSDN上未介紹的成員變量
for (int i = 0; i < nCount; i ++)
{
UINT nID = pPopupMenu->GetMenuItemID(i);
if (nID != 0)//如果不是分隔符
{
cmd.m_nIndex = i;
cmd.m_nID = nID;
cmd.DoUpdate(this, TRUE);//MSDN上未介紹的成員函數,可以找到自動更新宏,傳入CCmdUI指針,來執行自動更新
}
}
}
31.拖動 無邊框 對話框
UINT CXXXDlg::OnNcHitTest(CPoint point)
{
//截獲滑鼠事件
UINT nHitTest = CDialog::OnNcHitTest(point);
// 判斷是否在拖動客戶區,如果是則發送拖動标題欄的消息
return (nHitTest == HTCLIENT)? HTCAPTION : nHitTest;
}
32.對話框 形狀
初始化時:
GetClientRect(&Rect);
// 設定橢圓形窗體區域
newRgn.CreateEllipticRgn(0,0,Rect.Width(),Rect.Height());
::SetWindowRgn(this->m_hWnd,(HRGN)newRgn,TRUE);
33.查找 替換 對話框
CFindReplaceDialog* pDlg = new CFindReplaceDialog; //定義查找替換對話框指針
pDlg->Create(false, 0, 0, FR_DOWN, this); //建立
如下是建立一個查找視窗的執行個體:
CFindReplaceDialog *pFindDlg = new CFindReplaceDialog; // 由于是非模态對話框這裡用new指令配置設定記憶體
if (!pFindDlg->Create(TRUE,_T( "hyp's Knowledge Base"), NULL,FR_DOWN|FR_MATCHCASE|FR_WHOLEWORD , this)) //上面說明了
{
return;
}
pFindDlg->ShowWindow(SW_SHOW); //視窗建立完畢要顯示出來
pFindDlg->SetActiveWindow(); //設定為活動視窗
如下是建立一個查找替換視窗的執行個體:
CFindReplaceDialog *pReplaceDlg= new CFindReplaceDialog;
if(!pReplaceDlg->Create(FALSE,_T( "hyp"),_T( "hyp's Knowledge Base"),FR_DOWN, this))
{
return;
}
pReplaceDlg->ShowWindow(SW_SHOW);
pReplaceDlg->SetActiveWindow();
顯示的出來的對話框還需要添加一些響應函數吧,我找了一下發現沒有現成的消息響應-_-網上google了一下發現要自己添加我汗.
大體步驟是這樣子的:
在你添加查找替換對話框的頭檔案的#pragma once下面加入
const int WM_FINDREPLACE = ::RegisterWindowMessage(FINDMSGSTRING); //将FINDMSGSTRING注冊為WM_FINDREPLACE消息
并在頭檔案生成的消息映射函數
afx_msg LONG OnFindReplace(WPARAM wParam, LPARAM lParam);
在源檔案裡面将消息映射函數與WM_FINDREPLACE消息關聯
BEGIN_MESSAGE_MAP(...)
...
ON_REGISTERED_MESSAGE(WM_FINDREPLACE, OnFindReplace)
...
END_MESSAGE_MAP()
接下來編寫OnFindReplace函數就OK了
LONG ChypDlg::OnFindReplace( WPARAM wparam, LPARAM lparam )
{
// TODO: 在此添加控件通知處理程式代碼
CFindReplaceDialog* pDlg = CFindReplaceDialog::GetNotifier(lparam);
//具體查找替換代碼就自己編寫
// pDlg->GetFindString(); 該函數可以傳回查找字元串
// pDlg->GetReplaceString(); 該函數可以傳回查找替換串
//pDlg->SearchDown(); 該函數可以判斷是向上還是向下搜尋,TRUE是向下,反之OOXX
//pDlg->MatchCase(); 該函數可以判斷是否區分大小寫
//pDlg->MatchWholeWord(); 該函數可以判斷是否全字比對
if( pDlg != NULL )
{
if( pDlg->FindNext() )
{
MessageBox( "FindNext!", "hyp's Knowledge Base" ,MB_OK);
}
else
if( pDlg->ReplaceAll() )
{
MessageBox( "ReplaceAll!", "hyp's Knowledge Base", MB_OK);
}
else
if( pDlg->ReplaceCurrent() ){
MessageBox( "ReplaceCurrent!", "hyp's Knowledge Base", MB_OK);
}
}
delete pDlg; //加上這個點選完畢以後會自動銷毀對話框
return 1;
} 34.設定字型 void CFontSetDlg::OnButton1()
{
CFontDialog dlg; //字型對話框
dlg.m_cf.lpLogFont = &m_font; //字型
dlg.m_cf.rgbColors = m_textColor; //顔色
dlg.m_cf.Flags |= CF_INITTOLOGFONTSTRUCT;
if(dlg.DoModal() == IDOK)
{
dlg.GetCurrentFont(&m_font); //得到字型
m_textColor = dlg.GetColor(); //得到顔色
CFont font;
font.CreateFontIndirect(&m_font);
GetDlgItem(IDC_STATIC)->SetFont(&font, true); //設定控件字型
}
}
//控件顔色,如果不是每次都改變,隻需要寫在initaildialog中一次
HBRUSH CFontSetDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) //WM_CTLCOLOR
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
//如果控件是IDC_STATIC
if (pWnd->GetDlgCtrlID() == IDC_STATIC)
{
//設定控件顔色
m_textColor = RGB(255,0,0);
pDC->SetTextColor(m_textColor);
pDC->SetBkColor(m_textColor);
pDC->SetBkMode(TRANSPARENT);
}
return hbr;
}
35 CFileDialogEX 自定義 繼承 CFileDialog
CFileDialogEx::CFileDialogEx(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
{
//這個四個常用辨別位
m_ofn.Flags |= OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLETEMPLATE |OFN_HIDEREADONLY;
m_ofn.lpstrTitle = _T("打開圖檔預覽對話框");
m_ofn.hInstance = AfxGetInstanceHandle();
m_ofn.lpstrFilter = _T("圖像(.bmp;.tif;.jpg)\0*.bmp;*.tif;*.jpg\0所有檔案\0*.*\0 ");
m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_DIALOG_HOOK);
m_ofn.lpfnHook = (LPOFNHOOKPROC)ComDlgPreviewProc;
}
void CFileDialogEx::ShowImagepreview(HWND hDlg, char *ImagePath)
{
// CString strTemp;
RECT rect;
//如果用的是Win32的SDK,前面要加::
HDC hDC = ::GetDC(::GetDlgItem(hDlg, IDC_PIC)); //Get the DC for the CPicture Box
::GetClientRect(::GetDlgItem(hDlg, IDC_PIC), &rect); //Get dimensions of it
CPicture pic;
//Try to load the Image..if not then we should return
if(!pic.LoadPicture(ImagePath))
return;
//set the width & height labels
::SetDlgItemInt(hDlg, IDC_WIDTH, pic._GetWidth(), FALSE);
::SetDlgItemInt(hDlg, IDC_HEIGHT, pic._GetHeight(), FALSE);
//Draw the image in the picture box
//(could be updated to maintain aspect ratio)
pic.DrawPicture(hDC, 0, 0, rect.right - rect.left ,rect.bottom - rect.top );
::ReleaseDC(::GetDlgItem(hDlg, IDC_PIC), hDC);
}
LRESULT CALLBACK ComDlgPreviewProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG: //初始化對話框上的控件
if(SendDlgItemMessage(hDlg, IDC_SHOWPREVIEW, BM_GETCHECK, 0, 0) == BST_UNCHECKED)
{
HWND hWnd;
hWnd = GetDlgItem(hDlg, IDC_PIC);
::SetWindowText(hWnd, "\n\n\n\n 點選圖象檔案進行預覽!");
}
break;
case WM_DESTROY: //消毀對話框
{
LPOPENFILENAME lpOFN = (LPOPENFILENAME)GetWindowLong(hDlg, DWL_USER);
}
break;
case WM_COMMAND: //這裡處理,IDC_SHOWPREVIEW指令
if ((HIWORD(wParam) == BN_CLICKED) && (LOWORD(wParam) == IDC_SHOWPREVIEW))
{
if(SendDlgItemMessage(hDlg, IDC_SHOWPREVIEW, BM_GETCHECK, 0, 0) == BST_CHECKED)
{
InvalidateRect(GetDlgItem(hDlg, IDC_PIC), NULL, FALSE);
::SetDlgItemInt(hDlg, IDC_WIDTH, 0, FALSE);
::SetDlgItemInt(hDlg, IDC_HEIGHT, 0, FALSE);
}
}
//不選中,
if(SendDlgItemMessage(hDlg, IDC_SHOWPREVIEW, BM_GETCHECK,0,0) == BST_CHECKED)
{
HWND hWnd;
hWnd = GetDlgItem(hDlg, IDC_PIC);
::SetWindowText(hWnd, "\n\n\n\n 點選圖象檔案進行預覽!");
}
//選中
if(SendDlgItemMessage(hDlg, IDC_SHOWPREVIEW, BM_GETCHECK,0,0) != BST_UNCHECKED)
{
HWND hWnd;
hWnd = GetDlgItem(hDlg, IDC_PIC);
::SetWindowText(hWnd, "\n\n\n\n 處于非預覽狀态!");
}
if(LOWORD(wParam) == IDOK)
{
}
break;
case WM_NOTIFY:
HandleNotify(hDlg, (LPOFNOTIFY)lParam);
default:
return FALSE;
}
return TRUE;
}
//這裡處理notify 消息
BOOL NEAR CALLBACK HandleNotify(HWND hDlg, LPOFNOTIFY pofn)
{
CFileDialogEx dlg(TRUE);
switch (pofn->hdr.code)
{
case CDN_SELCHANGE:
{
char szFile[MAX_PATH];
// Get the path of the selected file.
if (CommDlg_OpenSave_GetFilePath(GetParent(hDlg), szFile, sizeof(szFile)) <= sizeof(szFile))
{
if(GetFileAttributes(szFile) != FILE_ATTRIBUTE_DIRECTORY)
{
//Should we load the Pic
if(SendDlgItemMessage(hDlg, IDC_SHOWPREVIEW, BM_GETCHECK,0,0) == BST_UNCHECKED)
dlg. ShowImagepreview(hDlg, szFile);
}
}
}
break;
case CDN_FILEOK:
return FALSE;
break;
}
return(TRUE);
}