Click to See Complete Forum and Search --> : Events on WinApi C++


skoumas
February 22nd, 2008, 08:54 AM
Hello there.

I am using Visual C++ Express right now.

I dont want to make applications throught the automated processes that this pack offers me, cause application gets much larger than they should have bin.

Yesterday I made my first step on WinApi: this is the result.


#include <windows.h>


char* App_Title = "Skoumas and the brains at the kangela";


LRESULT WINAPI myProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

if(message == WM_CLOSE) {

int apantisi;
apantisi = MessageBox(NULL, "Are you Sure?",App_Title,1);

if (apantisi==1)
PostQuitMessage(0);


}
return 0;
}

LRESULT WINAPI myProcbutton(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

if(message == 1) {

int apantisi;
apantisi = MessageBox(NULL, "Are you Sure?",App_Title,1);

if (apantisi==1)
PostQuitMessage(0);


}
return 0;
}


int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)

{
MSG msg;


HWND myDialog=CreateWindowEx(0, WC_DIALOG, App_Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
600, 100, 400, 200, NULL, NULL,NULL,NULL
);

HWND button1=CreateWindowEx( 0,"BUTTON", "My Button",BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
50,50,100,25,myDialog,NULL,NULL,NULL

);


SetWindowLong(myDialog, DWL_DLGPROC, (long) myProc);
SetWindowLong(button1, DWL_DLGPROC, (long) myProcbutton);


while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);

}


return 0;

}



------------------------------------------------------

In this code you can see that the program generates a simple window and a button inside it.

I want to be able when i press the button to show a message box.

Can you please help me?

heteroy
February 22nd, 2008, 10:24 AM
There are several problems I can see here.

1) You should use the resource editor to create a dialog resource and then use CreateDialog vs CreateWindow when creating a dialog. You can put the button control in the dialog resource so you don't have to create it in code.

2) A button does not have a dialogproc. It has a wndproc which for what you are trying to do should be sufficient. What you need to do is have the dialog (parent of the button) handle WM_COMMAND message for BN_CLICK with the id of the button.

3) Before you start pumping messages, call ShowWindow(hDlg) and UpdateWindow(hDlg).

skoumas
February 22nd, 2008, 12:14 PM
http://www.tutorialized.com/view/tutorial/Start-Windows-Programmng-winapi-win32-Api-basics/27406

this is the tutorial that helped me to make the upper code.

heteroy
February 22nd, 2008, 06:00 PM
I don't much care for the tutorial you were looking at. If you are serious about learning windows, a good place to start is with Charles Petzold's Programming Windows 5th Edition (http://www.charlespetzold.com/pw5/index.html).

In the meantime, I banged out a sample that works based on your code. It is compiled for UNICODE so you will have to set that in your project options.



#include <windows.h>

const WCHAR* App_Title = L"Skoumas and the brains at the kangela";
const UINT idButton = 101;

LRESULT WINAPI MyDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CLOSE:
{
int apantisi;
apantisi = MessageBox(NULL, L"Are you Sure?",App_Title,1);

if (apantisi==1)
{
PostQuitMessage(0);
}
}
break;

case WM_COMMAND:
if (LOWORD(wParam) == idButton && HIWORD(wParam) == BN_CLICKED)
{
int apantisi;
apantisi = MessageBox(NULL, L"Are you Sure?",App_Title,1);

if (apantisi==1)
{
PostQuitMessage(0);
}
}
break;

default:
break;

}
return 0;
}

int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
HWND hDlg = CreateWindowEx(0, WC_DIALOG, App_Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
600, 100, 400, 200, NULL, NULL, NULL, NULL);

if (hDlg)
{
SetWindowLong(hDlg, DWL_DLGPROC, (long) MyDlgProc);
HWND hwndBtn = CreateWindowEx(0, L"BUTTON", L"My Button", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
50, 50, 100, 25, hDlg, (HMENU)idButton, NULL, NULL);
if (hwndBtn)
{
ShowWindow(hDlg, SW_SHOWNORMAL);
UpdateWindow(hDlg);

MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
return 0;
}