Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic GUI 1

Status
Not open for further replies.

VBMan

Programmer
Jul 1, 2000
49
US
I was wondering how I could go about making a simple GUI like that of a Visual Basic App. This would be like a Window a button and a text box.

Thanks,
~Ben
 
Your best bet is to look at either Borland's VCL or Microsoft's MFC. There are also some freeware/shareware libraries like GNU.
James P. Cottingham
 
If you want to be able to create a window, menus, scrollbars, etc with a click and drag of a mouse then look to see if your C++ compiler came with a resource compiler. Then you have to write your code to use the controls, menu's etc as resources. Look up resources in your help file.



Kim_Christensen@telus.net
 
Yes but how do you make a button or a textfile from scratch or is it possible to call it from a .h file.

thanks,
~ben
 
By "from scratch" do you mean without using the resource compiler and just calling the CreateWindow() API function from your program and specifing if you want BUTTON, EDIT, SCROLLBAR, etc styles? Remember that all controls are actually "windows".
Or do you mean, "How do I create a NEW resource file and place new resources such as BUTTONs, EDITs, SCROLLBARs, etc within it?


Kim_Christensen@telus.net
 
i mean creating button and the like from raw code. And how what i need to do to act apon their actions. You know like what kind of .h file is used what commands.

Thank you for all the help,

~ben
 
Ok, here is a modified version of a Hello Windows example.
I have added a scroll bar and a button.

#define SCROLL_ME 101
#define PRESS_ME 100
#include <windows.h>

HANDLE ThisInstance;

long FAR PASCAL WndProc (HWND, UINT, UINT, LONG) ;

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
static char szAppName[] = &quot;HelloWin&quot; ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

if (!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

RegisterClass (&amp;wndclass) ;
}

hwnd = CreateWindow (szAppName, // window class name
&quot;The Hello Program&quot;, // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
ThisInstance = hInstance;

while (GetMessage (&amp;msg, NULL, 0, 0))
{
TranslateMessage (&amp;msg) ;
DispatchMessage (&amp;msg) ;
}
return msg.wParam ;
}

long FAR PASCAL WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;

switch (message)
{
case WM_CREATE:

CreateWindow (&quot;BUTTON&quot;, // window class name
&quot;Press Me!&quot;, // window caption
WS_VISIBLE | WS_CHILDWINDOW, // window style
100, // initial x position
100, // initial y position
100, // initial x size
50, // initial y size
hwnd, // parent window handle
PRESS_ME, // window menu handle
ThisInstance, // program instance handle
NULL) ; // creation parameters

CreateWindow (&quot;SCROLLBAR&quot;, // window class name
&quot;Scroll Me!&quot;, // window caption
WS_VISIBLE | WS_CHILDWINDOW, // window style
250, // initial x position
100, // initial y position
200, // initial x size
25, // initial y size
hwnd, // parent window handle
SCROLL_ME, // window menu handle
ThisInstance, // program instance handle
NULL) ; // creation parameters



return 0;


case WM_COMMAND:
if ( wParam == PRESS_ME) MessageBox ( hwnd, &quot;I was pressed!&quot;, &quot;BUTTON&quot;, MB_OK);
return 0;

case WM_HSCROLL:

if ( wParam == SB_LINEDOWN )
MessageBox ( hwnd, &quot;I was scrolled right&quot;, &quot;SCROLLBAR&quot;, MB_OK);
if ( wParam == SB_LINEUP )
MessageBox ( hwnd, &quot;I was scrolled left&quot;, &quot;SCROLLBAR&quot;, MB_OK);
return 0;

case WM_PAINT :
hdc = BeginPaint (hwnd, &amp;ps) ;

GetClientRect (hwnd, &amp;rect) ;

DrawText (hdc, &quot;Hello, Windows!&quot;, -1, &amp;rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

EndPaint (hwnd, &amp;ps);
return 0 ;

case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}



Kim_Christensen@telus.net
 
I entered it in and this is what i got:


line 26
untitled1.cpp
ANSI C++ forbids implicit conversion from `void *' in assignment

line 43
untitled1.cpp
ANSI C++ forbids implicit conversion from `void *' in argument passing

line 77
untitled1.cpp
passing `int' to argument 10 of `CreateWindowExA(long unsigned int, const CHAR *, const CHAR *, long unsigned int, int, int, int, int, HWND__ *, HMENU__ *, HINSTANCE__ *, void *)' lacks a cast

line 77
untitled1.cpp
ANSI C++ forbids implicit conversion from `void *' in argument passing

line 89
untitled1.cpp
passing `int' to argument 10 of `CreateWindowExA(long unsigned int, const CHAR *, const CHAR *, long unsigned int, int, int, int, int, HWND__ *, HMENU__ *, HINSTANCE__ *, void *)' lacks a cast

line 89
untitled1.cpp
ANSI C++ forbids implicit conversion from `void *' in argument passing




I don't know why it is doing this. I cut and pasted it to see how it worked and this is what I got.


My greatest thanks for your troubles,
~Ben
 
Just to add something: the PASCAL keyword has been replaced by WINAPI and CreateWindowEx() offers the Windows9X/2K style Window features. CreateWindow() is still included for compatability with Windows 3.X - but is also obsolete. As for your error - I don't use a Borland compiler, so I can't help you here. Rob Marriott
rob@career-connections.net
 
Neither do I. I use Dev-C++. I think that could be a problem.

 
As CTCC1 noted, my example was win16. If your compiler can create a 16bit target, try that. I'm not familiar with Dev-C++ at all, but I did cut and paste the code back into my compiler and it ran fine with one little mod. (I had to unwrap one line to eliminate an unterminated string error due to TekTips formatting) The key function to look up in your compilers help file is CreateWindowEx() for WIN32 or CreateWindow() for win16.


Kim_Christensen@telus.net
 
Dev-C++ comes with a lot of built in features for Win32 programming. It has a built in window maker and popup box generator.

You should check them out if you want a basis in the language.

Hope this helps,

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top