VC常用源码.docx
《VC常用源码.docx》由会员分享,可在线阅读,更多相关《VC常用源码.docx(47页珍藏版)》请在冰豆网上搜索。
![VC常用源码.docx](https://file1.bdocx.com/fileroot1/2022-12/29/fc33757a-5d8e-4191-910c-5ef7207bf88a/fc33757a-5d8e-4191-910c-5ef7207bf88a1.gif)
VC常用源码
VC常用源码
//注意事项===============================================>
//先执行OnInitDialog()函数,然后执行构造函数!
//98下没有新宋体
//XP下窗口高度加8
//滚动条处理方法**********************************************************************
//1,设置范围
m_spscroll.SetScrollRange(0,200);
SCROLLINFOsi;
si.cbSize=sizeof(SCROLLINFO);
si.nPage=100;
si.fMask=SIF_PAGE;//设置页宽
m_spscroll.SetScrollInfo(&si);
//2,处理消息,垂直滚动条加WM_VSCROLL消息,水平加WM_HSCROLL消息
voidCPrintView:
:
OnVScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
if(pScrollBar->GetDlgCtrlID()==IDC_SCROLLBAR1)
{
intnCurrentPos=pScrollBar->GetScrollPos();
SCROLLINFOsi;
si.fMask=SIF_PAGE;//取得页宽
pScrollBar->GetScrollInfo(&si);
switch(nSBCode)
{
caseSB_THUMBTRACK:
//移动滑块
caseSB_THUMBPOSITION:
pScrollBar->SetScrollPos(nPos);//注意,设置页宽后滚动条的pos会以max/nPage倍数减少,所以在使用时注意把pos值*(max/nPage)才能得到原值
break;
caseSB_LINEUP:
//点向上小三角
pScrollBar->SetScrollPos(nCurrentPos-1);
break;
caseSB_LINEDOWN:
//点向下小三角
pScrollBar->SetScrollPos(nCurrentPos+1);
break;
caseSB_PAGEUP:
//向上一页
pScrollBar->SetScrollPos(nCurrentPos-si.nPage);
break;
caseSB_PAGEDOWN:
//向下一页
pScrollBar->SetScrollPos(nCurrentPos+si.nPage);
break;
}
}
CDialog:
:
OnVScroll(nSBCode,nPos,pScrollBar);
}
//锁定鼠标****************************************************************************
boolpOld;
CRectrt;
SetForegroundWindow();
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,true,&pOld,SPIF_UPDATEINIFILE);
GetWindowRect(rt);
ClipCursor(rt);
//加到LRESULTCLockDlg:
:
WindowProc(UINTmessage,WPARAMwParam,LPARAMlParam)会有意外的效果
//在列表字符前插入一个负数字符以修改乱码******************************************************
intindex=m_list.GetSelectionMark();//在列表字符前插入一个负数字符以修改乱码
CStringcs;
cs=m_list.GetItemText(index,0);
charinsert_char=-87;
cs.Insert(0,insert_char);
m_list.SetItemText(index,0,cs);
//在列表中添加项目最大只能显示259个字符(不含'\0')*************************************************
intchar_length=cs.GetLength();//cs,ct为CString类对象,是要发到列表框的文本但是可能大于259字节
while(char_length>259)//如果大于259字节
{
ct=cs.Left(259);
m_list.InsertItem(0,ct);//在列表中添加项目最大只能显示259个字符(不含'\0')
cs=cs.Right(char_length-259);
char_length=cs.GetLength();
}
m_list.InsertItem(0,cs);//在列表中添加项目最大只能显示259个字符(不含'\0')
//<==========================================================
//设置NT窗口的透明度*******************************************
OSVERSIONINFOosv;
osv.dwOSVersionInfoSize=sizeofOSVERSIONINFO;
GetVersionEx(&osv);//取得版本信息
if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT)//VER_PLATFORM_WIN32_WINDOWS98Me用这个宏
{
//加入WS_EX_LAYERED扩展属性
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);//如果多次调用下面这个函数设置,这个函数只在一个位置调用一次就行了
HINSTANCEhInst=LoadLibrary("User32.DLL");
if(hInst)
{
typedefBOOL(WINAPI*MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNCfun=NULL;
//取得SetLayeredWindowAttributes函数指针
fun=(MYFUNC)GetProcAddress(hInst,"SetLayeredWindowAttributes");
if(fun)fun(this->GetSafeHwnd(),0,
200,//0~255
2);
FreeLibrary(hInst);
}
}
//字体对话框的初始化*********************************************
LOGFONTlf;
lf.lfHeight=-35;
lf.lfCharSet=134;
lf.lfWeight=400;
lf.lfOutPrecision=3;
lf.lfClipPrecision=2;
lf.lfQuality=1;
lf.lfPitchAndFamily=2;
strcpy(lf.lfFaceName,"宋体");//以上初始化为宋体26号字
CFontDialogcf(&lf);//字体
cf.m_cf.rgbColors=textcolor;//颜色
//移动没有标题的窗口**********************************************
//1定义:
CPointjust_point;
//2
voidCClockfortecherDlg:
:
OnLButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
just_point=point;
CDialog:
:
OnLButtonDown(nFlags,point);
}
//3
voidCClockfortecherDlg:
:
OnMouseMove(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
WINDOWPLACEMENTwi;
GetWindowPlacement(&wi);
if(nFlags==MK_LBUTTON)
SetWindowPos(&wndTop,
wi.rcNormalPosition.left+(point.x-just_point.x),
wi.rcNormalPosition.top+(point.y-just_point.y),
0,0,SWP_NOSIZE);
CDialog:
:
OnMouseMove(nFlags,point);
}
//线程与信号量***************************************************
//1,定义信号量句柄
HANDLE event;
//2,创建信号量
event=CreateEvent(NULL,TRUE,FALSE,NULL);
//3,创建线程,
//1)定义线程函数,格式必须如下,其中lParam为AfxBeginThread的第二个参数值,可强制转化成所需类型
UINTWorkThreadProc(LPVOIDlParam)//必须是UINTXXX..XXX(LPVOIDlParam)
{
//代码示例WaitForSingleObject:
while
(1)
{//-----------------------注意,如果线程间要求同步或互斥的时候,要在每一层循环体中加入WaitForSingleObject
WaitForSingleObject((HANDLE)lParam,INFINITE);
//WaitForSingleObject的使用方法:
第一个为信号量HANDLE,是CreateEvent的返回值,第二个参数为等待的毫秒数(1/1000秒)
//第二个参数为INFINITE时则一直等待,直到调用SetEvent()设置信号量时函数返回;为数值(如1000)则函数在1秒后返回
//(即使你没调用SetEvent()设置信号量)
AfxMessageBox("fcuk");//不能用MessageBox()因为这不是在类中了...
ResetEvent((HANDLE)lParam);
/*重置信号量,以使WaitForSingleObject函数可以继续等待,否则(如果你已经调用过了SetEvent()设置了信号量)
WaitForSingleObject函数将会立刻返回
*/
}
}
//2)用AfxBeginThread创建一个WorkThreadProc的线程
AfxBeginThread(WorkThreadProc,event);
//4,在主程序需要的地方调用SetEvent()设置信号量启动线程
SetEvent(event);
//-----------或者用WaitForMultipleObjects函数
staticUINT__stdcallWorkThreadProc(void*pThis);/*如果lParam参数为一个对话框的指针,想调用
这个对话框的变量或函数那么就得这样定义线程函数,
还要将WorkThreadProc改成CWait_forDlg:
:
WorkThreadProc,
这样WorkThreadProc就成为CWait_forDlg类的函数,在这个线程里就可以调用该类的变量了,
注意得用_beginthreadex函数创建线程*/
UINTCWait_forDlg:
:
WorkThreadProc(void*lParam)
{
CWait_forDlg*pThis=(CWait_forDlg*)lParam;
HANDLEhObjects[2];
hObjects[0]=pThis->event1;
hObjects[1]=pThis->event2;
while
(1)
{
DWORDdwWait=WaitForMultipleObjects(2,hObjects,TRUE,INFINITE);
/*第一个参数为信号量个数2,第二个为指针,第三个如果为TRUE函数要等待两个信号量都被SetEvent才返回,返回值为最后一个
SetEvent的WAIT_OBJECT_0+i;而为FALSE则只要有一个被SetEvent就返回,返回值为WAIT_OBJECT_0+i即信号量在数组中的位置
+WAIT_OBJECT_0*/
if(dwWait==WAIT_OBJECT_0)
AfxMessageBox("fcuk1");
//开始ping
if(dwWait==WAIT_OBJECT_0+1)
AfxMessageBox("fcuk2");
ResetEvent(hObjects[1]);
ResetEvent(hObjects[0]);
}
}
#include/*调用_beginthread,_endthread得包涵这个头文件*/
_beginthreadex(NULL,
0,
WorkThreadProc,
(void*)this,
0,
0);
//第三种创建线程的方法:
HANDLEthread;
DWORDthreadrid;//线程ID
DWORDWINAPIsniff(LPVOIDno){}//线程函数这样定义
thread=CreateThread(NULL,//安全属性
0,//栈大小
sniff,//要创建的线程名
NULL,//参数(一般为调用线程的指针)
0,//创建标志
&threadrid);//线程ID
CloseHandle(thread);
//操作数据库**************************************************
//在stdafx.h中加入
#include
//1,用类向导,建立基于CRecordset或CDaoRecordset的新子类,并选择数据源m_setComplete
//2,添加
if(!
m_setComplete.IsOpen())//iftherecordsetisn'talreadyopen..
m_setComplete.Open();//openit
m_setComplete.AddNew();//begintheadd
m_setComplete.m_strCallsign=strCallsign;//changetherecordsetmembers
m_setComplete.m_strFrequency=strFrequency;
m_setComplete.m_strCity=strCity;
m_setComplete.m_strState=strState;
m_setComplete.m_strInput=strInput;
m_setComplete.Update();//completetheaddbydoinganupdate
m_setComplete.Close();//closetherecordset
//3,修改
if(!
m_setComplete.IsOpen())//iftherecordsetisn'talreadyopen..
m_setComplete.Open();//openit
m_setComplete.Edit();//begintheedit
m_setComplete.m_strCallsign=strCallsign;//changetherecordsetmembers
m_setComplete.m_strFrequency=strFrequency;
m_setComplete.m_strCity=strCity;
m_setComplete.m_strState=strState;
m_setComplete.m_strInput=strInput;
m_setComplete.Update();//completetheaddbydoinganupdate
m_setComplete.Close();//closetherecordset
//4,删除
/*1,DAO数据库,不是ODBC
if(!
m_setComplete.IsOpen())
m_setComplete.Open();
//cyclethroughtheselectedlistboxelements.
strRecordIdQuery=CString("[ID]=")+CString(m_lcRepeaterList.GetItemText(nItemIndex,0));//puttheIDintothequerystring
MessageBox(strRecordIdQuery);
if(m_setComplete.FindFirst(strRecordIdQuery)){//lookingforthisIDinthedatabase,IDisaunique'autonumber'
m_setComplete.Delete();//deletetherecord
m_setComplete.MoveFirst();//movebacktothefirstrecord
m_bRecordsWereDeleted=TRUE;//makeanotethatwechangedthedatabase
SetDlgItemText(IDC_DELETE_STATUS,"RepeaterDeletedFromDatabase");//setthestatusfield
}
else{
//ifweEVERenduphere,eitherthedatabaseisinthecrapper,orIwillhavescreweduphorribly--beenknowntohappenfromtimetotime:
)...
//solet'scoverourass-etsjustincase.
AfxMessageBox("Internalfailure\n\nCannotfindselectedrepeaterindatabase\nordatabaseiscorrupted",MB_ICONSTOP);
}
m_setComplete.Close();//closethedatabase*/
//ODBChere
m_setComplete.m_strFilter="number=200301";//条件查询(where语句)
if(!
m_setComplete.IsOpen())
m_setComplete.Open();
m_setComplete.Delete();
m_setComplete.MoveFirst();
m_setComplete.Close();//closethedatabase
m_setComplete.m_strFilter="";//清空条件(where语句)
//查询(where语句)
m_setComplete.m_strFilter="number=200301";
if(!
m_setComplete.IsOpen())
m_setComplete.Open();
m_setComplete.Close();//closethedatabase
//不用类向导写连接数据库的程序段,但是我只看明白了连接和查询,不会修改和添加,删除
//1,
CDatabasem_dbCust;//定义数据库类对象
m_dbCust.OpenEx(_T("DSN=MQIS;UID=sa;PWD=1980623")//打开数据库 //数据源名,用户名,密码
CDatabase:
:
forceOdbcDialog);//此参数只定是否打开连接确认对话框
//MessageBox(m_dbCust.GetDatabaseName()); 取得数据源名
//m_dbCust.ExecuteSQL("selectnumberfromworks");测试是否支持SQL语句
CRecordsetcs(&m_dbCust);//定义目录查询对象
cs.Open(CRecordset:
:
dynaset,
_T("select*fromworks"));//打开时执行的SQL语句
shortnFields=cs.GetODBCFieldCount();//取得字段数,(列数)
CDBVariantvarValue;//定义通用数据类型
CODBCFieldInfoco;//定义字段信息
CStringcc;
while(!
cs.IsEOF())
{
for(shortindex=0;index{