CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Clipboard Help

  1. #1
    Join Date
    Apr 1999
    Posts
    4

    Clipboard Help

    Here is my program. For some reason none of the buttons work! Any suggestions would be appreciated.

    #include <windows.h>
    #include <string.h>
    #include <stdio.h>
    #include <winuser.h>

    #define ID_CUTBUTTON 101
    #define ID_PASTEBUTTON 102
    #define ID_CLEARBUTTON 103
    #define ID_TEXTEDIT 104

    void To_Clipboard(HWND, HWND);
    void From_Clipboard(HWND, HWND);

    typedef unsigned int UNIT;

    HINSTANCE hInst;
    int nCmd;

    long FAR PASCAL WndProc( HWND hwnd, UNIT msg, UNIT wParam, LONG lParam );


    int PASCAL WinMain( HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpszCmdLineArgs,
    int nCmdShow)
    {

    char szAppName[]="WinHello";
    HWND hwnd;
    MSG msg;
    WNDCLASS wc;

    hInst = hInstance;
    nCmd = nCmdShow;

    if( !hPrevInstance )
    {
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = (HBRUSH__*) GetStockObject( WHITE_BRUSH );
    wc.lpszMenuName = NULL;
    wc.lpszClassName = szAppName;
    RegisterClass( &wc );
    }

    hwnd = CreateWindow( szAppName,
    "Karen's Final Project",
    WS_OVERLAPPEDWINDOW,
    130,
    130,
    250,
    250,
    NULL,
    NULL,
    hInstance,
    NULL );
    ShowWindow( hwnd, nCmdShow );
    UpdateWindow( hwnd );

    while( GetMessage( &msg, NULL, 0 , 0 ) )
    {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
    }

    return( msg.wParam );
    }

    long FAR PASCAL WndProc( HWND hwnd, UNIT msg, UNIT wParam, LONG lParam )
    {
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    HWND CutButton, ClearButton, PasteButton, TextEdit;



    switch( msg )
    {
    case WM_CREATE: CutButton = CreateWindow ("button","Cut",WS_CHILD|BS_PUSHBUTTON,
    10,50,60,30,hwnd,(HMENU) ID_CUTBUTTON,
    hInst, NULL);
    ShowWindow(CutButton, SW_SHOW);
    UpdateWindow(CutButton);

    PasteButton = CreateWindow ("button","Paste",WS_CHILD|BS_PUSHBUTTON,
    10,100,60,30,hwnd,(HMENU) ID_PASTEBUTTON,
    hInst, NULL);
    ShowWindow(PasteButton, SW_SHOW);
    UpdateWindow(PasteButton);

    ClearButton = CreateWindow ("button","Clear",WS_CHILD|BS_PUSHBUTTON,
    10,150,60,30,hwnd,(HMENU) ID_CLEARBUTTON,
    hInst, NULL);
    ShowWindow(ClearButton, SW_SHOW);
    UpdateWindow(ClearButton);

    TextEdit = CreateWindow ("edit","",WS_CHILD|WS_BORDER,
    10,10,150,30,hwnd,(HMENU) ID_TEXTEDIT,hInst,NULL);
    ShowWindow(TextEdit, SW_SHOW);
    UpdateWindow(TextEdit);
    break;


    case WM_PAINT: hdc = BeginPaint( hwnd, &ps );
    GetClientRect( hwnd, &rect );
    EndPaint( hwnd, &ps );
    break;

    case WM_DESTROY: PostQuitMessage( 0 );
    break;

    case WM_COMMAND: switch( wParam )
    {
    case ID_CUTBUTTON: To_Clipboard(TextEdit, hwnd);
    break;
    case ID_PASTEBUTTON: From_Clipboard(TextEdit, hwnd);
    break;
    case ID_CLEARBUTTON: SetWindowText(TextEdit, "");
    break;
    }
    break;

    default: return( DefWindowProc( hwnd, msg, wParam, lParam ));
    }
    return (0);
    }

    void To_Clipboard(HWND TextEdit, HWND hwnd2){
    char source[20];
    GetWindowText(TextEdit, source, 20);
    if(OpenClipboard(hwnd2)){
    HGLOBAL clipbuffer;
    char * buffer;
    EmptyClipboard();
    clipbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(source)+1);
    buffer = (char*)GlobalLock(clipbuffer);
    strcpy(buffer, LPCSTR(source));
    GlobalUnlock(clipbuffer);
    SetClipboardData(CF_TEXT,clipbuffer);
    CloseClipboard();
    SetWindowText(TextEdit, "");
    }
    else{
    MessageBox(NULL, "Unable to write to clipboard!", "Error",
    MB_OK | MB_ICONINFORMATION);
    }
    }

    void From_Clipboard(HWND TextEdit, HWND hwnd2){
    char * buffer;
    if(OpenClipboard(hwnd2)){

    buffer = (char*)GetClipboardData(CF_TEXT);
    //do something with buffer here
    //before it goes out of scope
    SetWindowText(TextEdit, buffer);
    }
    CloseClipboard();
    }


  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Clipboard Help

    You're going to have to be more descriptive. What do you mean that "none of the buttons work"? Are the functions being called, but the clipboard functions don't do anything? Did you use a debugger to see where the failure occurs?
    Is this 16 or 32-bit? WM_COMMAND is different depending on the platform.

    Regards,

    Paul McKenzie


  3. #3
    Join Date
    Apr 1999
    Posts
    4

    Re: Clipboard Help

    Sorry, I wasn't more descriptive in the first place. My functions are being called, but are not giving me the expected results. When I press the cut button, it should copy the text string from the edit box to the clipboard and clear it. The paste button does the reverse. These buttons do nothing, even though I have included an error message if the clipboard doesn't open. The clear button merely clears the text from the edit box however, it does not!, even though it only calls the function SetWindowText(). I think that I am using a 32-bit platform. I will make an attempt at using the debugger. Thanks.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured