C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 16th, 2004, 05:48 PM
c0ldshadow c0ldshadow is offline
register char *c0ldMember;
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Posts: 510 c0ldshadow User rank is Private First Class (20 - 50 Reputation Level)c0ldshadow User rank is Private First Class (20 - 50 Reputation Level) 
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;
}

Reply With Quote
  #2  
Old May 16th, 2004, 06:56 PM
fisch's Avatar
fisch fisch is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2004
Location: Frostbite capital of the world
Posts: 542 fisch User rank is Lance Corporal (50 - 100 Reputation Level)fisch User rank is Lance Corporal (50 - 100 Reputation Level)fisch User rank is Lance Corporal (50 - 100 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > create button and do something when its clicked

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap