⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 剪切板示例.c

📁 剪贴板原码
💻 C
📖 第 1 页 / 共 2 页
字号:
		   {  
			  if (nFileExist==0)
			   {
				 MessageBox(hwnd,"没有文件可保存","提示",MB_OK);
                 break;
			   }
			  else
			   { 
				 nLength=GetWindowTextLength(hwndEdit);
	             lpBuffer=(LPTSTR)malloc(nLength+1);
                 GetWindowText(hwndEdit,lpBuffer,nLength+1);
	     	     
			
			     SetFilePointer(hFile,0,NULL,FILE_BEGIN);
                 bWriteFile=WriteFile(hFile,lpBuffer,nLength+1,&dwFileWrite,NULL);
           
		         if (bWriteFile==FALSE)
			       {
				   MessageBox(hwnd,"文件写入失败!","提示",MB_OK);
				   free(lpBuffer);
                   break;
			       }
			     else
			      {  
			       MessageBox(hwnd,"文件保存成功!","文件保存",MB_OK);
			       free(lpBuffer);
			     }
		     }
		 }
       
		break;

	   case IDM_SAVEAS:

		   SendMessage(hStatusBar,SB_SETTEXT,(WPARAM)1,
			            (LPARAM)"另存文件");

		   if (nFileExist==1)
		     {
			   CloseHandle(hFile);
			   nFileExist=0;
		     }

           ofn.lpstrFileTitle=lpstrFileName;
           ofn.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST
                   |OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT;
		   ofn.lpstrTitle="另存为";

		   GetSaveFileName(&ofn);
           
		   hFile=CreateFile(lpstrFileName,GENERIC_WRITE,0,NULL,
			                    CREATE_ALWAYS,0,NULL);
		
		   if ((DWORD)hFile==-1)
		     {
			   MessageBox(hwnd,"文件保存失败!","提示",MB_OK);
			  
               break;
		     }
           else 
		    { 
			  nFileExist=1;

              nLength=GetWindowTextLength(hwndEdit);
			  lpBuffer=(LPTSTR)malloc(nLength+1);
		      GetWindowText(hwndEdit,lpBuffer,nLength+1);
		  
		      SetFilePointer(hFile,0,NULL,FILE_BEGIN);
              bWriteFile=WriteFile(hFile,lpBuffer,nLength+1,&dwFileWrite,NULL);
              
			  if (bWriteFile==FALSE)
			   {
				 MessageBox(hwnd,"文件写入失败!","提示",MB_OK);
				 CloseHandle(hFile);
				 nFileExist=0;
				 free(lpBuffer);
				 break;
			   }
			 else
			   {
				 MessageBox(hwnd,"文件保存成功!","文件保存",MB_OK);
				 free(lpBuffer);
				 nIsNew=0;
			   }
		    }

		   break;
	
	   case IDM_CLOSE:

		   SendMessage(hStatusBar,SB_SETTEXT,(WPARAM)1,
			            (LPARAM)"关闭文件");
		   if (nFileExist==1)
		     {
			   CloseHandle(hFile);
			   MessageBox(hwnd,"文件已关闭","关闭文件",MB_OK);
		     }
           SetWindowText(hwndEdit,"");
		   nFileExist=0;
		   
           break;
	  
	

	   case IDM_EXIT:
		 if (nFileExist==1)
		    CloseHandle(hFile);
         SendMessage(hwnd,WM_DESTROY,0,0);
       break;


       case IDM_CUT:
		   SendMessage(hStatusBar,SB_SETTEXT,(WPARAM)1,
			            (LPARAM)"剪切");
    
		   nLength=GetWindowTextLength(hwndEdit);
           lpBuffer=(LPTSTR)malloc(nLength+1);
		   GetWindowText(hwndEdit,lpBuffer,nLength+1);

          if (!(hText=GlobalAlloc(GHND,nLength+1))) 
           {
             MessageBox(hwnd,"全局内存分配失败!","提示",MB_OK);
			 free(lpBuffer);
             break;
           }
        
        lpText=(LPTSTR)GlobalLock(hText);
        lstrcpy(lpText,lpBuffer);
	    GlobalUnlock(hText); 

        if(!OpenClipboard(hwnd))
            {
              MessageBox(hwnd,"剪切板打开失败!","提示",MB_OK);
              free(lpBuffer);
			  break;
            }
         
             EmptyClipboard(); 
             SetClipboardData(CF_TEXT,hText); 
             CloseClipboard();  
			                     
          hText=NULL;
		  SetWindowText(hwndEdit,"");
		  free(lpBuffer);
			  
	   break;


	   case IDM_COPY:

		   SendMessage(hStatusBar,SB_SETTEXT,(WPARAM)1,
			            (LPARAM)"拷贝");

		   nLength=GetWindowTextLength(hwndEdit);
           lpBuffer=(LPTSTR)malloc(nLength+1);
		   GetWindowText(hwndEdit,lpBuffer,nLength+1);

          if (!(hText=GlobalAlloc(GHND,nLength+1))) 
           {
             MessageBox(hwnd,"全局内存分配失败!","提示",MB_OK);
			 free(lpBuffer);
             break;
           }
        
           lpText=(LPTSTR)GlobalLock(hText);
           lstrcpy(lpText,lpBuffer);
    	   GlobalUnlock(hText); 

           if(!OpenClipboard(hwnd))
             {
              MessageBox(hwnd,"剪切板打开失败!","提示",MB_OK);
              free(lpBuffer);
			  break;
             }
         
             EmptyClipboard(); 
             SetClipboardData(CF_TEXT,hText); 
             CloseClipboard();  
			                     
             hText=NULL;
		     free(lpBuffer);
	
        break;


	    case IDM_PASTE:

		  SendMessage(hStatusBar,SB_SETTEXT,(WPARAM)1,
			            (LPARAM)"粘贴");

		  if (nFileExist==1)
			 {
			  MessageBox(hwnd,"请先关闭当前文件","提示",MB_OK);
			  break;
			 }
		     else
		     {
                SetWindowText(hwndEdit,""); 
                if(!IsClipboardFormatAvailable(CF_TEXT))
                    {
                      MessageBox(hwnd,"剪切板上无文本数据","提示" ,MB_OK);
                       break;
                     }
       
                if(!OpenClipboard(hwnd))
                    {
                      MessageBox(hwnd,"剪切板打开失败","提示" ,MB_OK);
                      break;
                    }
   
                if (!(hTextp=GetClipboardData(CF_TEXT)))
                   {
                     MessageBox(hwnd,"无法读取剪切板数据","提示" ,MB_OK);
                     CloseClipboard();
                     break;
                   }
       
               lpBuffer=(LPTSTR)malloc(GlobalSize(hTextp));
               lpTextp=(LPTSTR)GlobalLock(hTextp);
               lstrcpy(lpBuffer,lpTextp);
              
               GlobalUnlock(hTextp);
               CloseClipboard(); 
                 
			   SetWindowText(hwndEdit,lpBuffer);
			   free(lpBuffer);
   
			 }
        break;

	   case IDM_DELETE:
		  SetWindowText(hwndEdit,"");
        break;
	   
	   case IDM_ABOUT:
          MessageBox(hwnd,"文件操作示例","关于",MB_OK);
		break;

	   }
	     break;

  case WM_NOTIFY:
		lpnmhdr=(LPNMHDR)lParam;
            if (lpnmhdr->code==TTN_NEEDTEXT)
                {
                  lpttext=(LPTOOLTIPTEXT)lParam;
                  switch(lpttext->hdr.idFrom)
                    {
                      case IDM_NEW:
                        lpttext->lpszText="新建";
                      break;
                     
                      case IDM_OPEN:
                        lpttext->lpszText="打开";
                      break;
                 
                      case IDM_SAVE:
						lpttext->lpszText="保存";
                      break;

					  case IDM_CLOSE:
                         lpttext->lpszText="关闭";
                      break;

					  case IDM_CUT:
						lpttext->lpszText="剪切";
					  break;

					  case IDM_COPY:
						lpttext->lpszText="复制";
					  break;

					  case IDM_PASTE:
						lpttext->lpszText="粘贴";
                      break;

				    }              
                  }

  		 break;


  case WM_DESTROY:          
          PostQuitMessage(0);   
          break;   
   
default:                  
     return  DefWindowProc(hwnd,message,wParam,lParam);			
   }
   return 0;
}

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -