1、网易掌握C+教程教你如何更好的掌握C+的基础2011-03-16 16:59:13|分类:默认分类|标签:it动态|字号订阅1. 如何获取系统日期CTime tm = CTime : GetCurrentTime();CString strTime = tm.Format(_TEXT(“%Y-%M-%d %H:%M:%S);MessageBox(strTime);2. 动态分配二维数组int *array;array = new int*col;if(array = = 0) exit(-1);for(int i=0; icol; i+)arrayi = new introw;if(array
2、i = = 0) exit(-1); /释放for(i=0; icol; i+)delete arrayi; delete array;3. 如何定义一个二维字符串数组 char*str = “abcdef” ; /一维 char*strrowcol=“abc”, “cdf”, “efg”.; /rowcol个4. 将一个十六进制”FF”、”FA”等字符串数组等转化为十进制数组int array3232;int a,b, n=0;int result1;char *str;for(int i=0; i32; i+)for(int j=0; j= A & str1 =A)/”AF” a = st
3、r0 A; b= str1 A; result1 = (a+10)*16+(b+10)*1; else if(str0 = A & str1 A) /”A9” a = str0 A; b = A str1 +1; result1 = (a+10)*16+b; else if(str0 =A) /”8B” a = A str0+1; b = str1 A; result1 = a*16+(b+10)*1; else /”87” a = A str0+1; b = A str1 +1; result1 = a*16+b; arrayij = reslut1; printf(“%5d”, array
4、ij); n+; if(n%32 = = 0)printf(“n”); 将这个矩阵数组在屏幕上显示:i nclude i nclude i nclude i nclude DWORD color_;HDC hMyDC = GetDC(NULL);for(i=0; i32; i+)for(j=0; j SetRange(0,255,TRUE); /设置滑动条的范围pSliderCtrl - SetPos(100); / 设置滑动条的初始位置在Dlg类中响应WM_HSCROLL消息:CSliderCtrl *pSliderCtrl=(CSliderCtrl*)GetDlgItem(IDC_SLID
5、ER1);m_nCur = pSliderCtrl - GetPos(); /获得当前的位置值10. 更改光标: SetCursor(: LoadCursor(NULL, IDC_SIZEALL);/如果是自己定义的光标资源,则要用MAKEINTRESOURCE 进行转化,例如:: SetCursor(: LoadCursor(NULL, IDC_CURSOR_CUT);11. 设置文本显示的一些函数CDCdc(this);CPenpen(PS_SOLID, 2, #0000ff);/初始化一支笔CBrush*pBush = CBrush : FromHandle(HBRUSH)GetStoc
6、kObject(NULL_BRUSH);dc.SelectObject(pBrush);dc.SetTextColor(RGB(255,0 ,255);dc.SetBKMode(TRANSPARENT); /将文本背景设置为透明12. 将对话框中的图像拷贝到剪贴板CWnd* pWnd = GetDlgItem(IDC_IMAGE_SELECT);WINDOWPLACEMENT *winPlacement;WinPlacement = new WINDOWPLACEMENT;pWnd - GetWindowPlacement(winPlacement); /获得一个控件的位置CDC *dcTem
7、p;dcTemp= new CClientDC(FromHandle(m_hWnd);CDCmemDC;memDC.CreateCompatibleDC(dcTemp); /创建一个兼容的DCCBitmapbm;CSizesz(lWidth, lHeight);bm.CreateCompatilbleBitmap(dcTemp, sz.cx, sz.cy); /创建一个兼容的位图CBitmap* oldbm = memDC.SelectObject(&bm);memDC.BitBlt(0, 0, sz.cx, sz.cy, dcTemp, winPlacement -rcNormalPosit
8、ion.left, winPlacement -rcNormalPosition.top, SRCCOPY);pWnd - OpenClipboard();/打开剪贴板,不用pWnd-GetParent()-OpenClipard();: EmptyClipard();: SetClipardData(CF_BITMAP, bm.m_hObject); /粘贴到剪贴板CloseClipard();memDC.SelcetObject(oldbm);delete dcTemp;13. VC里获取一个文件夹路径BROWSEINFObi;TCHARszDisplayNameMAX_PATH;LPIT
9、EMIDLISTpidl;LPMALLOCpMalloc = NULL;ZeroMemory(&bi, sizeof(bi);bi.hWndOwner = GetSafeHwnd();bih.pszDisplayName = szDisplayName;bi.lpszTitle = TEXT(“Please select a folder:”);bi.ulFlags = BIF_RETURNONLYFSDIRS;Pidl = SHBrowseForFolder(&bi);if(pidl) SHGetPathFromIDList(pidl, szDisplayName); sPath = szD
10、isplayName; MessageBox(sPath); 14. 如何设置密码输入时显示的是*号i nclude i nclude char a8;void main() int i=0;Cout”请输入密码:”=8 | ai= = 13) break; putch(*); i+; coutendla m_hWnd, WM_COMMAND, ID_FILE_CLOSE,0);获取主窗口的指针CWinThread : m_pMainWnd调用AfxGetMainWnd()可实现 继续上面的总结! To making it count!。1. 获取CMain类的指针CMain* pApp =
11、(CMain*)AfxGetApp()-m_pMainWnd);ASSERT_KINDOF(CMain, pAPP); /确保pAPP是CMain的类对象2. VC+如何获取应用程序的实例句柄实例句柄保存在CWinAppm_hInstance中HANDLEhInstance = AfxGetInstanceHandle();3. VC+怎样加载其他的应用程序 三个SDK函数WinExec, ShellExecute, CreateProcess. WinExec最简单,前一个指定路径,后一个指定显示方式; ShellExecute(null, null, _T(“1.txt”), NULL,
12、_T(“c:temp”), SW_SHOWNORMAL); STARTUPINFOstinfo; /启动窗口的信息PROCESSINFOproinfo; /进程的信息CreateProcess(NULL,_T(“notepad.exe)”,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&stinfo,&proinfo);4. 如何确定顶层菜单所占据的菜单行数:首先,计算主窗口的高度和客户区高度;其次,从主框窗口的高度中减去客户区、框边界以及标题的高度;最后,除以菜单栏的高度CRect rc, rcClient;GetWindowRect(rc)
13、;GetClientRect(rcClient);Int menuHeight;menuHeight = (rc.Height() rcClicent.Height():GetSystemMetrics(SM_CYCAPTION) : GetSystemMetrics(SM_CY)*2)- / : GetSystemMetrics(SM_CYMENU) ;5. 响应下拉菜单的消息为ON_CBN_SELECTDOK(ID_TOOL_ZOOM, OnSelectZoomed)6. 设置工具栏的标题m_wndTestBar.SetWindowText(“your toolbar title”);7.
14、 如何获得应用程序主窗口的指针CWndpMainWnd = AfxGetApp() -m_pMainWnd;CMain *pMain = (CMain*)pMainWnd;CRect rect;CWnd* pParent = AfxGetApp()-GetMainWnd();pParent -GetWindowRect(&rect); /得到应用程序窗口矩形/移到窗口pParent - MoveWindow(rect.left, rect.top, rect.Width()+1,rect.Height()+1,TRUE);8. 获得获得子窗口CMDIChildWnd* pChild = (CM
15、DIChildWnd*)GetActive();/或:CMDIChildWnd* pChild=MDIGetActive();9. 获得活动子窗口的活动视图CMyView* pView = (CMyView*)pChild-GetActiveView();获取当前窗口的指针CWnd : GetForegoundWindow();10. 从句柄转换到指针HWND hwnd;hwnd = : FindWindow(NULL, “TEST”);CWnd* pWnd = FromHandle(hwnd);11. 怎样改变进度条控件的颜色,发送消息m_progress.SendMessage(PBM_S
16、ETBKCOLOR, 0, #ff0000); /背景色m_progress.SendMessage(PBM_SETBARCOLOR, 0, #00ff00); /前景色12. 定义char num10; sprintf(num, “%d”, calnum); /格式化数字 charunit=”矩形”; sprintf(num, “%s”, unit);/格式化字符串13. 改变字符串的显示字体和大小CFont font;font.CreatePointFont(300, “华文行楷”,NULL);CFont *pOldFont=dc.SelectObject(&font);dc.Select
17、Object(pOldFont);14. 将路径中的单变为CMyDoc *pDoc=GetDocument();CString reportPath = pDoc - strPathName;int lentemp = reportPath.GetLength();reportPath = reportPath.Left(lentemp-4);int lenpath = reportPath.GetLength();for(int i=0; i m_pMainWnd-GetSafeHwnd();int xSrc, ySrc;xSrc = GetDeviceCaps(hSrcDC, HORIRE
18、S); /水平分辨率ySrc = GetDeviceCaps(hSrcDC, VERTRES); /垂直分辨率16. 让Edit控件响应回车键:利用获取消息来完成,调用虚函数PreTranslateMessage(MSG* pMsg) If( pMsg -message = = WM_KEYDOWN) switch(pMsg - wParam) caseVK_RETURN: GetDlgItem(IDC_BTN_INPUT)-SendMessage(BM_CLICK, 0, 0); return TRUE; 17. 改变对话框的背景色:在InitInstance()中加入SetDialogBK
19、Color(#a0b4dc, #ff0000); /后面是字体颜色18. 让指定的矩形框重画 InvalidateRect(&rect, TRUE);19. 怎样选择所选的List当前位置intiTem;POSITIONpos=m_findCtrl.GetFirstSelectedItemPosition();if(pos = = 0) MessageBox(“请选择需要删除的记录”);return;else iTem = m_findCtrl.GetNextSelectedItem(pos); /删除List中的某行m_findCtrl.DeleteItem(iTem);m_findCtrl
20、.Update(iTem);20. 动态创建控件CEditm_edit;CRectrect(0,0,100,200);M_edit.Create(WS_CHILD | ES_AUTOHCROLL | WS_BORDER, rect, this, ID_EDIT_1);21. 列表控件的应用CListCtrl*m_list;CRectrect_list;this-GetCientRect(&rect_list);rect_list.top + =100;m_list - Create(WS_CHILD | LVS_REPORT | WS_BORDER | LVS_SINGLESEL, rect_
21、list, this, ID_TABLIST);this -m_list-ModifyStyle(LVS_EDITABELS,0L); /禁止标题编辑m_list-ModifyStyle(0L ,LVS_REPORT); /设置为Report类型m_list-ModifyStyle(0L, LVS_SHOWSELALWAYS); /始终高亮显示被选中的表项m_list-ModifyStyle(0L,LVS_NOSORTHEADER); /禁止标题编辑m_list-SetBkColor(#00c8c8); /设置背景颜色m_list-SetTextBkColor(#00c8c8); /设置文本背
22、景颜色m_list-ModifyStyle(LVS_OWNERDRAWFIXED,0L);m_list-SetExtendedStyle(LVS_EX_FULLROWSELET/整行选中 |LVS_EX_HEADERDARGDROP/允许整列拖动 |LVS_EX_GRIDLINES /画出网格线 |LVS_EX_FLATSB);/ 扁平风格的滚动条22. 取得当前获得焦点的窗口句柄HWNDhwnd = :GetFocus();intiID = : GetDlgCtrlID(hwnd); / 根据句柄取得其资源符号23. 修改控件的字体CFont m_font;m_font.CreateFont
23、(-12,0,0,0,400,FALSE,FALSE,0,GB2312_SHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH | FF_, “楷体_GB2312”);/为想改变字体的控件改变m_editPh.SetFont(&m_font, true);1. 如何暂停主线程直到第二个线程的终止?void CMyTestDialog :PeekMessageLoop() MSGmsg; while ( PeekMessage(&msg, NULL, NULL, NULL, NULL,NULL, PM_REMOVE) TranslateMessage(&msg); DispatchMessage(&msg); Void CMyTestDialog : WaitForThreadToTerminate( HANDLE hT
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1