954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

WinAPI noob question

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!

Silvershaft
Junior Poster
139 posts since Jul 2009
Reputation Points: 12
Solved Threads: 0
 
athlon32
Junior Poster in Training
97 posts since Jan 2009
Reputation Points: 67
Solved Threads: 16
 

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

Thanks

Silvershaft
Junior Poster
139 posts since Jul 2009
Reputation Points: 12
Solved Threads: 0
 

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);
 }
Frederick2
Posting Whiz
330 posts since Jul 2008
Reputation Points: 244
Solved Threads: 31
 

Sorry I didn't see code thanks!

Silvershaft
Junior Poster
139 posts since Jul 2009
Reputation Points: 12
Solved Threads: 0
 

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!

Silvershaft
Junior Poster
139 posts since Jul 2009
Reputation Points: 12
Solved Threads: 0
 

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!

These might help: http://msdn.microsoft.com/en-us/library/bb849169%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/bb775951%28VS.85%29.aspx

athlon32
Junior Poster in Training
97 posts since Jan 2009
Reputation Points: 67
Solved Threads: 16
 

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

Silvershaft
Junior Poster
139 posts since Jul 2009
Reputation Points: 12
Solved Threads: 0
 
athlon32
Junior Poster in Training
97 posts since Jan 2009
Reputation Points: 67
Solved Threads: 16
 

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

Silvershaft
Junior Poster
139 posts since Jul 2009
Reputation Points: 12
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You