Hey, I have read windows API tutorial now but I am wondering how I can add things to main window itself, I know how to add things to dialogs and such but I just wonder how to add to main window, For example add button to main window?

Sorry this is totally noob question.

Thanks already!

Recommended Answers

All 9 Replies

I didn't really understand if you can give example please do it.

Thanks

When a Sdk type Api program starts, one of the first (not the first, but one of the first) messages your main window will receive is the WM_CREATE message. In the message handler for that message you can create additional child windows of the main form/dialog/window such as buttons, listboxes, edit controls, etc. This is accomplished by additional calls to CreateWindowEx() where the 2nd parameter is the window class you want to create, i.e., "button", and parent parameter is the HWND of your main window. In standard 'Petzold' style Sdk code the WM_CREATE message will be processed within the main windows WndProc. However, I always create message cracker functions in my code and relagate each message's processing to handler functions. I typically create all my child window controls in a function such as...

long fnWndProc_OnCreate(....)
{
hButton=CreateWindowEx(...._;
...

return 0;
}

here's a simple example of adding a button...

#include <windows.h>
#define IDC_BUTTON   WM_USER + 1

LRESULT CALLBACK WindowProcedure(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
 HINSTANCE hIns;
 HWND hButton;
 
 switch(msg)
 {
  case WM_CREATE:
    hIns=((LPCREATESTRUCT)lParam)->hInstance;
    hButton=CreateWindow("button","Click Me",WS_CHILD|WS_VISIBLE,70,60,150,30,hwnd,(HMENU)IDC_BUTTON,hIns,0);
    break;
  case WM_COMMAND:
    MessageBox(hwnd,"You Clicked The Button","Message",MB_OK);
    break;
  case WM_KEYDOWN:
    if(wParam==VK_DELETE)
       MessageBox(hwnd,"You Clicked The Delete Key!","Delete!",MB_OK);
    break;
  case WM_SYSKEYDOWN:
    if((lParam&0x20000000)&&wParam==VK_DELETE)
        MessageBox(hwnd,"Alt + Delete Key!","Delete!",MB_OK);
    break;
  case WM_DESTROY:
    PostQuitMessage (0);
    break;
  default:
    return DefWindowProc(hwnd,msg,wParam,lParam);
 }

 return 0;
}

int __stdcall WinMain(HINSTANCE hIns,HINSTANCE hPrevIns,LPSTR lpszArgument,int iShow)

{
 char szClassName[]="WindowsApp";
 WNDCLASSEX wincl; 
 MSG messages;
 HWND hWnd; 

 wincl.hInstance=hIns;
 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;
 RegisterClassEx(&wincl);
 hWnd=CreateWindow(szClassName,"Windows App",WS_OVERLAPPEDWINDOW,200,100,300,215,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
  TranslateMessage(&messages);
  DispatchMessage(&messages);
 }

Sorry I didn't see code thanks!

Thanks I got it now! Is there any way to make buttons look like well, modern button like circle like. I know it has something to do with style but where I put it :P

Thanks!

Anyways again how would I use those :S if anyone wants to show
Thanks

That doesn't say really, where do I add it to, WM_CREATE or WM_COMMAND? If I put it to WM_COMMAND it says it uses hButton without being initialized :S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.