clipboard&file operating.

13091 단어 직장sdk레저

  
  
  
  
  1. /*------------------------------------------------- 
  2.    Code By Pnig0s1992 
  3.   -------------------------------------------------*/ 
  4.  
  5. #include <windows.h> 
  6. #include <windowsx.h> 
  7. #define DIVISIONS 5 
  8.  
  9. LRESULT CALLBACK WndProc (HWNDUINTWPARAMLPARAM) ; 
  10.  
  11. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  12.                     PSTR  szCmdLine, int iCmdShow) 
  13.      static TCHAR szAppName[] = TEXT ("pwn0ne") ; 
  14.      HWND         hwnd ; 
  15.      MSG          msg ; 
  16.      WNDCLASS     wndclass ; 
  17.       
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ; 
  19.      wndclass.lpfnWndProc   = WndProc ; 
  20.      wndclass.cbClsExtra    = 0 ; 
  21.      wndclass.cbWndExtra    = 0 ; 
  22.      wndclass.hInstance     = hInstance ; 
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ; 
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ; 
  25.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; 
  26.      wndclass.lpszMenuName  = NULL ; 
  27.      wndclass.lpszClassName = szAppName ; 
  28.       
  29.      if (!RegisterClass (&wndclass)) 
  30.      { 
  31.           MessageBox (NULL, TEXT ("Program requires Windows NT!"),  
  32.                       szAppName, MB_ICONERROR) ; 
  33.           return 0 ; 
  34.      } 
  35.       
  36.      hwnd = CreateWindow (szAppName, TEXT ("pwn0ne"), 
  37.                           WS_OVERLAPPEDWINDOW, 
  38.                           CW_USEDEFAULT, CW_USEDEFAULT, 
  39.                           CW_USEDEFAULT, CW_USEDEFAULT, 
  40.                           NULL, NULL, hInstance, NULL) ; 
  41.       
  42.      ShowWindow (hwnd, iCmdShow) ; 
  43.      UpdateWindow (hwnd) ; 
  44.       
  45.      while (GetMessage (&msg, NULL, 0, 0)) 
  46.      { 
  47.           TranslateMessage (&msg) ; 
  48.           DispatchMessage (&msg) ; 
  49.      } 
  50.      return msg.wParam ; 
  51. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
  52.      static PTSTR pText; 
  53.      HBRUSH hBrush; 
  54.      HDC hdc; 
  55.      PAINTSTRUCT ps; 
  56.      RECT rc; 
  57.      HGLOBAL hGlobal; 
  58.      PTSTR pGlobal; 
  59.      HANDLE hHandle; 
  60.      LPCWSTR szFilePath = L"example.txt"
  61.      DWORD dzWriteDataSize; 
  62.      LPSTR szFileData = "Test for clipboard&file operating."
  63.      int i,iLength = strlen(szFileData); 
  64.      BOOL bAvailable; 
  65.      BOOL bOpen; 
  66.  
  67.      switch(message){ 
  68.      case WM_CREATE: 
  69.          hGlobal = GlobalAlloc(GPTR,iLength+1); 
  70.          pGlobal = GlobalLock(hGlobal); 
  71.          for(i=0;i<iLength;i++){ 
  72.              *pGlobal++=*szFileData++; 
  73.          } 
  74.          GlobalUnlock(hGlobal); 
  75.  
  76.          OpenClipboard(hwnd); 
  77.          EmptyClipboard(); 
  78.          SetClipboardData(CF_TEXT,hGlobal); 
  79.          CloseClipboard(); 
  80.          return 0; 
  81.      case WM_PAINT: 
  82.          hdc = BeginPaint(hwnd,&ps); 
  83.          GetClientRect(hwnd,&rc); 
  84.          hBrush=CreateSolidBrush(RGB(0,0,255)); 
  85.          FillRect(hdc,&rc,hBrush); 
  86.          if(bAvailable = IsClipboardFormatAvailable(CF_TEXT)){ 
  87.             MessageBox(hwnd,TEXT("Clipboard is empty."),TEXT("Tip"),MB_OK); 
  88.             SendMessage(hwnd,WM_DESTROY,0,0); 
  89.          } 
  90.          if(!(bOpen = OpenClipboard(hwnd))){ 
  91.             MessageBox(hwnd,TEXT("Open clipboard failed."),TEXT("Tip"),MB_OK); 
  92.             SendMessage(hwnd,WM_DESTROY,0,0); 
  93.          } 
  94.          hGlobal = GetClipboardData(CF_TEXT); 
  95.          pText = (char*)malloc(GlobalSize(hGlobal)); 
  96.          pGlobal = GlobalLock(hGlobal); 
  97.          lstrcpy(pText,pGlobal); 
  98.          GlobalUnlock(hGlobal); 
  99.          CloseClipboard(); 
  100.  
  101.          hHandle = CreateFile(szFilePath,GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,NULL); 
  102.   
  103.         if(hHandle == INVALID_HANDLE_VALUE){ 
  104.         MessageBox(hwnd,TEXT("Create file failed."),TEXT("Warnning"),MB_OK); 
  105.         SendMessage(hwnd,WM_DESTROY,0,0); 
  106.         }else
  107.             if(!WriteFile(hHandle,szFileData,iLength,&dzWriteDataSize,NULL)){ 
  108.                 MessageBox(hwnd,TEXT("Write file failed."),TEXT("Warnning"),MB_OK); 
  109.                 SendMessage(hwnd,WM_DESTROY,0,0); 
  110.             }else
  111.                 MessageBox(hwnd,TEXT("Write file successed."),TEXT("Tip"),MB_OK); 
  112.                 SendMessage(hwnd,WM_DESTROY,0,0); 
  113.         } 
  114.     } 
  115.          EndPaint(hwnd,&ps); 
  116.          DeleteObject(hBrush); 
  117.          return 0; 
  118.      case WM_DESTROY: 
  119.          PostQuitMessage(0); 
  120.          return 0; 
  121.      } 
  122.      return DefWindowProc(hwnd,message,wParam,lParam); 

좋은 웹페이지 즐겨찾기