The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
create button and do something when its clicked
Discuss create button and do something when its clicked in the C Programming forum on Dev Shed. create button and do something when its clicked C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 16th, 2004, 05:48 PM
|
|
register char *c0ldMember;
|
|
Join Date: Nov 2003
Posts: 510

Time spent in forums: 3 Days 15 m 46 sec
Reputation Power: 10
|
|
|
create button and do something when its clicked
i am trying to learn about win32 programs in C++. i want to simply make a button, and be able to do things when that button is clicked. i am having trouble finding a site that shows examples of how to make programs like this.
below i have the code for my simple win32 program.
could someone help me edit this code to have a button and show me how to make things happen when the button is clicked?
peace, --ave
Code:
#include <windows.h>
LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
char szClassName[]="Filepool";
int WINAPI WinMain(HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance=hThisInstance;
wincl.lpszClassName=szClassName;
wincl.lpfnWndProc=WindowProcedure;
wincl.style=CS_DBLCLKS;
wincl.cbSize=sizeof(WNDCLASSEX);
wincl.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wincl.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
wincl.hCursor=LoadCursor(NULL,IDC_ARROW);
wincl.lpszMenuName=NULL;
wincl.cbClsExtra=0;
wincl.cbWndExtra=0;
wincl.hbrBackground=(HBRUSH)COLOR_BACKGROUND;
if(!RegisterClassEx(&wincl))
{
return 0;
}
hwnd=CreateWindowEx(0,szClassName,"Filepool",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,544,375,HWND_DESKTOP,NULL,hThisInstance,NULL);
ShowWindow(hwnd,nFunsterStil);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
|

May 16th, 2004, 06:56 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Location: Frostbite capital of the world
Posts: 542
 
Time spent in forums: 16 h 58 m 34 sec
Reputation Power: 10
|
|
check out the win32 tutorial at this site:
http://www.winprog.org/tutorial/
you can try this code. It should close the window and end the program when the button is clicked. I just pasted it together fast from some other stuff, so there might be some garbage in there that really isn't relevant.
Code:
#include <windows.h>
LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
char szClassName[]="Filepool";
int WINAPI WinMain(HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance=hThisInstance;
wincl.lpszClassName=szClassName;
wincl.lpfnWndProc=WindowProcedure;
wincl.style=CS_DBLCLKS;
wincl.cbSize=sizeof(WNDCLASSEX);
wincl.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wincl.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
wincl.hCursor=LoadCursor(NULL,IDC_ARROW);
wincl.lpszMenuName=NULL;
wincl.cbClsExtra=0;
wincl.cbWndExtra=0;
wincl.hbrBackground=(HBRUSH)COLOR_BACKGROUND;
if(!RegisterClassEx(&wincl))
{
return 0;
}
hwnd=CreateWindowEx(0,szClassName,"Filepool",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,544,375,HWND_DESKTOP,NULL,hThisInstance,NULL);
ShowWindow(hwnd,nFunsterStil);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
static HWND hwndButton = 0;
static int cx, cy;/* Height and width of our button. */
switch(message)
{
case WM_CREATE:
{
/* The window is being created. Create our button
* window now. */
TEXTMETRIC tm;
/* First we use the system fixed font size to choose
* a nice button size. */
hdc = GetDC (hwnd);
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
GetTextMetrics (hdc, &tm);
cx = tm.tmAveCharWidth * 30;
cy = (tm.tmHeight + tm.tmExternalLeading) * 2;
ReleaseDC (hwnd, hdc);
/* Now create the button */
hwndButton = CreateWindow (
"button",/* Builtin button class */
"Click Here",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, cx, cy,
hwnd,/* Parent is this window. */
(HMENU) 1,/* Control ID: 1 */
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
return 0;
break;
}
case WM_COMMAND:
/* Check the control ID, notification code and
* control handle to see if this is a button click
* message from our child button. */
if (LOWORD(wParam) == 1 &&
HIWORD(wParam) == BN_CLICKED &&
(HWND) lParam == hwndButton)
{
/* Our button was clicked. Close the window. */
DestroyWindow (hwnd);
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
Last edited by fisch : May 16th, 2004 at 07:08 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|