|
-
Nov 19th, 2000, 12:36 AM
#1
Thread Starter
Frenzied Member
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
-
Nov 19th, 2000, 06:22 AM
#2
Monday Morning Lunatic
Here ya go:
Code:
#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()
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 19th, 2000, 01:43 PM
#3
Thread Starter
Frenzied Member
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
-
Nov 19th, 2000, 02:03 PM
#4
Monday Morning Lunatic
Oops...change LONG_PTR to LRESULT (I was using the latest version of the headers)
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 19th, 2000, 02:28 PM
#5
Thread Starter
Frenzied Member
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
-
Nov 19th, 2000, 02:31 PM
#6
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 19th, 2000, 02:49 PM
#7
Thread Starter
Frenzied Member
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
-
Nov 19th, 2000, 03:32 PM
#8
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 19th, 2000, 04:04 PM
#9
Thread Starter
Frenzied Member
Thanks! Works like a charm.. Don't know why mine didn't work.. Ohwell..
Dan
-
Nov 19th, 2000, 04:08 PM
#10
Monday Morning Lunatic
Also look at the Platform SDK at http://msdn.microsoft.com/library for loads of information on programming for Win32.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 19th, 2000, 07:49 PM
#11
To narrow it down, you can go to the Visual C++ library which is info for using VC++.
-
Nov 25th, 2000, 03:20 PM
#12
Frenzied Member
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.
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
|