Thread: Buttons

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    207

    Buttons

    Are there any relatively painless tutorials for buttons in the WinAPI?

    mw
    Blucast Corporation

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Add this to the beginning of the file:
    Code:
    #define IDB_BUTTON1 50001
    A code example, place it in your WM_CREATE:
    Code:
    HWND button;
    button=CreateWindowEx(0,"Button","Buttontext", WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 1, 1, 100, 23, hwnd, (HMENU)IDB_BUTTON1, GetModuleHandle(NULL), NULL);
    And to change the font you can use:
    Code:
    HFONT hf;
    long lf;
    lf = -MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
    hf = CreateFont(lf, 0, 0, 0, FW_MEDIUM, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
    SendMessage(button,WM_SETFONT,(WPARAM)hf,TRUE);
    You can use any other font instead of Times New Roman.
    And if you want it to do something, when you click on the button, add this code to your message switch:
    Code:
    case WM_COMMAND:
    switch(wParam){
    case IDB_BUTTON1:
    MessageBox(hwnd,"You clicked the button","Message",MB_OK | MB_INFORMATION);
    return 0;
    }
    break;
    Last edited by maxorator; 09-30-2005 at 01:29 PM.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    True, you can use every font installed on you fonts folder, and also you can install new fonts to use them; I'm not sure if you can use a non-installed ttf in runtime. So the painless and bored way is to change "Times New Roman" for "Arial", up the escapement param (the third in CreateFont) and underline it by setting the 7th param to TRUE; that's all. The other way is to change some other params on the CreateFont function (as said before you'll find a lot of theory and descriptions in winprog, or you can browse through the msdn site). And another one is to read a bit more about CreateFontIndirect and AddFontResource functions, LOGFONT struct, etc...
    Niara

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    MSDN is the best place to see what createfont can do...

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    207
    maxorator,

    I'm getting an error when I try to create my button:

    Code:
    case WM_CREATE:
        button = CreateWindow
            (
            TEXT ("Button"),
            TEXT ("New Game"),
            WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            1, 1,
            100, 23,
            hwnd,
            (HMENU) IDB_BUTTON1,
            GetModuleHandle (NULL),
            NULL
            );
    The error is:

    syntax error: ','

    When I first typed this in my program, I typed it exactly as you typed it (all on one line). After I got the syntax error, I made it look like the above to see if MSVC++ was smart enough to point out the exact position where my syntax error occurs. Of course, since this is Microsux I'm dealing with, it simply pointed to the last NULL instead of trying to help me.

    Do you know where I'm missing/adding a comma? I have no idea. I've compared my code to yours and other code that I've found and the syntax looks correct to me... :-(

    mw

    PS: I'm using "CreateWindow" instead of "CreateWindowEx".
    Last edited by Lionmane; 10-04-2005 at 01:37 AM.
    Blucast Corporation

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    That should work:
    Code:
    HWND button=CreateWindowEx(0,"BUTTON",WS_CHILD|WS_VISIBLE|WS_TABSTOP,1,1,100,100,hwnd,(HMENU)IDB_BUTTON1,GetModuleHandle(NULL),NULL);
    you shuld define ('#define') the button identificator ('IDB_BUTTON1') before use it.
    Niara

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    case WM_CREATE:
        button = CreateWindow
            (
           0,
            TEXT ("Button"),
            TEXT ("New Game"),
            WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            1, 1,
            100, 23,
            hwnd,
            (HMENU) IDB_BUTTON1,
            GetModuleHandle (NULL),
            NULL
            );
    You forgot to add the first argument...

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    207
    CreateWindow doesn't require the first argument. When I add that to my code I get the error msg "Too many arguments".

    Can my syntax error occur BEFORE the function? Is there any way to track this?

    I hate Microsux... :-(

    mw

    PS: I also tried copying your code exactly, but I still get the syntax...
    Blucast Corporation

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I don't think you need those TEXT() tags anyway...

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    There was one error in my example:
    Code:
    case WM_COMMAND:
    switch(wParam){
    case IDB_BUTTON1:
    MessageBox(hwnd,"You clicked the button","Message",MB_OK | MB_INFORMATION);
    return 0;
    }
    break;
    It must be MB_ICONINFORMATION not MB_INFORMATION

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Try to copy this:
    Code:
    #include <windows.h>
    #define IDB_BUTTON1 50001
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
                 HWND button;
                 button=CreateWindowEx(0,"Button","Buttontext", WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 1, 1, 100, 23, hwnd, (HMENU)IDB_BUTTON1, GetModuleHandle(NULL), NULL);
            case WM_COMMAND:
                 switch(wParam){
                 case IDB_BUTTON1:
                 MessageBox(hwnd,"You clicked the button","Message",MB_OK | MB_ICONINFORMATION);
                 return 0;
                 }
                 break;
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    AND, you used CreateWindow instead of CreateWindowEx...
    Last edited by maxorator; 10-04-2005 at 12:46 PM.

  13. #13
    Registered User
    Join Date
    May 2005
    Posts
    207
    maxorator,

    I got rid of the syntax error! I forgot to add "50001" after the "#define" statement. What does that have to do with it?

    mw
    Blucast Corporation

  14. #14
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by Lionmane
    maxorator,

    I got rid of the syntax error! I forgot to add "50001" after the "#define" statement. What does that have to do with it?

    mw

    Code:
    			 case IDB_BUTTON1:
    			 MessageBox(hwnd,"You clicked the button","Message",MB_OK | MB_ICONINFORMATION);
    			 return 0;
    Replace "case IDB_BUTTON1:" with "case 50001:" and see what happens.


    Quote Originally Posted by maxorator
    AND, you used CreateWindow instead of CreateWindowEx...
    So what? So long as he isn't using any extended styles, there is no need to use CreateWindowEx() instead of CreateWindow().
    Last edited by Queatrix; 10-04-2005 at 06:14 PM.

  15. #15
    Registered User
    Join Date
    May 2005
    Posts
    207
    Thanks guys! Everything's working now! :-)

    mw
    Blucast Corporation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-09-2009, 02:31 AM
  2. Ownerdraw buttons (2)
    By maes in forum Windows Programming
    Replies: 7
    Last Post: 09-11-2003, 05:50 AM
  3. Radio Buttons in Visual C++ 6
    By Ripper1 in forum Windows Programming
    Replies: 22
    Last Post: 05-16-2003, 07:54 AM
  4. (Ken Fitlike) buttons
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2002, 01:21 PM
  5. Grouping radio buttons
    By Bazz in forum Windows Programming
    Replies: 1
    Last Post: 08-28-2001, 07:15 AM