Enough console programming.

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2657 Views - Last Post: 01 May 2011 - 08:36 AM Rate Topic: -----

#1 Taibah  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 41
  • Joined: 23-February 11

Enough console programming.

Posted 16 April 2011 - 09:10 AM

Hi all

I started first with a book of C++ very good information so i learned the basic's loops etc.

But i was thinking im only programming in blackbox CMD. now i want to go further

I was intressed in windows libary but can somebody guide me how to learn that i read at msdn offical site but still dont understand how it works exactly. i started some a week ago.

Example

I created some button's with a textbox but i want to put example if i click on a button so i recive a message but i dont now how i can use it here is my code i aslo look at MSDN.

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
     HINSTANCE hPrevInstance,
     LPSTR lpCmdLine,
     int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style    = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra  = 0;
    wcex.cbWndExtra  = 0;
    wcex.hInstance  = hInstance;
    wcex.hIcon    = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
  MessageBox(NULL,
    _T("Call to RegisterClassEx failed!"),
    _T("Win32 Guided Tour"),
    NULL);

  return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
  szWindowClass,
  szTitle,
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  500, 500,
  NULL,
  NULL,
  hInstance,
  NULL
    );

    if (!hWnd)
    {
  MessageBox(NULL,
    _T("Call to CreateWindow failed!"),
    _T("Win32 Guided Tour"),
    NULL);

  return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
  nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Voer uw bericht in");
	switch (message)
		{
			case WM_PAINT:
				hdc = BeginPaint(hWnd, &ps);
				TextOut(hdc,5, 5,greeting, _tcslen(greeting));
				EndPaint(hWnd, &ps);
			break;
		case WM_CREATE:
			CreateWindow(TEXT("button"), TEXT("Submit"),
					WS_VISIBLE | WS_CHILD,
					300, 50, 80, 25,
					hWnd,(HMENU) 1,NULL, NULL
					);

			CreateWindow(TEXT("edit"), TEXT(""),
					WS_VISIBLE | WS_CHILD|WS_BORDER,
					30, 50, 250, 25,
					hWnd,(HMENU) 1, NULL, NULL
					);
			break;
		case WM_COMMAND:
			MessageBox(hWnd, _T("Message") , _T("Title"), MB_OK);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
	  break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	  break;
    }

    return 0;
}



This post has been edited by Taibah: 16 April 2011 - 09:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Enough console programming.

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Enough console programming.

Posted 16 April 2011 - 09:14 AM

Does my tutorial on using controls help?
link
Was This Post Helpful? 0
  • +
  • -

#3 Taibah  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 41
  • Joined: 23-February 11

Re: Enough console programming.

Posted 16 April 2011 - 09:43 AM

View PostAphex19, on 16 April 2011 - 09:14 AM, said:

Does my tutorial on using controls help?
link


Not really sorry. when i look up to msdn i see many functions but i dont understand how to make them work with the code.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Enough console programming.

Posted 16 April 2011 - 06:28 PM

You can add ID's to the button and textbox.

     

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tchar.h>

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

/* ID's for component */
#define ID_SUBMITBUTTON  101
#define ID_SUBMITTEXT    102


// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
     HINSTANCE hPrevInstance,
     LPSTR lpCmdLine,
     int nCmdShow)
{
    HWND hWnd;
    MSG msg;
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style    = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra  = 0;
    wcex.cbWndExtra  = 0;
    wcex.hInstance  = hInstance;
    wcex.hIcon    = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
  MessageBox(NULL,
    _T("Call to RegisterClassEx failed!"),
    _T("Win32 Guided Tour"),
    NULL);

  return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application

   hWnd = CreateWindow(
  szWindowClass,
  szTitle,
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  500, 500,
  NULL,
  NULL,
  hInstance,
  NULL
    );

    if (!hWnd)
    {
  MessageBox(NULL,
    _T("Call to CreateWindow failed!"),
    _T("Win32 Guided Tour"),
    NULL);

  return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
  nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:

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

    return (int) msg.wParam;
}


//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 PAINTSTRUCT ps;
 HDC hdc;
 TCHAR greeting[] = _T("Voer uw bericht in");
 char buffer[81];

 switch (message)
 {
  case WM_PAINT:
       hdc = BeginPaint(hWnd, &ps);
       TextOut(hdc,5, 5,greeting, _tcslen(greeting));
       EndPaint(hWnd, &ps);
       break;

  case WM_CREATE:
       CreateWindow(TEXT("button"), TEXT("Submit"),
        WS_VISIBLE | WS_CHILD,
	300, 50, 80, 25,
	hWnd,(HMENU) ID_SUBMITBUTTON, NULL, NULL
	);

       CreateWindow(TEXT("edit"), TEXT(""),
        WS_VISIBLE | WS_CHILD | WS_BORDER,
	30, 50, 250, 25,
	hWnd,(HMENU) ID_SUBMITTEXT, NULL, NULL
	);

       break;

  case WM_COMMAND:

        switch(LOWORD(wParam))
        {
         case ID_SUBMITBUTTON :
              {
               GetDlgItemText(hWnd, ID_SUBMITTEXT, buffer, 81);
               MessageBox(hWnd, TEXT(buffer),  
                          TEXT("Submit Button Pop-Up"), 
                          MB_OK|MB_ICONEXCLAMATION);
               break;
              }

        } /* switch param */
      
        break;
        
  case WM_DESTROY:
       PostQuitMessage(0);
       break;

  default:
       return DefWindowProc(hWnd, message, wParam, lParam);

 } /* switch message */

 return 0;
}



Was This Post Helpful? 0
  • +
  • -

#5 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Enough console programming.

Posted 16 April 2011 - 07:26 PM

Why not look at learning something a little more abstracted, like Qt?
Was This Post Helpful? 1
  • +
  • -

#6 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5304
  • View blog
  • Posts: 27,195
  • Joined: 10-May 07

Re: Enough console programming.

Posted 16 April 2011 - 08:27 PM

To follow up on #define's post, you must give everything an ID, & define the id. The callback loop is listening for the events, but if nothing is defined then the button is just a button & nothing will happen with it.

I believe the bloodshed compiler & Visual compilers will have what is called an rc, or resource compiler. They will allow you to develop the windows rc file, which will contain all of the layout for the main window & child windows, as well as allow you to visually edit their contents. Just be certain to give everything an ID, & define it in a header file.
Was This Post Helpful? 0
  • +
  • -

#7 Taibah  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 41
  • Joined: 23-February 11

Re: Enough console programming.

Posted 17 April 2011 - 02:56 AM

View Post#define, on 16 April 2011 - 06:28 PM, said:

You can add ID's to the button and textbox.

     

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tchar.h>

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

/* ID's for component */
#define ID_SUBMITBUTTON  101
#define ID_SUBMITTEXT    102


// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
     HINSTANCE hPrevInstance,
     LPSTR lpCmdLine,
     int nCmdShow)
{
    HWND hWnd;
    MSG msg;
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style    = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra  = 0;
    wcex.cbWndExtra  = 0;
    wcex.hInstance  = hInstance;
    wcex.hIcon    = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
  MessageBox(NULL,
    _T("Call to RegisterClassEx failed!"),
    _T("Win32 Guided Tour"),
    NULL);

  return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application

   hWnd = CreateWindow(
  szWindowClass,
  szTitle,
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  500, 500,
  NULL,
  NULL,
  hInstance,
  NULL
    );

    if (!hWnd)
    {
  MessageBox(NULL,
    _T("Call to CreateWindow failed!"),
    _T("Win32 Guided Tour"),
    NULL);

  return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
  nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:

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

    return (int) msg.wParam;
}


//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 PAINTSTRUCT ps;
 HDC hdc;
 TCHAR greeting[] = _T("Voer uw bericht in");
 char buffer[81];

 switch (message)
 {
  case WM_PAINT:
       hdc = BeginPaint(hWnd, &ps);
       TextOut(hdc,5, 5,greeting, _tcslen(greeting));
       EndPaint(hWnd, &ps);
       break;

  case WM_CREATE:
       CreateWindow(TEXT("button"), TEXT("Submit"),
        WS_VISIBLE | WS_CHILD,
	300, 50, 80, 25,
	hWnd,(HMENU) ID_SUBMITBUTTON, NULL, NULL
	);

       CreateWindow(TEXT("edit"), TEXT(""),
        WS_VISIBLE | WS_CHILD | WS_BORDER,
	30, 50, 250, 25,
	hWnd,(HMENU) ID_SUBMITTEXT, NULL, NULL
	);

       break;

  case WM_COMMAND:

        switch(LOWORD(wParam))
        {
         case ID_SUBMITBUTTON :
              {
               GetDlgItemText(hWnd, ID_SUBMITTEXT, buffer, 81);
               MessageBox(hWnd, TEXT(buffer),  
                          TEXT("Submit Button Pop-Up"), 
                          MB_OK|MB_ICONEXCLAMATION);
               break;
              }

        } /* switch param */
      
        break;
        
  case WM_DESTROY:
       PostQuitMessage(0);
       break;

  default:
       return DefWindowProc(hWnd, message, wParam, lParam);

 } /* switch message */

 return 0;
}




Thank you very much i didnt now about that you can gave id's also so learned some new thing.
BTW: what does switch(LOWORD(wParam)) means will it also work if i dont use that in the switch statements.
Was This Post Helpful? 0
  • +
  • -

#8 manojtc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-April 11

Re: Enough console programming.

Posted 17 April 2011 - 03:51 AM

If you're going to program for Windows, I think the easier way is to do it in C#, VB or even Java. They have simpler UI libraries than you'd get in C or C++. If you're still inclined in using windows.h, theForger's Win32 tutorial helped me a lot. It's at http://winprog.org/tutorial/

This post has been edited by manojtc: 17 April 2011 - 03:52 AM

Was This Post Helpful? 0
  • +
  • -

#9 Taibah  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 41
  • Joined: 23-February 11

Re: Enough console programming.

Posted 17 April 2011 - 04:05 AM

View Postmanojtc, on 17 April 2011 - 03:51 AM, said:

If you're going to program for Windows, I think the easier way is to do it in C#, VB or even Java. They have simpler UI libraries than you'd get in C or C++. If you're still inclined in using windows.h, theForger's Win32 tutorial helped me a lot. It's at http://winprog.org/tutorial/


I realy dont want to use C# or VB it always need dependence. i got more intresset in C++ even it takes me 10 years to learn it.

This post has been edited by Taibah: 17 April 2011 - 04:05 AM

Was This Post Helpful? 0
  • +
  • -

#10 Taibah  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 41
  • Joined: 23-February 11

Re: Enough console programming.

Posted 17 April 2011 - 04:38 AM

@#define



ERROR argument of type "char*" is incompatible with parameter of type "LPWSTR"
error indentifier "Lbuffer" is undefinend

This post has been edited by Taibah: 17 April 2011 - 04:39 AM

Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Enough console programming.

Posted 17 April 2011 - 04:59 AM

View PostTaibah, on 17 April 2011 - 01:38 PM, said:

@#define

ERROR argument of type "char*" is incompatible with parameter of type "LPWSTR"
error indentifier "Lbuffer" is undefinend


Try changing
 char buffer[81];



to :
 TCHAR buffer[81];



And change this
   MessageBox(hWnd, TEXT(buffer),  
              TEXT("Submit Button Pop-Up"), 
              MB_OK|MB_ICONEXCLAMATION);



to this

   MessageBox(hWnd, buffer,  
              TEXT("Submit Button Pop-Up"), 
              MB_OK|MB_ICONEXCLAMATION);



See if that works.
Was This Post Helpful? 0
  • +
  • -

#12 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Enough console programming.

Posted 17 April 2011 - 05:35 AM

The best way to do GUI in C++ is definitely Qt.
It's very easy to learn and it's cross platform too :)
Check this link out as this is how I started learning it http://doc.qt.nokia....s-tutorial.html

This post has been edited by DivideByZero: 17 April 2011 - 05:36 AM

Was This Post Helpful? 0
  • +
  • -

#13 Taibah  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 41
  • Joined: 23-February 11

Re: Enough console programming.

Posted 17 April 2011 - 07:11 AM

View Post#define, on 17 April 2011 - 04:59 AM, said:

View PostTaibah, on 17 April 2011 - 01:38 PM, said:

@#define

ERROR argument of type "char*" is incompatible with parameter of type "LPWSTR"
error indentifier "Lbuffer" is undefinend


Try changing
 char buffer[81];



to :
 TCHAR buffer[81];



And change this
   MessageBox(hWnd, TEXT(buffer),  
              TEXT("Submit Button Pop-Up"), 
              MB_OK|MB_ICONEXCLAMATION);



to this

   MessageBox(hWnd, buffer,  
              TEXT("Submit Button Pop-Up"), 
              MB_OK|MB_ICONEXCLAMATION);



See if that works.



Thanks this worked for me;) i got a personal question how much exprience you got with C++ and how fast did you understand the windows libary:)
Was This Post Helpful? 0
  • +
  • -

#14 manojtc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-April 11

Re: Enough console programming.

Posted 17 April 2011 - 07:50 AM

View PostTaibah, on 17 April 2011 - 04:05 AM, said:

I realy dont want to use C# or VB it always need dependence. i got more intresset in C++ even it takes me 10 years to learn it.


Agreed. I suggested those since you were doing Win32 anyways. Did you think about cross-platform UI frameworks (like qt)?
Was This Post Helpful? 0
  • +
  • -

#15 Taibah  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 41
  • Joined: 23-February 11

Re: Enough console programming.

Posted 17 April 2011 - 11:01 AM

View Postmanojtc, on 17 April 2011 - 07:50 AM, said:

View PostTaibah, on 17 April 2011 - 04:05 AM, said:

I realy dont want to use C# or VB it always need dependence. i got more intresset in C++ even it takes me 10 years to learn it.


Agreed. I suggested those since you were doing Win32 anyways. Did you think about cross-platform UI frameworks (like qt)?


I will look to it thank you
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2