- /*-------------------------------------------------
- Code By Pnig0s1992
- -------------------------------------------------*/
- #include <windows.h>
- #include <windowsx.h>
- #define DIVISIONS 5
- LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
- int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
- PSTR szCmdLine, int iCmdShow)
- {
- static TCHAR szAppName[] = TEXT ("pwn0ne") ;
- HWND hwnd ;
- MSG msg ;
- WNDCLASS wndclass ;
- wndclass.style = CS_HREDRAW | CS_VREDRAW ;
- wndclass.lpfnWndProc = WndProc ;
- wndclass.cbClsExtra = 0 ;
- wndclass.cbWndExtra = 0 ;
- wndclass.hInstance = hInstance ;
- wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
- wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
- wndclass.lpszMenuName = NULL ;
- wndclass.lpszClassName = szAppName ;
- if (!RegisterClass (&wndclass))
- {
- MessageBox (NULL, TEXT ("Program requires Windows NT!"),
- szAppName, MB_ICONERROR) ;
- return 0 ;
- }
- hwnd = CreateWindow (szAppName, TEXT ("pwn0ne"),
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT,
- CW_USEDEFAULT, CW_USEDEFAULT,
- NULL, NULL, hInstance, NULL) ;
- ShowWindow (hwnd, iCmdShow) ;
- UpdateWindow (hwnd) ;
- while (GetMessage (&msg, NULL, 0, 0))
- {
- TranslateMessage (&msg) ;
- DispatchMessage (&msg) ;
- }
- return msg.wParam ;
- }
- LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- static PTSTR pText;
- HBRUSH hBrush;
- HDC hdc;
- PAINTSTRUCT ps;
- RECT rc;
- HGLOBAL hGlobal;
- PTSTR pGlobal;
- HANDLE hHandle;
- LPCWSTR szFilePath = L"example.txt";
- DWORD dzWriteDataSize;
- LPSTR szFileData = "Test for clipboard&file operating.";
- int i,iLength = strlen(szFileData);
- BOOL bAvailable;
- BOOL bOpen;
- switch(message){
- case WM_CREATE:
- hGlobal = GlobalAlloc(GPTR,iLength+1);
- pGlobal = GlobalLock(hGlobal);
- for(i=0;i<iLength;i++){
- *pGlobal++=*szFileData++;
- }
- GlobalUnlock(hGlobal);
- OpenClipboard(hwnd);
- EmptyClipboard();
- SetClipboardData(CF_TEXT,hGlobal);
- CloseClipboard();
- return 0;
- case WM_PAINT:
- hdc = BeginPaint(hwnd,&ps);
- GetClientRect(hwnd,&rc);
- hBrush=CreateSolidBrush(RGB(0,0,255));
- FillRect(hdc,&rc,hBrush);
- if(bAvailable = IsClipboardFormatAvailable(CF_TEXT)){
- MessageBox(hwnd,TEXT("Clipboard is empty."),TEXT("Tip"),MB_OK);
- SendMessage(hwnd,WM_DESTROY,0,0);
- }
- if(!(bOpen = OpenClipboard(hwnd))){
- MessageBox(hwnd,TEXT("Open clipboard failed."),TEXT("Tip"),MB_OK);
- SendMessage(hwnd,WM_DESTROY,0,0);
- }
- hGlobal = GetClipboardData(CF_TEXT);
- pText = (char*)malloc(GlobalSize(hGlobal));
- pGlobal = GlobalLock(hGlobal);
- lstrcpy(pText,pGlobal);
- GlobalUnlock(hGlobal);
- CloseClipboard();
- hHandle = CreateFile(szFilePath,GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,NULL);
- if(hHandle == INVALID_HANDLE_VALUE){
- MessageBox(hwnd,TEXT("Create file failed."),TEXT("Warnning"),MB_OK);
- SendMessage(hwnd,WM_DESTROY,0,0);
- }else{
- if(!WriteFile(hHandle,szFileData,iLength,&dzWriteDataSize,NULL)){
- MessageBox(hwnd,TEXT("Write file failed."),TEXT("Warnning"),MB_OK);
- SendMessage(hwnd,WM_DESTROY,0,0);
- }else{
- MessageBox(hwnd,TEXT("Write file successed."),TEXT("Tip"),MB_OK);
- SendMessage(hwnd,WM_DESTROY,0,0);
- }
- }
- EndPaint(hwnd,&ps);
- DeleteObject(hBrush);
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd,message,wParam,lParam);
- }
本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/604402,如需转载请自行联系原作者