Buttons 
Author Message
 Buttons

Hi,

Could someone show me how to use buttons in a app.
I've got the following code, but I dont know how to see when the button is
pressed.

(btw, I'm not using MFC)

Please help!!!

--Lasse Hassing

----Main.c-----

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <string.h>
#include "d:\projects\kill\code\kasres.h"
/*<---------------------------------------------------------------------->*/
HINSTANCE hInst;  // Instance handle

HINSTANCE button1;

HWND hwndMain;

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM
lParam);

HDC hdc;

HBITMAP GraphSheep;
HDC HDCSheep;

HBITMAP GraphBack;
HDC HDCBack;

int SheepState;
/*
 <0 = Error

 0  = Normal
 1  = Shotgun
 2  = DNA
 3  = Flame

 >3 = Error
*/

HGDIOBJ hbmOld;

/*<---------------------------------------------------------------------->*/

static BOOL InitApplication(void)
{
 WNDCLASS wc;

 memset(&wc,0,sizeof(WNDCLASS));
 wc.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ;
 wc.lpfnWndProc = (WNDPROC)MainWndProc;
 wc.hInstance = hInst;
 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
 wc.lpszClassName = "kasWndClass";
 wc.hCursor = LoadCursor(NULL,IDC_ARROW);
 wc.hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDAPPLICON));
 if (!RegisterClass(&wc))
  return 0;

 // ---TODO--- Call module specific initialization routines here

 return 1;

Quote:
}

/*<---------------------------------------------------------------------->*/

HWND CreatekasWndClassWnd(void)
{
   GraphSheep = LoadBitmap(hInst, MAKEINTRESOURCE(SHEEP1));
   HDCSheep = CreateCompatibleDC(hdc);

   GraphBack = LoadBitmap(hInst, MAKEINTRESOURCE(BackIMG));
   HDCBack = CreateCompatibleDC(hdc);

   hbmOld = SelectObject(HDCSheep, GraphSheep);
   hbmOld = SelectObject(HDCBack, GraphBack);

 return CreateWindowEx(WS_EX_TOPMOST,
  "kasWndClass","Kill a Sheep - Alpha",

WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_CAPTION|WS_BORDER|WS_SYSMENU,
  206,138,210,163,
  NULL,
  NULL,
  hInst,
  NULL);

Quote:
}


/*<---------------------------------------------------------------------->*/
/* --- The following code comes from c:\lcc\lib\wizard\defOnCmd.tpl. */
BOOL SetSheep(int state)
{
 int tmpState;
 if(state<0||state>3)
  return FALSE;

 tmpState = SheepState;
 SheepState=state;

 if(SheepState!=tmpState)
  InvalidateRect(hwndMain,NULL,FALSE);

 return TRUE;

Quote:
}

void MainWndProc_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
 switch(id) {
  // ---TODO--- Add new menu commands here

  case IDM_EXIT:
  PostMessage(hwnd,WM_CLOSE,0,0);
  break;
 }

Quote:
}

/*<---------------------------------------------------------------------->*/

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
 PAINTSTRUCT ps;

 switch (msg) {

 case WM_COMMAND:
  HANDLE_WM_COMMAND(hwnd,wParam,lParam,MainWndProc_OnCommand);
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 case WM_NOTIFY:
  break;
 case WM_PAINT:
  hdc = BeginPaint(hwnd, &ps);
  BitBlt(hdc,0,0,206,138, HDCBack,0,0,SRCCOPY);
  BitBlt(hdc,2,2,100,100, HDCSheep,0,SheepState * 100,SRCCOPY);
  EndPaint(hwnd, &ps);
  break;
 default:
  return DefWindowProc(hwnd,msg,wParam,lParam);
 }

 return 0;

Quote:
}


/*<---------------------------------------------------------------------->*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, INT nCmdShow)
{
 MSG msg;
 HANDLE hAccelTable;

 hInst = hInstance;
 if (!InitApplication())
  return 0;
 hAccelTable = LoadAccelerators(hInst,MAKEINTRESOURCE(IDACCEL));
 if ((hwndMain = CreatekasWndClassWnd()) == (HWND)0)
  return 0;

 SheepState = 0; // Normal

 CreateWindow("BUTTON","ShotGun",BS_PUSHBUTTON | WS_CHILD |
WS_VISIBLE,104,2,99,32,hwndMain,NULL, button1,NULL);

 ShowWindow(hwndMain,SW_SHOW);

 while (GetMessage(&msg,NULL,0,0)) {
  if (!TranslateAccelerator(msg.hwnd,hAccelTable,&msg)) {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }
 return msg.wParam;

Quote:
}

----Main.c----


Fri, 02 Aug 2002 03:00:00 GMT  
 Buttons
I am a newbie, but from my VB experience I think you have to look for the
WM_CLICK message or one of those
Richie


Quote:
> Hi,

> Could someone show me how to use buttons in a app.
> I've got the following code, but I dont know how to see when the button is
> pressed.

> (btw, I'm not using MFC)

> Please help!!!

> --Lasse Hassing



Mon, 05 Aug 2002 03:00:00 GMT  
 Buttons
The notification of the click is send to the parent window of the button
through a WM_COMMAND message, with codeNotify == BN_CLICKED. You didn't
provide an id to your button, so unless it is the only one, you will have to
wether give it an id (this is the HMENU parameter in the CreateWindow
function) or to store in a global variable the handle of the button. The
first solution is better (no global var).

Vincent Jacquet



Tue, 06 Aug 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. buttons, check buttons, radio buttons, and such...

2. how to create a window without any close button (include OK button and X button)

3. Dialog Box two OK buttons, two Cancel buttons and TODO still on it

4. MSVC 1.52 button disable with owner draw buttons

5. Creating a Button in a Button

6. Drawing a button, that's not really a button

7. toolbar buttons with combo buttons not getting ON_UPDATE_COMMAND_UI messages

8. Button colour & Detecting mouse over button

9. check a single radio button in a radio-button-group

10. Accessing all other buttons with Context-help button in an SDI

11. Click 1 button = click 2 button?

12. How to make Cancel button not default button on Wizard Page

 

 
Powered by phpBB® Forum Software