Are there any relatively painless tutorials for buttons in the WinAPI?
mw
Are there any relatively painless tutorials for buttons in the WinAPI?
mw
Blucast Corporation
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
Add this to the beginning of the file:
A code example, place it in your WM_CREATE:Code:#define IDB_BUTTON1 50001
And to change the font you can use: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);
You can use any other font instead of Times New Roman.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);
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.
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
MSDN is the best place to see what createfont can do...
maxorator,
I'm getting an error when I try to create my button:
The error is: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 );
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
That should work:
you shuld define ('#define') the button identificator ('IDB_BUTTON1') before use it.Code:HWND button=CreateWindowEx(0,"BUTTON",WS_CHILD|WS_VISIBLE|WS_TABSTOP,1,1,100,100,hwnd,(HMENU)IDB_BUTTON1,GetModuleHandle(NULL),NULL);
Niara
You forgot to add the first argument...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 );
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
I don't think you need those TEXT() tags anyway...
There was one error in my example:
It must be MB_ICONINFORMATION not MB_INFORMATIONCode:case WM_COMMAND: switch(wParam){ case IDB_BUTTON1: MessageBox(hwnd,"You clicked the button","Message",MB_OK | MB_INFORMATION); return 0; } break;
Try to copy this:
AND, you used CreateWindow instead of CreateWindowEx...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; }
Last edited by maxorator; 10-04-2005 at 12:46 PM.
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
Originally Posted by Lionmane
Replace "case IDB_BUTTON1:" with "case 50001:" and see what happens.Code:case IDB_BUTTON1: MessageBox(hwnd,"You clicked the button","Message",MB_OK | MB_ICONINFORMATION); return 0;
So what? So long as he isn't using any extended styles, there is no need to use CreateWindowEx() instead of CreateWindow().Originally Posted by maxorator
Last edited by Queatrix; 10-04-2005 at 06:14 PM.
Thanks guys! Everything's working now! :-)
mw
Blucast Corporation