|
-
February 22nd, 2008, 09:54 AM
#1
Events on WinApi C++
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.
Code:
#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?
Last edited by skoumas; February 22nd, 2008 at 10:15 AM.
-
February 22nd, 2008, 11:24 AM
#2
Re: Events on WinApi C++
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).
-
February 22nd, 2008, 01:14 PM
#3
Re: Events on WinApi C++
http://www.tutorialized.com/view/tut...i-basics/27406
this is the tutorial that helped me to make the upper code.
-
February 22nd, 2008, 07:00 PM
#4
Re: Events on WinApi C++
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.
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.
Code:
#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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|