【ACCESS精品源码栏目提醒】:网学会员ACCESS精品源码为您提供VC常用源码 - 教育参考,解决您在VC常用源码 - 教育学习中工作中的难题,参考学习。
VC 常用
源码//注意事项>//先执行 OnInitDialog函数,然后执行构造函数//98 下没有新宋体//XP 下窗口高度加 8//滚动条处理方法//1设置范围m_spscroll.SetScrollRange0200SCROLLINFO sisi.cbSizesizeofSCROLLINFOsi.nPage100si.fMaskSIF_PAGE//设置页宽m_spscroll.SetScrollInfoampsi//2,处理消息,垂直滚动条加 WM_VSCROLL 消息,水平加 WM_HSCROLL 消息void CPrintView::OnVScrollUINT nSBCode UINT nPos CScrollBar pScrollBar// TODO: Add your message handler code here and/or call defaultifpScrollBar-gtGetDlgCtrlIDIDC_SCROLLBAR1int nCurrentPospScrollBar-gtGetScrollPosSCROLLINFO sisi.fMaskSIF_PAGE//取得页宽pScrollBar-gtGetScrollInfoampsiswitchnSBCodecase SB_THUMBTRACK://移动滑块case SB_THUMBPOSITION:pScrollBar-gtSetScrollPosnPos//注意,设置页宽后滚动条的 pos 会以 max/nPage 倍数减少,所以在使用时注意把 pos 值max/nPage才能得到原值breakcase SB_LINEUP://点向上小三角pScrollBar-gtSetScrollPosnCurrentPos-1breakcase SB_LINEDOWN://点向下小三角pScrollBar-gtSetScrollPosnCurrentPos1breakcase SB_PAGEUP://向上一页pScrollBar-gtSetScrollPosnCurrentPos-si.nPagebreakcase SB_PAGEDOWN://向下一页pScrollBar-gtSetScrollPosnCurrentPossi.nPagebreakCDialog::OnVScrollnSBCode nPos pScrollBar//锁定鼠标bool pOldCRect rtSetForegroundWindowSystemParametersInfoSPI_SETSCREENSAVERRUNNINGtrueamppOldSPIF_UPDATEINIFILEGetWindowRectrtClipCursorrt//加到 LRESULT CLockDlg::WindowProcUINT message WPARAM wParam LPARAMlParam 会有意外的效果//在列表字符前插入一个负数字符以修改乱码int indexm_list.GetSelectionMark//在列表字符前插入一个负数字符以修改乱码CString cscsm_list.GetItemTextindex0char insert_char-87cs.Insert0insert_charm_list.SetItemTextindex0cs//在列表中添加项目最大只能显示 259 个字符不含0int char_lengthcs.GetLength//csct 为 CString 类对象,是要发到列表框的文本但是可能大于 259 字节whilechar_lengthgt259//如果大于 259 字节ctcs.Left259m_list.InsertItem0ct//在列表中添加项目最大只能显示 259 个字符不含0cscs.Rightchar_length-259char_lengthcs.GetLengthm_list.InsertItem0cs//在列表中添加项目最大只能显示 259 个字符不含0//<//设置 NT 窗口的透明度OSVERSIONINFO osvosv.dwOSVersionInfoSizesizeof OSVERSIONINFOGetVersionExamposv//取得版本信息ifosv.dwPlatformIdVER_PLATFORM_WIN32_NT//VER_PLATFORM_WIN32_WINDOWS 98 Me 用这个宏//加入 WS_EX_LAYERED 扩展属性SetWindowLongthis-gtGetSafeHwndGWL_EXSTYLEGetWindowLongthis-gtGetSafeHwndGWL_EXSTYLE0x80000//如果多次调用下面这个函数设置,这个函数只在一个位置调用一次就行了HINSTANCE hInst LoadLibraryquotUser32.DLLquotifhInsttypedef BOOL WINAPI MYFUNCHWNDCOLORREFBYTEDWORDMYFUNC fun NULL//取得 SetLayeredWindowAttributes 函数指针funMYFUNCGetProcAddresshInst quotSetLayeredWindowAttributesquotiffunfunthis-gtGetSafeHwnd0200//0 2552FreeLibraryhInst//字体对话框的初始化LOGFONT lflf.lfHeight-35lf.lfCharSet134lf.lfWeight400lf.lfOutPrecision3lf.lfClipPrecision2lf.lfQuality1lf.lfPitchAndFamily2strcpylf.lfFaceNamequot宋体quot//以上初始化为宋体 26 号字CFontDialog cfamplf//字体cf.m_cf.rgbColorstextcolor//颜色//移动没有标题的窗口//1 定义:CPoint just_point//2void CClockfortecherDlg::OnLButtonDownUINT nFlags CPoint point// TODO: Add your message handler code here and/or call defaultjust_pointpointCDialog::OnLButtonDownnFlags point//3void CClockfortecherDlg::OnMouseMoveUINT nFlags CPoint point// TODO: Add your message handler code here and/or call defaultWINDOWPLACEMENT wiGetWindowPlacementampwiifnFlagsMK_LBUTTONSetWindowPosampwndTopwi.rcNormalPosition.leftpoint.x-just_point.xwi.rcNormalPosition.toppoint.y-just_point.y00SWP_NOSIZECDialog::OnMouseMovenFlags point//线程与信号量//1定义信号量句柄HANDLE event//2创建信号量eventCreateEventNULL TRUE FALSE NULL//3创建线程//1定义线程函数,格式必须如下其中 lParam 为 AfxBeginThread 的第二个参数值,可强制转化成所需类型UINT WorkThreadProcLPVOID lParam//必须是 UINT XXX..XXXLPVOID lParam//代码示例 WaitForSingleObject:while1//-----------------------注意,如果线程间要求同步或互斥的时候,要在每一层循环体中加入WaitForSingleObjectWaitForSingleObjectHANDLElParam INFINITE//WaitForSingleObject 的使用方法:第一个为信号量 HANDLE,是 CreateEvent 的返回值第二个参数为等待的毫秒数(1/1000 秒)//第二个参数为 INFINITE 时则一直等待,直到调用 SetEvent设置信号量时函数返回;为数值(如 1000)则函数在 1 秒后返回//(即使你没调用 SetEvent设置信号量)AfxMessageBoxquotfcukquot//不能用 MessageBox因为这不是在类中了...ResetEventHANDLElParam/重置信号量,以使 WaitForSingleObject 函数可以继续等待,否则(如果你已经调用过了SetEvent设置了信号量)WaitForSingleObject 函数将会立刻返回///2用 AfxBeginThread 创建一个 WorkThreadProc 的线程AfxBeginThreadWorkThreadProcevent//4在主程序需要的地方调用 SetEvent设置信号量启动线程SetEventevent//-----------或者用 WaitForMultipleObjects 函数static UINT __stdcall WorkThreadProcvoid pThis/如果 lParam 参数为一个对话框的指针,想调用这个对话框的变量或函数那么就得这样定义线程函数,还要将 WorkThreadProc 改成 CWait_forDlg::WorkThreadProc,这样 WorkThreadProc 就成为 CWait_forDlg 类的函数,在这个线程里就可以调用该类的变量了,注意得用_beginthreadex 函数创建线程/UINT CWait_forDlg::WorkThreadProcvoid lParamCWait_forDlg pThisCWait_forDlg lParamHANDLE hObjects2hObjects0 pThis-gtevent1hObjects1 pThis-gtevent2while1DWORD dwWait WaitForMultipleObjects2hObjectsTRUEINFINITE/第一个参数为信号量个数 2,第二个为指针,第三个如果为 TRUE 函数要等待两个信号量都被 SetEvent 才返回,返回值为最后一个SetEvent 的 WAIT_OBJECT_0i;而为 FALSE 则只要有一个被 SetEvent 就返回,返回值为 WAIT_OBJECT_0i 即信号量在数组中的位置WAIT_OBJECT_0 /if dwWait WAIT_OBJECT_0AfxMessageBoxquotfcuk 1 quot//开始 pingif dwWait WAIT_OBJECT_0 1AfxMessageBoxquotfcuk 2quotResetEventhObjects1ResetEventhObjects0include ltprocess.hgt / 调用_beginthread _endthread 得包涵这个头文件/_beginthreadexNULL0WorkThreadProcvoid this00//第三种创建线程的方法:HANDLE threadDWORD threadrid //线程 IDDWORD WINAPI sniffLPVOID no //线程函数这样定义threadCreateThreadNULL //安全属性0 //栈大小sniff //要创建的线程名NULL //参数(一般为调用线程的指针)0 //创建标志ampthreadrid //线程 IDCloseHandlethread//操作数据库//在 stdafx.h 中加入include ltafxdb.hgt//1,用类向导,建立基于 CRecordset 或 CDaoRecordset 的新子类并选择数据源m_setComplete//2,添加if m_setComplete . IsOpen // if the recordset isnt already open..m_setComplete . Open // open itm_setComplete . AddNew // begin the addm_setComplete . m_strCallsign strCallsign // change the recordset membersm_setComplete . m_strFrequency strFrequencym_setComplete . m_strCity strCitym_setComplete . m_strState strStatem_setComplete . m_strInput strInputm_setComplete . Update // complete the add by doing an updatem_setComplete . Close // close the recordset//3修改if m_setComplete . IsOpen // if the recordset isnt already open..m_setComplete . Open // open itm_setComplete . Edit // begin the editm_setComplete . m_strCallsign strCallsign // change the recordset membersm_setComplete . m_strFrequency strFrequencym_setComplete . m_strCity strCitym_setComplete . m_strState strStatem_setComplete . m_strInput strInputm_setComplete . Update // complete the add by doing an updatem_setComplete . Close // close the recordset//4,删除/ 1 DAO 数据库,不是 ODBCif m_setComplete . IsOpen m_setComplete . Open // cycle through the selected listbox elements.strRecordIdQuery CString quotIDquot CString m_lcRepeaterList . GetItemText nItemIndex 0 // put the ID into the query stringMessageBoxstrRecordIdQueryif m_setComplete . FindFirst strRecordIdQuery // looking for this ID in thedatabase ID is a unique autonumberm_setComplete . Delete // delete the record m_setComplete . MoveFirst // move back to the first recordm_bRecordsWereDeleted TRUE // make a note that we changed the databaseSetDlgItemText IDC_DELETE_STATUS quotRepeater Deleted From Databasequot //set the status fieldelse // if we EVER end up here either the database is in the crapper or I will havescrewed up horribly--been known to happen from time to time : ...// so lets cover our ass-ets just in case.AfxMessageBox quotInternal failurennCannot find selected repeater in databasenordatabase is corruptedquot MB_ICONSTOP m_setComplete . Close // close the database/// ODBC herem_setComplete.m_strFilterquotnumber200301quot//条件查询(where 语句)if m_setComplete . IsOpen m_setComplete . Open m_setComplete.Deletem_setComplete . MoveFirst m_setComplete . Close // close the databasem_setComplete.m_strFilterquotquot//清空条件(where 语句)//查询(where 语句)m_setComplete.m_strFilterquotnumber200301quotif m_setComplete . IsOpen m_setComplete . Open m_setComplete . Close // close the database//不用类向导写连接数据库的程序段,但是我只看明白了连接和查询,不会修改和添加,删除//1CDatabase m_dbCust //定义数据库类对象m_dbCust.OpenEx_TquotDSNMQISUIDsaPWD1980623quot//打开数据库 //数据源名,用户名,密码CDatabase::forceOdbcDialog //此参数只定是否打开连接确认对话框//MessageBoxm_dbCust.GetDatabaseName 取得数据源名//m_dbCust.ExecuteSQLquotselect number from worksquot测试是否支持 SQL 语句CRecordset csampm_dbCust//定义目录查询对象cs.Open CRecordset::dynaset_T quotselect from worksquot //打开时执行的 SQL 语句short nFields cs.GetODBCFieldCount //取得字段数,(列数)CDBVariant varValue//定义通用数据类型CODBCFieldInfo co//定义字段信息CString ccwhile cs.IsEOF for short index 0 index lt nFields index // do something with varValuecs.GetFieldValueindexvarValue//取得某列的数据,//cs.GetFieldValueindexcc 也可直接取得由某列的数据直接转化成的文本ifvarValue.m_dwTypeDBVT_LONG//m_dwType 成员用来判断数据类型/ m_dwType Union data memberDBVT_NULL No union member is valid for
access.DBVT_BOOL m_boolValDBVT_UCHAR m_chValDBVT_SHORT m_iValDBVT_LONG m_lValDBVT_SINGLE m_fltValDBVT_DOUBLE m_dblValDBVT_DATE m_pdateDBVT_STRING m_pstringDBVT_BINARY m_pbinary/cc.FormatquotdquotvarValue.m_lVal//m_dwTypeDBVT_LONG 时为 LONG 型数据,成员为 m_lVal,数值在其中MessageBoxcccs.GetODBCFieldInfoindexco//取得字段信息/ struct CODBCFieldInfoCString m_strName 字段名SWORD m_nSQLType SQL 数据类型UDWORD m_nPrecisionSWORD m_nScaleSWORD m_nNullability/cs.MoveNext //下一个cs.Close//显示程序段CString cstrcstr.Formatquotdquot MessageBoxcstrifMessageBoxquot关闭 Windows 秘书您可会失去重要的提醒信息确定要关闭吗quotquotWindows 秘书quotMB_OKCANCELMB_DEFBUTTON2MB_ICONWARNINGIDOKMessageBeepMB_ICONQUESTION//注册与卸载 OCX 控件//1,函数入口宏定义typedef HRESULT STDAPICALLTYPE CTLREGPROC//2,注册函数BOOL RegisterOcxCString ocxfilenameBOOL bResult FALSE HMODULE hModule ::LoadLibraryocxfilename //获得注册函数地址CTLREGPROC DLLRegisterServer CTLREGPROC::GetProcAddresshModulequotDllRegisterServerquot if DLLRegisterServer NULLHRESULT regResult DLLRegisterServer bResult regResult NOERROR ::FreeLibraryhModule return bResult //2,卸载函数BOOL UnRegisterOcxCString ocxfilenameBOOL bResult FALSE HMODULE hModule ::LoadLibraryocxfilename //获得卸载函数地址CTLREGPROC DLLUnregisterServer CTLREGPROC::GetProcAddress hModulequotDllUnregisterServerquot if DLLUnregisterServer NULLHRESULT regResult DLLUnregisterServer bResult regResult NOERROR ::FreeLibraryhModule return bResult //处理文件拖动//1 添加 WM_DROPFILES 静态消息//2 在 OnInitDialog函数中加入DragAcceptFilesTRUE//允许拖放//3 WM_DROPFILES 的处理函数为void CCXatDemoDlg::OnDropFilesHDROP hDropInfounsigned int nFilesDragQueryFilehDropInfo0xFFFFFFFFNULL0//取得拖放的文件总数for unsigned int i0iltnFilesi//循环取得文件名unsigned int nLenDragQueryFilehDropInfoiNULL01//取得文件名长度char psBuffernew charnLenunsigned int sLenDragQueryFilehDropInfoipsBuffernLen//取得文件名到psBuffer 中sLen 为实际拷贝的字符数//To add code here...delete psBufferDragFinishhDropInfo//结束//设置窗口大小和位置SetWindowPosampwndTopMost0000SWP_NOSIZESWP_NOMOVE//设为最高层/wndBottomwndTopwndTopMostwndNoTopMost///在窗体中这样做 HWND_NOTOPMOST 为不是最高层::SetWindowPosAfxGetMainWnd-gtm_hWndHWND_TOPMOST-1-1-1-1SWP_NOMOVESWP_NOSIZE/HWND_BOTTOMHWND_NOTOPMOSTHWND_TOPHWND_TOPMOSTSWP_DRAWFRAME Draws a frame defined in the class description of the windowaround the window.SWP_FRAMECHANGED Sends a WM_NCCALCSIZE message to the window even ifthe size of the window is not being changed. If this flag is not specifiedWM_NCCALCSIZE is sent only when the size of the window is being changed.SWP_HIDEWINDOW Hides the window.SWP_NOACTIVATE Does not activate the window. If this flag is not set the windowis activated and moved to the top of either the topmost or nontopmost groupdepending on the setting of the hWndInsertAfter parameter.SWP_NOMOVE Retains the current position and ignores the X and Y parameters.SWP_NOOWNERZORDER Does not change the position in the Z order of the ownerwindow.SWP_NOREPOSITION Same as the SWP_NOOWNERZORDER flag.SWP_NOSIZE Retains the current size and ignores the cx and cy parameters.SWP_NOZORDER Retains the current Z order and ignores the hWndInsertAfterparameter.SWP_SHOWWINDOW Displays the window.///在系统栏显示图标//1 定义结构NOTIFYICONDATA systemicon//2 定义新的消息define WM_SYSTEMMESSAGE WM_USER998//3 添加消息函数映射ON_MESSAGEWM_SYSTEMMESSAGEOnSystemMessage//4 初始化结构systemicon.cbSizesizeofNOTIFYICONDATAsystemicon.hWndthis-gtm_hWndsystemicon.uIDIDI_SYSICON //图标systemicon.uFlagsNIF_MESSAGENIF_ICONNIF_TIPsystemicon.uCallbackMessageWM_SYSTEMMESSAGEsystemicon.hIconLoadIconAfxGetInstanceHandleMAKEINTRESOURCEIDI_SYSICONstrcpysystemicon.szTipquotWindows 秘书quotShell_NotifyIconNIM_ADDampsystemicon//显示strcpysystemicon.szTipcsShell_NotifyIconNIM_MODIFYampsystemicon//修改Shell_NotifyIconNIM_DELETEampsystemicon//删除//处理函数void OnSystemMessageWPARAM wParam LPARAM lParamifwParamIDI_SYSICONreturnswitchlParamcase WM_RBUTTONUP://右键起来时弹出快捷菜单CMenu menuNULLLPPOINT mousepointnew tagPOINT::GetCursorPosmousepoint//得到鼠标位置BCMenu bmpmenubmpmenu.LoadMenuIDR_SYSPOPUPMENUmenu bmpmenu.GetSubMenu 0//确定弹出式菜单的位置ifIsWindowVisiblebmpmenu.ModifyODMenuAquot隐藏主窗口quotID_SHOWHIDEIDB_HIDEelsebmpmenu.ModifyODMenuAquot显示主窗口quotID_SHOWHIDEIDB_SHOWiffindtxdata-gtstoplowstep0bmpmenu.ModifyODMenuAquot停止普通级提醒quotID_STOPALLPTIDB_STOPelsebmpmenu.ModifyODMenuAquot启用普通级提醒quotID_STOPALLPTIDB_NOTSTOPiftopornotbmpmenu.ModifyODMenuAquot窗体不在最上层quotID_NOTONTOPIDB_NOTTOPelsebmpmenu.ModifyODMenuAquot窗体在最上层quotID_NOTONTOPIDB_TOPifmuteornotbmpmenu.ModifyODMenuAquot静音quotIDMUTEIDB_NOSOUNDelsebmpmenu.ModifyODMenuAquot取消静音quotIDMUTEIDB_SOUNDSetForegroundWindow//必须先调用这个函数才能如果我不选择任何菜.