Karen
April 15th, 1999, 03:06 PM
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();
}
#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();
}