|
-
Aug 29th, 2001, 10:34 AM
#1
Thread Starter
Fanatic Member
A Window
Code:
//CREATOR TOM ZHANG, 12 YEARS OLD
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize =sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc =WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra =0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName=NULL;
wc.lpszClassName=g_szClassName;
wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
if(!RegisterClassEx(&wc)){
return 0;
}
hwnd = CreateWindowEx(CW_USEDEFAULT,g_szClassName,"Cool Man",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500,800,NULL,NULL,hInstance,NULL);
if(hwnd==NULL){
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MessageBox(NULL,"Visual C++ Coding is much but cool!","From Tom Zhang",MB_OK);
while(GetMessage(&Msg,NULL,0,0)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Msg.wParam;
}
That code works... Just wanna know how to DISPLAY A TEXT ON THE PROGRAM...

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Aug 29th, 2001, 12:38 PM
#2
Frenzied Member
What do you mean? If you mean cretinf a textbox then use the CreateWindow API but pass "EDIT" as the class name parameter. That will crete a text box. To put text in the text box use the SetWindowText API or send the WM_SETTEXT message.
-
Aug 29th, 2001, 01:11 PM
#3
PowerPoster
If you just want to draw the text on the window then try this code:
PHP Code:
//CREATOR TOM ZHANG, 82 YEARS OLD
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
RECT rect;
HDC winhdc;
switch(msg){
case WM_PAINT:
GetClientRect(hwnd, &rect);
winhdc = BeginPaint(hwnd, &ps);
DrawText(winhdc, "The Text", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hwnd, &ps);
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize =sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc =WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra =0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName=NULL;
wc.lpszClassName=g_szClassName;
wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
if(!RegisterClassEx(&wc)){
return 0;
}
hwnd = CreateWindowEx(CW_USEDEFAULT,g_szClassName,"Cool Man",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500,800,NULL,NULL,hInstance,NULL);
if(hwnd==NULL){
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MessageBox(NULL,"Visual C++ Coding is much but cool!","From Tom Zhang",MB_OK);
while(GetMessage(&Msg,NULL,0,0)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Msg.wParam;
}
-
Aug 29th, 2001, 08:43 PM
#4
PowerPoster
ohh sorry,
Just put:
in the declarations - under the dec "HDC winhdc" or "RECT rect"
-
Aug 29th, 2001, 08:43 PM
#5
just add the declaration of the PAINTSTRUCT at the beginning of the WndProc.
Code:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
RECT rect;
HDC winhdc;
PAINTSTRUCT ps; // <<-- Here !!
switch(msg){
case WM_PAINT:
GetClientRect(hwnd, &rect);
winhdc = BeginPaint(hwnd, &ps);
DrawText(winhdc, "The Text", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hwnd, &ps);
return 0;
case WM_CLOSE:
// etc...
}
The PAINTSTRUCT contains information about the properties of the things to be drawed such as color and thickness (of lines)
-
Aug 30th, 2001, 09:39 AM
#6
Thread Starter
Fanatic Member
Thanks
It worked, but how come it's like a Tiled Wallpaper? When I enlarge it, it has a lot of the text...

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Aug 30th, 2001, 09:47 AM
#7
Frenzied Member
Just put a static conrtol on your window.....
PHP Code:
HWND MyStatic
MyStatic = CreateWindow("Static", "I am Prog_Tom's Static Control", WS_CHILD | WS_VISIBLE, 0, 0, 150, 20, hwnd, NULL, hInstance, NULL);
and poof.....you have a static box
-
Aug 30th, 2001, 09:55 AM
#8
Frenzied Member
so here would be your new code
PHP Code:
//CREATOR TOM ZHANG, 12 YEARS OLD
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize =sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc =WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra =0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName=NULL;
wc.lpszClassName=g_szClassName;
wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
if(!RegisterClassEx(&wc)){
return 0;
}
hwnd = CreateWindowEx(CW_USEDEFAULT,g_szClassName,"Cool Man",WS_OVERLAPPEDWINDOW,0,0,300,200,NULL,NULL,hInstance,NULL);
HWND MyStatic;
MyStatic = CreateWindow("Static", "I am Prog_Tom's Static Control", WS_CHILD | WS_VISIBLE, 0, 0, 200, 20, hwnd, NULL, hInstance, NULL);
if(hwnd==NULL){
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MessageBox(NULL,"Visual C++ Coding is much but cool!","From Tom Zhang",MB_OK);
while(GetMessage(&Msg,NULL,0,0)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Msg.wParam;
}
enjoy
-
Aug 30th, 2001, 12:09 PM
#9
PowerPoster
I just made a little change. So here is the new code
PHP Code:
//CREATOR TOM ZHANG, 85 YEARS OLD
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize =sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc =WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra =0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName=NULL;
wc.lpszClassName=g_szClassName;
wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
if(!RegisterClassEx(&wc)){
return 0;
}
hwnd = CreateWindowEx(CW_USEDEFAULT,g_szClassName,"Cool Man",WS_OVERLAPPEDWINDOW,0,0,300,200,NULL,NULL,hInstance,NULL);
HWND MyStatic;
MyStatic = CreateWindow("Static", "I am Prog_Tom's Static Control", WS_CHILD | WS_VISIBLE, 0, 0, 200, 20, hwnd, NULL, hInstance, NULL);
if(hwnd==NULL){
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MessageBox(NULL,"Visual C++ Coding is much but cool!","From Tom Zhang",MB_OK);
while(GetMessage(&Msg,NULL,0,0)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Msg.wParam;
}
enjoy
-
Aug 30th, 2001, 05:57 PM
#10
Thread Starter
Fanatic Member
Reasons?
I'm a new comer. Can you guys tell me which method is more popular? Also, do you guys know how to create a TextBox and a CommandButton?
Thanks,

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Aug 30th, 2001, 08:07 PM
#11
PowerPoster
Re: Reasons?
Originally posted by prog_tom
I'm a new comer. Can you guys tell me which method is more popular?
What do you mean "which method?"?
---
You can create textboxes, buttons using the spicific class names.
A button has a class name of "button" (so simple)
An editbox has a class name of "EDIT"
Here is a simple example of creating a button and a text box. I just modified your code of creating a simple window:
PHP Code:
//CREATOR TOM ZHANG, 12 YEARS OLD
#include <windows.h>
HINSTANCE hinst;
HWND edithwnd, buttonhwnd; //Handles of button and editbox
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_SHOWWINDOW:
//Create a button
buttonhwnd = CreateWindowEx(NULL,
"button",
"This is a button",
WS_CHILD | WS_VISIBLE,
12,
12,
49,
49,
hwnd,
NULL,
hinst,
NULL);
//Create the editbox
buttonhwnd = CreateWindowEx(NULL,
"EDIT",
"This is an editbox",
WS_CHILD | WS_VISIBLE,
50,
50,
100,
100,
hwnd,
NULL,
hinst,
NULL);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
hinst = hInstance;
wc.cbSize =sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc =WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra =0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName=NULL;
wc.lpszClassName=g_szClassName;
wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
if(!RegisterClassEx(&wc)){
return 0;
}
hwnd = CreateWindowEx(CW_USEDEFAULT,g_szClassName,"Cool Man",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500,800,NULL,NULL,hInstance,NULL);
if(hwnd==NULL){
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MessageBox(NULL,"Visual C++ Coding is much but cool!","From Tom Zhang",MB_OK);
while(GetMessage(&Msg,NULL,0,0)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Msg.wParam;
}
If you are just getting started then I suggest that you buy a book like "Programming windows by Charles Petzold"
You can also look through the MSDN library for some refrence
-
Aug 30th, 2001, 08:26 PM
#12
Frenzied Member
Originally posted by abdul
enjoy
Hey stop stealing my line!
-
Aug 30th, 2001, 10:30 PM
#13
-
Aug 31st, 2001, 10:44 AM
#14
PowerPoster
"Programming windows by Charles Petzold"
Get this book and it will get you started in windows programming. I don't know but I found an electronic version of that book at maxcode.com
You can search at that site and you will find that book
-
Sep 1st, 2001, 06:00 AM
#15
Thread Starter
Fanatic Member
Not Found
Well anyways. I can't find that book. Please have a look at this code... that I tried to create a Button with... It doesn't work... The Button is not showwned...
Code:
//INPUT NAMES AND AGES:
//ANOTHER ONE FROM TOM
//PROGTZ8894
#include <windows.h>
const char tzID[]="myClassWindow";
HWND but1;
HINSTANCE hInst;
int nCmdShoW;
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch(msg){
case WM_SHOWWINDOW:
but1=CreateWindowEx(NULL,"button","Submit",WS_CHILD|WS_VISIBLE,100,100,40,20,but1,NULL,hInst,NULL);
ShowWindow(but1,nCmdShoW);
UpdateWindow(but1);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
WNDCLASSEX tz;
HWND hwnd;
MSG Msg;
tz.cbSize=sizeof(WNDCLASSEX);
tz.style=0;
tz.lpfnWndProc=WndProc;
tz.cbClsExtra =0;
tz.cbWndExtra =0;
tz.hInstance = hInstance;
tz.hIcon=LoadIcon(NULL,IDI_APPLICATION);
tz.hCursor=LoadCursor(NULL,IDC_ARROW);
tz.hbrBackground=(HBRUSH)(COLOR_WINDOW+8);
tz.lpszMenuName=NULL;
tz.lpszClassName=tzID;
tz.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
if(!RegisterClassEx(&tz)){
MessageBox(hwnd,"Error while Registering the Program","",MB_OK);
return 0;
}
hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,tzID,"Welcome to my Program!",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,200,100,NULL,NULL,hInstance,NULL);
if(hwnd==NULL){
MessageBox(hwnd,"Errow while Creating the Window","",MB_OK);
return 0;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg,NULL,0,0)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
How to declare a CLICK Event for the Button? Do you know how to Set the Icon of the Program? I mean every time I compiled it the ICON is like a DOS Icon! Though in the Program it's not... Do you know how to Center the Window? Like
VB Code:
Private Sub Form_Load()
Me.Move (-me.width+screen.width)/2,(-me.height+screen.height)/2
End Sub

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Sep 1st, 2001, 06:13 AM
#16
Thread Starter
Fanatic Member
Write to a File?
Oh and do you guys know how to write to a File in Win32 App Programming? I know how to do it in Console programming...
Code:
#include <fstream>
int main(){
ofstream IP("c:\up.txt");
if(IP.Is_Open()){
IP<<"Hey Man\n";
IP.Close();
}
return 0;
}

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Sep 1st, 2001, 07:37 AM
#17
Lively Member
Creating the event
Visual Basic is event driven. C++ is not. This is how you do it. Notice the parts I changed in your code
[PHP]
//Sample Program for Tom
#include <windows.h>
const char tzID[]="myClassWindow";
#define IDC_BUTTON1 9001
//Notice that I removed your hwnd and made a define statement.
//You'll see why later
HINSTANCE hInst;
//int nCmdShoW; This really isnt necessary
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch(msg){
case WM_CREATE: //This stuff should really be done in WM_CREATE
CreateWindowEx(NULL,"button","Submit",WS_CHILD|WS_VISIBLE,100,100,40,20,hwnd,(HMENU)IDC_BUTTON1,hIns t,NULL);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
MessageBox(hwnd,"You Clicked the Button!","",0);
//This is where we use the define statement
break;
}
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
WNDCLASSEX tz;
HWND hwnd;
MSG Msg;
tz.cbSize=sizeof(WNDCLASSEX);
tz.style=0;
tz.lpfnWndProc=WndProc;
tz.cbClsExtra =0;
tz.cbWndExtra =0;
tz.hInstance = hInstance;
tz.hIcon=LoadIcon(NULL,IDI_APPLICATION);
tz.hCursor=LoadCursor(NULL,IDC_ARROW);
tz.hbrBackground=(HBRUSH)(COLOR_WINDOW+8); //I beleive your only supposed to add 1 to this....?
tz.lpszMenuName=NULL;
tz.lpszClassName=tzID;
tz.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
if(!RegisterClassEx(&tz)){
MessageBox(hwnd,"Error while Registering the Program","",MB_OK);
return 0;
}
hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,tzID,"Welcome to my Program!",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,200,100,NULL,NULL,hInstance,NULL);
if(hwnd==NULL){
MessageBox(hwnd,"Errow while Creating the Window","",MB_OK);
return 0;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg,NULL,0,0)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
-
Sep 1st, 2001, 11:28 AM
#18
Frenzied Member
"How to declare a CLICK Event for the Button? Do you know how to Set the Icon of the Program? I mean every time I compiled it the ICON is like a DOS Icon! Though in the Program it's not... Do you know how to Center the Window? "
um, I already answered that...the whole WM_COMMAND thing?
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
|