随笔 - 147  文章 - 222  trackbacks - 0
<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(8)

随笔分类

随笔档案

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜

60天内阅读排行

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; i<col; i++){

array[i] = new int[row];

if(array[i] = = 0)

    exit(-1); }

//释放

for(i=0; i<col; i++){

  delete [] array[i]; }

delete [] array;

 

3.        如何定义一个二维字符串数组

      char  **str = “abcdef” ; //一维

      char  *str[row][col]={“abc”, “cdf”, “efg”…..}; //row×col

 

4.        将一个十六进制”FF””FA”等字符串数组等转化为十进制数组

int array[32][32];

int a,b, n=0;

int result1;

char *str;

for(int i=0; i<32; i++)

for(int j=0; j<32; j++){

     str = str[i][j];

     if(str[0] >= ‘A’ && str[1] >=’A’){  //”AF”

        a = str[0] – ‘A’;

        b= str[1] – ‘A’;

        result1 = (a+10)*16+(b+10)*1;}

     else if(str[0] >= ‘A’ && str[1] <’A’){ //”A9”

        a = str[0] – ‘A’;

        b = ‘A’  – str[1] +1;

        result1 = (a+10)*16+b;}

     else if(str[0] < ‘A’ && str[1] >=’A’){ //”8B”

        a = ‘A’ – str[0]+1;

        b = str[1] – ‘A’;

        result1 = a*16+(b+10)*1;}

     else{                       //”87”

        a = ‘A’ – str[0]+1;

        b = ‘A’  – str[1] +1;

        result1 = a*16+b;}

     array[i][j] = reslut1;

     printf(“%5d”, array[i][j]);

     n++;

     if(n%32 = = 0)  printf(“\n”); }

将这个矩阵数组在屏幕上显示:

#i nclude < afxwin.h >

#i nclude < afxext.h >

#i nclude < afxdisp.h >

#i nclude < afxdtctl.h >

DWORD color_;

HDC hMyDC = GetDC(NULL);

for(i=0; i<32; i++)

for(j=0; j<32; j++){

     color_ = array[i][j];

     color_ = color_*256*256 + color_*256+color_;

     SetPixel(hMyDC, j, i, color_); }

 

5.        将一个整型转化为一个字符串 _itoa

//将从位图信息头得到的图像宽度和高度显示出来

char  buffer1[20], buffer2[20];

_itoa( width, buffer1, 10);

_itoa( height, buffer2, 10);

char str[80];

strcpy(str, “width=”);

strcat(str, buffer1);

strcat(str, “, hight=”;

strcat(str, buffer2);

AfxMesageBox(str, MB_OK, 0);

 

6.        当把二维数组地址用作参数传递,而又要此地址不断递增时,可以另外定义一个同类型指针,指向二维数组第一个元素的地址,把这个赋值放在循环外:

     unsigned char *p;

     p = &m_pImage[0][0];

   然后在循环中,可以p + m_count*4096;

 

7.        改变最近打开文档的个数

InitInstance()中的LoadStdProfileSetting(8); //括号里是要设置的个数

8.        打开调色板对话框

CColorDialog  dlg;

dlg.Domodal();

 

9.        如何添加工具栏,如何使用Slider?

OnInitDialog()中添加:

CSliderCtrl *pSliderCtrl=(CSliderCtrl*)GetDlgItem(IDC_SLIDER1);

pSliderCtrl -> SetRange(0,255,TRUE); //设置滑动条的范围

pSliderCtrl -> SetPos(100); // 设置滑动条的初始位置

Dlg类中响应WM_HSCROLL消息:

CSliderCtrl *pSliderCtrl=(CSliderCtrl*)GetDlgItem(IDC_SLIDER1);

m_nCur = pSliderCtrl - >GetPos(); //获得当前的位置值

 

10.     更改光标

:: SetCursor(:: LoadCursor(NULL, IDC_SIZEALL));

//如果是自己定义的光标资源,则要用MAKEINTRESOURCE 进行转化,例如:

:: SetCursor(:: LoadCursor(NULL, IDC_CURSOR_CUT));

 

11.     设置文本显示的一些函数

CDC  dc(this); 

CPen  pen(PS_SOLID, 2, RGB(0,0,255));  //初始化一支笔

CBrush  *pBush = CBrush :: FromHandle((HBRUSH)GetStockObject(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 *dcTemp;

dcTemp= new CClientDC(FromHandle(m_hWnd));

CDC  memDC;

memDC.CreateCompatibleDC(dcTemp); //创建一个兼容的DC

CBitmap  bm;

CSize  sz(lWidth, lHeight);

bm.CreateCompatilbleBitmap(dcTemp, sz.cx, sz.cy); //创建一个兼容的位图

CBitmap* oldbm = memDC.SelectObject(&bm);

memDC.BitBlt(0, 0, sz.cx, sz.cy, dcTemp, winPlacement ->rcNormalPosition.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里获取一个文件夹路径

BROWSEINFO  bi;

TCHAR  szDisplayName[MAX_PATH];

LPITEMIDLIST  pidl;

LPMALLOC  pMalloc = 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 = szDisplayName;

      MessageBox(sPath); }

 

14.     如何设置密码输入时显示的是*

#i nclude <iostream.h>

#i nclude <conio.h>

char a[8];

void main() {

int i=0;

Cout<<”请输入密码:”<<endl;

while(1) {

     a[i] = getch();

     if( i>=8 || a[i]= = 13)

        break;

     putch(‘*’);

     i++; }

cout<<endl<<a<<endl;

getch(); }

 

15.     设置菜单:可以使用CMenu:: EnableMenuItem()来设置菜单可用或禁用,但是在MFC中,要使该函数起作用,需要将CWnd :: m_bAutomenuEnable设置为FALSE.

16.     关闭子窗口

:: SendMessage(:: AfxGetMainWnd() -> m_hWnd, WM_COMMAND, ID_FILE_CLOSE,0);

获取主窗口的指针  CWinThread :: m_pMainWnd

调用AfxGetMainWnd()可实现

   继续上面的总结~! To making  it  count~!。。。。。。
 
1.        获取CMain类的指针

CMain* pApp = ((CMain*)AfxGetApp()->m_pMainWnd);

ASSERT_KINDOF(CMain, pAPP); //确保pAPPCMain的类对象

2.        VC++如何获取应用程序的实例句柄

实例句柄保存在CWinApp  m_hInstance

HANDLE  hInstance = AfxGetInstanceHandle();

3.        VC++怎样加载其他的应用程序 三个SDK函数WinExec, ShellExecute, CreateProcess.

      WinExec最简单,前一个指定路径,后一个指定显示方式;

      ShellExecute(null, null, _T(“1.txt”), NULL, _T(“c:\\temp”), SW_SHOWNORMAL);

      STARTUPINFO  stinfo; //启动窗口的信息

PROCESSINFO  proinfo; //进程的信息

CreateProcess(NULL,_T(“notepad.exe)”,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&stinfo,&proinfo);

 

4.        如何确定顶层菜单所占据的菜单行数:首先,计算主窗口的高度和客户区高度;其次,从主框窗口的高度中减去客户区、框边界以及标题的高度;最后,除以菜单栏的高度

CRect rc, rcClient;

GetWindowRect(rc);

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.        如何获得应用程序主窗口的指针

CWnd  pMainWnd = 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 = (CMDIChildWnd*)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_SETBKCOLOR, 0, RGB(255,0,0)); //背景色

m_progress.SendMessage(PBM_SETBARCOLOR, 0, RGB(0,255,0)); //前景色

12.     定义char num[10];

        sprintf(num, “%d”, calnum); //格式化数字

        char  unit[]=”矩形”;

        sprintf(num, “%s”, unit);  //格式化字符串

13.     改变字符串的显示字体和大小

CFont font;

font.CreatePointFont(300, “华文行楷”,NULL);

CFont *pOldFont=dc.SelectObject(&font);

dc.SelectObject(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<lenpath-1; ) {

     if(reportpath.GetAt(i) = = ‘\\’) {

         reportpath.Insert(i, ‘\\’);

         i+=2; }

     i++; }

 

15.     获取屏幕分辨率

HDC  hSrcDC;

hSrcDC = ::GetDC(AfxGetApp() -> m_pMainWnd->GetSafeHwnd());

int xSrc, ySrc;

xSrc = GetDeviceCaps(hSrcDC, HORIRES); //水平分辨率

ySrc = GetDeviceCaps(hSrcDC, VERTRES); //垂直分辨率

 

16.     Edit控件响应回车键:利用获取消息来完成,调用虚函数

PreTranslateMessage(MSG* pMsg) {

   If( pMsg ->message = = WM_KEYDOWN) {

      switch(pMsg -> wParam) {

         case  VK_RETURN:

            GetDlgItem(IDC_BTN_INPUT)->SendMessage(BM_CLICK, 0, 0);

            return TRUE; }  }    }

17.     改变对话框的背景色:在InitInstance()中加入

SetDialogBKColor(RGB(160, 180, 220), RGB(255, 0, 0)); //后面是字体颜色

18.     让指定的矩形框重画 InvalidateRect(&rect, TRUE);

19.     怎样选择所选的List当前位置

int  iTem;

POSITION  pos=m_findCtrl.GetFirstSelectedItemPosition();

if(pos = = 0) {

  MessageBox(“请选择需要删除的记录”);

  return;  }

else {

iTem = m_findCtrl.GetNextSelectedItem(pos); }

  //删除List中的某行

  m_findCtrl.DeleteItem(iTem);

m_findCtrl.Update(iTem);

 

20.     动态创建控件

CEdit  m_edit;

CRect  rect(0,0,100,200);

M_edit.Create(WS_CHILD | ES_AUTOHCROLL | WS_BORDER, rect, this, ID_EDIT_1);

21.     列表控件的应用

CListCtrl*  m_list;

CRect  rect_list;

this->GetCientRect(&rect_list);

rect_list.top + =100;

m_list -> Create(WS_CHILD | LVS_REPORT | WS_BORDER | LVS_SINGLESEL, rect_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(RGB(0,200,200)); //设置背景颜色

m_list->SetTextBkColor(RGB(0,200,200)); //设置文本背景颜色

m_list->ModifyStyle(LVS_OWNERDRAWFIXED,0L);

m_list->SetExtendedStyle(LVS_EX_FULLROWSELET  //整行选中

                     |  LVS_EX_HEADERDARGDROP  //允许整列拖动

                     |  LVS_EX_GRIDLINES   //画出网格线

                     |  LVS_EX_FLATSB);  // 扁平风格的滚动条

22.     取得当前获得焦点的窗口句柄

HWND  hwnd = ::GetFocus();

int  iID = :: GetDlgCtrlID(hwnd); // 根据句柄取得其资源符号

 

23.     修改控件的字体

CFont m_font;

m_font.CreateFont(-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() {

      MSG  msg;

     while ( PeekMessage(&msg, NULL, NULL, NULL, NULL,NULL, PM_REMOVE)) {

                   TranslateMessage(&msg);

                   DispatchMessage(&msg); }  }

Void CMyTestDialog :: WaitForThreadToTerminate( HANDLE hThread)  {

          //将指示哪个线程需要等待并作进一步处理

      DWORD  dwRet;

      Do {

           dwRet = :: MsgWaitForMultipleObject(1, &hThread, FALSE, INFINITE, QS_ALLINPUT);

           if(dwRef != WAIT_OBJET_0)  {

                 PeekMessageLoop();   }

         }while ( (dwRet != WAIT_OBJET_0) && (dwRet != WAIT_FAILED));

//示例代码:假设对话框上有一个按钮,当点击按钮时,开始启动第二个线程,等到第

//二个线程完成后,我们再继续主线程:

void CMyTestDialog :: OnButton1()  {

      m_pUpdateThread = AfxBeginThread(UpdateDeviceContent, (LPVOID)this);

      ifm_pUpdateThread  {