Clipboard Manager

Iniciado por Jhonjhon_123, Febrero 23, 2010, 07:27:33 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Febrero 23, 2010, 07:27:33 PM Ultima modificación: Enero 26, 2011, 10:30:19 AM por ANTRAX
Empecé hace poco en C y con la ayuda de Pablo y dreams_eater he terminado mi primera aplicación usando la API de Win, un manejador del portapapeles:

Es sencillo, hay un botón para imprimir lo que hay en el portapapeles y otro que saca los datos introducidos en un Textbox y los mete en el portapapeles.

Para cuando sepa un poco más me gustaría hacer lo mismo con imágenes y archivos :)

Código: c
#include <windows.h>
#include <stdio.h>

#define ID_EDIT 3
#define ID_BUTTON_1 1
#define ID_BUTTON_2 2


HINSTANCE hInst;
const char g_szClassName[] = "myWindowClass";

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    HWND hwndboton1, hwndboton2;
    static HWND hwndEdit;
    static TCHAR text[30];

    switch (iMsg)
    {
        case WM_CREATE:
        hwndboton1 = CreateWindow("button", "Sacar portapapeles",
          WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
          70, 10, 150, 30,
          hwnd, (HMENU) ID_BUTTON_1, hInst, NULL);
        hwndboton2 = CreateWindow("button", "Insertar texto en portapapeles",
          WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
          10, 50, 260, 30,
          hwnd, (HMENU) ID_BUTTON_2, hInst, NULL);
        hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
          30, 95, 230, 20, hwnd, (HMENU) ID_EDIT,
          NULL, NULL);
        return 0;

        case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case 1:
            {
                char * buffer = NULL;
                if (!OpenClipboard(NULL))
                {
                    SendMessage(hwnd, WM_CLOSE, 0, 0);
                }
                HANDLE hData = GetClipboardData(CF_TEXT);
                char * Portapapeles = (char*)GlobalLock(hData);
                MessageBox(hwnd, Portapapeles,"Portapapeles",MB_ICONINFORMATION|MB_OK);
                GlobalUnlock( hData );
                CloseClipboard();
                break;
            }

            case 2:
                {
                int len = GetWindowTextLength(hwndEdit) + 1;
                GetWindowText(hwndEdit,text,len);
                if (!OpenClipboard(NULL))
                {
                    MessageBox (NULL,"Can't open Clipboard","Error",MB_OK);
                    SendMessage(hwnd, WM_CLOSE, 0, 0);
                }
                EmptyClipboard();
                HANDLE hMem = GlobalAlloc(GMEM_MOVEABLE,strlen(text)+1);
                if (hMem == NULL)
                {
                    MessageBox (NULL,"Can't allocate memory","Error",MB_OK);
                    CloseClipboard();
                    SendMessage(hwnd, WM_CLOSE, 0, 0);
                }
                char * buffer = (char*)GlobalLock(hMem);
                strcpy(buffer, LPCSTR(text));
                GlobalUnlock(hMem);
                SetClipboardData(CF_TEXT,hMem);
                CloseClipboard();
                }

            break;

            default:
            break;
        }
        return 0;

        case WM_DESTROY:
        PostQuitMessage (0);
        return 0;
    }

    return DefWindowProc (hwnd, iMsg, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    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)(COLOR_BTNFACE + 1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Clipboard Manager",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 300, 170,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}


Autor: [N]athaniel
No tienes permitido ver los links. Registrarse o Entrar a mi cuenta