Click to See Complete Forum and Search --> : C++ and Windows?
softwareguy74
Nov 18th, 2000, 11:36 PM
Hi,
I'm currently learning C++ (tech yourself C++ in 21 days) and it's going quite well!! I am actually suprised (and quite happy!!!) to find that my VB experience is helping me out as far as understading C++, despite the fact that most people on these forums seem to think that VB knowledge will not help you in learning C++.
Anyway, back to the question..
Because this book is strickly C++ (not Visual C++) it does not go over the subject of Win32 and C++. So I just wanted to find out from you experts out there what I have to look forward to after I master C++ and then move to C++ and Win32. Is it just a matter of calling Win32 API functions like you would in VB and just tie it in with the C++ code?
Any help would be appreciated.. In fact, if you could give me a CPP file that creates a form with a command button on it that pops up a message box in Visual C++, I would greatly appreciate it!!
Thanks,
Dan
parksie
Nov 19th, 2000, 05:22 AM
Here ya go:
#include <windows.h>
HWND ghWnd_Main;
HWND ghWnd_ButtonA;
HWND ghWnd_ButtonB;
LONG_PTR CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch(uMsg) {
case WM_CLOSE:
DestroyWindow(ghWnd_Main); // Destroy the main window
return 0;
case WM_DESTROY:
PostQuitMessage(0); // Quit the application
return 0;
case WM_COMMAND:
if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == ghWnd_ButtonA) {
MessageBox(hWnd, "Button A clicked!", "Button", MB_OK);
}
if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == ghWnd_ButtonB) {
MessageBox(hWnd, "Button B clicked!", "Button", MB_OK);
}
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam); // Not handled - let the system deal with it
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wcxMyClass;
wcxMyClass.cbSize = sizeof(WNDCLASSEX); // Size of structure in bytes
wcxMyClass.style = 0; // Extra style information
wcxMyClass.lpfnWndProc = WndProc; // Pointer to window procedure
wcxMyClass.cbClsExtra = 0; // Extra bytes in class definition
wcxMyClass.cbWndExtra = 0; // Extra bytes for each window
wcxMyClass.hInstance = hInstance; // Instance handle for the class
wcxMyClass.hIcon = NULL; // Default icon - use system
wcxMyClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load the system arrow
wcxMyClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); // Background brush
wcxMyClass.lpszMenuName = NULL; // Menu name [MAKEINTRESOURCE(IDM_MAINMENU)] - none here
wcxMyClass.lpszClassName = "TESTCLASS"; // Class name
wcxMyClass.hIconSm = NULL; // Small icon
RegisterClassEx(&wcxMyClass); // Register the class
ghWnd_Main = CreateWindow("TESTCLASS", "My test window", \
WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, \
NULL, NULL, hInstance, NULL);
if(ghWnd_Main) {
ghWnd_ButtonA = CreateWindow("Button", "Button A", \
WS_CHILD | WS_VISIBLE, \
50, 50, 200, 40, ghWnd_Main, NULL, hInstance, NULL);
ghWnd_ButtonB = CreateWindow("Button", "Button B", \
WS_CHILD | WS_VISIBLE, \
280, 50, 200, 40, ghWnd_Main, NULL, hInstance, NULL);
ShowWindow(ghWnd_Main, nCmdShow); // Show using the specific show style
}
/////////////////////////
// Handle message loop //
/////////////////////////
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg); // Translate keycode messages
DispatchMessage(&msg); // Send to the window
}
UnregisterClass("TESTCLASS", hInstance); // Unregister our window class
return msg.wParam; // msg.wParam contains the code passed to PostQuitMessage()
}
softwareguy74
Nov 19th, 2000, 12:43 PM
Thanks, but I think I'm having a problem here.. Is there any special setup required in my VC++ environment or can I just past your code into a .cpp file and compile it?
I started a blank project and added a .cpp file and pasted your code into it.. I then tried to build it and it generated 3 errors:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
c:\program files\microsoft visual studio\myprojects\test1\cpp1.cpp(7) : error C2143: syntax error : missing ';' before '__stdcall'
c:\program files\microsoft visual studio\myprojects\test1\cpp1.cpp(7) : error C2501: 'LONG_PTR' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\test1\cpp1.cpp(34) : error C2440: '=' : cannot convert from 'int (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long
)'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
Cpp1.exe - 3 error(s), 0 warning(s)
any ideas what this is?
Thanks,
Dan
parksie
Nov 19th, 2000, 01:03 PM
Oops...change LONG_PTR to LRESULT (I was using the latest version of the headers)
softwareguy74
Nov 19th, 2000, 01:28 PM
Welp, I think we're getting closer, it's down to 2 errors now..
--------------------Configuration: Cpp3 - Win32 Debug--------------------
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/Cpp3.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Cpp3.exe - 2 error(s), 0 warning(s)
Any ideas?
It compiles okay, but gives the above errors when trying to build..
thanks,
dan
parksie
Nov 19th, 2000, 01:31 PM
Technically, that's only one error...but...
The problem is not with the source. You didn't use the correct project type - it must be Win32 Application. (Not Console or MFC). Windows programs have WinMain as their entry-point, as opposed to _main.
softwareguy74
Nov 19th, 2000, 01:49 PM
Okay, I created a Win32 Application but now it's saying "Cannot execute program" when I try to run it..
Do I need to add anything else to the project in order to run it? I just created a win32 app and pasted your code right into a .cpp file..
Thanks,
Dan
parksie
Nov 19th, 2000, 02:32 PM
Here's a full project: http://www.parksie.uklinux.net/Raw.zip
softwareguy74
Nov 19th, 2000, 03:04 PM
Thanks! Works like a charm.. Don't know why mine didn't work.. Ohwell..
Dan
parksie
Nov 19th, 2000, 03:08 PM
Also look at the Platform SDK at http://msdn.microsoft.com/library for loads of information on programming for Win32.
To narrow it down, you can go to the Visual C++ library which is info for using VC++.
PsyVision
Nov 25th, 2000, 02:20 PM
sometime i find that if i compile and get an error then change something i get an error to do with linking, if so goto Build on the menu then click clean and then compile and it should work.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.