vc常用代碼1
?
VC常用代碼
分類:?VC 學(xué)習(xí)2009-03-07 15:28?1114人閱讀?評論(0)?收藏?舉報
===========================================================
//注意事項
//先執(zhí)行OnInitDialog()函數(shù),然后執(zhí)行構(gòu)造函數(shù)!?
//98下沒有新宋體?
//XP下窗口高度加8?
===========================================================
………1,滾動條處理方法
//1,設(shè)置范圍?
m_spscroll.SetScrollRange(0,200);?
SCROLLINFO si;?
si.cbSize=sizeof(SCROLLINFO);?
si.nPage=100;?
si.fMask=SIF_PAGE;//設(shè)置頁寬?
m_spscroll.SetScrollInfo(&si);?
//2,處理消息,垂直滾動條加WM_VSCROLL消息,水平加WM_HSCROLL消息?
void CPrintView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)?
{?
// TODO: Add your message handler code here and/or call default?
if(pScrollBar->GetDlgCtrlID()==IDC_SCROLLBAR1)?
{?
int nCurrentPos=pScrollBar->GetScrollPos();?
SCROLLINFO si;?
si.fMask=SIF_PAGE;//取得頁寬?
pScrollBar->GetScrollInfo(&si);?
switch(nSBCode)?
{?
case SB_THUMBTRACK://移動滑塊?
case SB_THUMBPOSITION:?
pScrollBar->SetScrollPos(nPos); //設(shè)置頁寬后滾動條的pos會以max/nPage倍數(shù)減少,所以在使用時注意把pos值*(max/nPage)能得到原值?
break;?
case SB_LINEUP://點向上小三角?
pScrollBar->SetScrollPos(nCurrentPos-1);?
break;?
case SB_LINEDOWN://點向下小三角?
pScrollBar->SetScrollPos(nCurrentPos+1);?
break;?
case SB_PAGEUP://向上一頁?
pScrollBar->SetScrollPos(nCurrentPos-si.nPage);?
break;?
case SB_PAGEDOWN://向下一頁?
pScrollBar->SetScrollPos(nCurrentPos+si.nPage);?
break;?
}?
}?
CDialog::OnVScroll(nSBCode, nPos, pScrollBar); {}?
………2,鎖定鼠標(biāo)
bool pOld;?
CRect rt;?
SetForegroundWindow();?
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,true,&pOld,SPIF_UpdateINIFILE);?
GetWindowRect(rt);?
ClipCursor(rt);?
//加到 LRESULT CLockDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 會有意外的效果
………3,在列表字符前插入一個負(fù)數(shù)字符以修改亂碼
int index=m_list.GetSelectionMark();//在列表字符前插入一個負(fù)數(shù)字符以修改亂碼?
CString cs;?
cs=m_list.GetItemText(index,0);?
char insert_char=-87;?
cs.Insert(0,insert_char);?
m_list.SetItemText(index,0,cs);?
………4,在列表中添加項目最大只能顯示259個字符(不含'/0')?
int char_length=cs.GetLength();//cs,ct為CString類對象,是要發(fā)到列表框的文本但是可能大于259字節(jié)?
while(char_length>259)//如果大于259字節(jié)?
{?
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')?
………5,設(shè)置NT窗口的透明度
OSVERSIONINFO osv;?
osv.dwOSVersionInfoSize=sizeof OSVERSIONINFO;?
GetVersionEx(&osv);//取得版本信息?
if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT)//VER_PLATFORM_WIN32_WINDOWS 98 Me用這個宏?
{?
//加入WS_EX_LAYERED擴展屬性?
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,?
GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);//如果多次調(diào)用下面這個函數(shù)設(shè)置,這個函數(shù)只在一個位置調(diào)用一次就行了?
HINSTANCE hInst = LoadLibrary("User32.DLL");?
if(hInst)?
{?
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);?
MYFUNC fun = NULL;?
//取得SetLayeredWindowAttributes函數(shù)指針?
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");?
if(fun)fun(this->GetSafeHwnd(),0, 200,//0 ~ 2552);?
FreeLibrary(hInst);?
}?
}?
………6,字體對話框的初始化
LOGFONT lf;?
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號字?
CFontDialog cf(&lf);//字體?
cf.m_cf.rgbColors=textcolor;//顏色?
………7,移動沒有標(biāo)題的窗口
//1定義:?
CPoint just_point;?
//2?
void CClockfortecherDlg::OnLButtonDown(UINT nFlags, CPoint point)?
{?
// TODO: Add your message handler code here and/or call default?
just_point=point;?
CDialog::OnLButtonDown(nFlags, point);?
}?
//3?
void CClockfortecherDlg::OnMouseMove(UINT nFlags, CPoint point)?
{?
// TODO: Add your message handler code here and/or call default?
WINDOWPLACEMENT wi;?
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);?
}?
………8,線程與信號量
//1,定義信號量句柄?
HANDLE event;?
//2,創(chuàng)建信號量?
event=CreateEvent(NULL, TRUE, FALSE, NULL);?
//3,創(chuàng)建線程:?
//1)定義線程函數(shù),格式必須如下,其中l(wèi)Param為AfxBeginThread的第二個參數(shù)值,可強制轉(zhuǎn)化成所需類型?
UINT WorkThreadProc(LPVOID lParam)//必須是 UINT XXX..XXX(LPVOID lParam)?
{?
//代碼示例WaitForSingleObject:?
while(1)?
{
//注意,如果線程間要求同步或互斥的時候,要在每一層循環(huán)體中加入WaitForSingleObject?
WaitForSingleObject((HANDLE)lParam, INFINITE);?
//WaitForSingleObject的使用方法:第一個為信號量HANDLE,是CreateEvent的返回值,第二個參數(shù)為等待的毫秒數(shù)(1/1000秒)?
//第二個參數(shù)為INFINITE時則一直等待,直到調(diào)用SetEvent()設(shè)置信號量時函數(shù)返回;為數(shù)值(如1000)則函數(shù)在1秒后返回?
//(即使你沒調(diào)用SetEvent()設(shè)置信號量)?
AfxMessageBox("fuck");//不能用MessageBox()因為這不是在類中了...?
ResetEvent((HANDLE)lParam);?
//重置信號量,以使WaitForSingleObject函數(shù)可以繼續(xù)等待,否則(如果你已經(jīng)調(diào)用過了SetEvent()設(shè)置了信號量)?
//WaitForSingleObject函數(shù)將會立刻返回?
}?
}?
//2)用AfxBeginThread創(chuàng)建一個WorkThreadProc的線程?
AfxBeginThread(WorkThreadProc,event);?
//4,在主程序需要的地方調(diào)用SetEvent()設(shè)置信號量啟動線程?
SetEvent(event);//或者用WaitForMultipleObjects函數(shù)?
static UINT __stdcall WorkThreadProc(void* pThis);
//如果lParam參數(shù)為一個對話框的指針,想調(diào)用這個對話框的變量或函數(shù)那么就得這樣定義線程函數(shù),還要將WorkThreadProc改成//Wait_forDlg::WorkThreadProc,這樣WorkThreadProc就成為CWait_forDlg類的函數(shù),在這個線程里就可以調(diào)用該類的變量了,注意得用_beginthreadex函數(shù)創(chuàng)建線程
UINT CWait_forDlg::WorkThreadProc(void * lParam)?
{?
CWait_forDlg *pThis=(CWait_forDlg *)lParam;?
HANDLE hObjects[2];?
hObjects[0] = pThis->event1;?
hObjects[1]= pThis->event2;?
while(1)?
{?
DWORD dwWait = WaitForMultipleObjects(2,hObjects,TRUE,INFINITE);?
//第一個參數(shù)為信號量個數(shù)2,第二個為指針,第三個如果為TRUE函數(shù)要等待兩個信號量都被SetEvent才返回,返回值為最后一個
//SetEvent的WAIT_OBJECT_0+i;而為FALSE則只要有一個被SetEvent就返回,返回值為 WAIT_OBJECT_0+i 即信號量在數(shù)組中的位置?
//+WAIT_OBJECT_0 */?
if (dwWait == WAIT_OBJECT_0) AfxMessageBox("fcuk 1 ");?
//開始ping?
if (dwWait == WAIT_OBJECT_0 + 1)?
AfxMessageBox("fcuk 2");?
ResetEvent(hObjects[1]);?
ResetEvent(hObjects[0]);?
}?
}?
#include