|
-
Jul 8th, 2001, 09:35 AM
#1
How to make a simple drawing?
Here i post the code of a simple function. I want it to create a window and draw a circle in it, but i can't make it. 
[code]
HWND initEditor(HWND hMainWnd) {
HWND hewnd;
HDC hDispl;
PAINTSTRUCT *lpPaint;
hewnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szEditClassName,
"Test Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 350,
hMainWnd, NULL, g_hEdInst, NULL);
ShowWindow(hewnd, SW_SHOWNORMAL);
UpdateWindow(hewnd);
hDispl = BeginPaint(hwnd, lpPaint);
Ellipse(hDispl, 10, 10, 15, 15); // this should draw a circle!!!!
EndPaint(hwnd, lpPaint);
}
[\code]
Probably there's something wrong with it, but i can't find anything wrong...
Thanks
-
Jul 8th, 2001, 12:01 PM
#2
PowerPoster
Can you post the full code
Can you post the full code because if find some words that should not be there. One of them is the hwnd of the window. Because you are not drawing the ellipse in "WndProc" you dont have any "hwnd". Use "hewnd" because that is what you put for your windows' hwnd.
Just post the full code here and i may be able to fix it for you
-
Jul 9th, 2001, 05:33 AM
#3
well, the full code is quite large, but this is it.
I made an error when transcribing the code in here, because the original one is slpitted up in parts, and there i was using some other names for vars. Here it is:
#include <windows.h>
#include <wingdi.h>
#include "nnetwork.rh"
void draw_cell(HDC handle, int x, int y);
void DrawNetwork(HWND hwnd);
static char g_szMainClassName[] = "Main Window class";
static char g_szEditClassName[] = "Network Editor class";
static HINSTANCE g_hInst = NULL;
static HINSTANCE g_hEdInst = NULL;
class cell {
float x, y;
cell(float a, float b) {x=a;y=b;}
void sep_pos(float a, float b) {x=a; y=b;}
};
////////////////////////////////////////////////////////////////////////////////
// EDITOR WINDOW PROCEDURES //
////////////////////////////////////////////////////////////////////////////////
void draw_cell(HDC handle, int x, int y) {
Ellipse(handle, x, y, x+5, y+5);
}
void DrawNetwork(HWND hwnd) {
HDC hDispl;
PAINTSTRUCT *lpPaint;
hDispl = BeginPaint(hwnd, lpPaint);
draw_cell(hDispl, 1, 1);
EndPaint(hwnd, lpPaint);
}
LRESULT CALLBACK EditProc(HWND hewnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CLOSE:
DestroyWindow(hewnd);
break;
case WM_DESTROY:
// PostQuitMessage(0);
break;
case WM_PAINT:
DrawNetwork(hewnd);
default:
return DefWindowProc(hewnd, Message, wParam, lParam);
}
return 0;
}
HWND initEditor(HWND hMainWnd) {
HWND hewnd;
hewnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szEditClassName,
"Network Editor",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 350,
hMainWnd, NULL, g_hEdInst, NULL);
if(hewnd == NULL)
{
MessageBox(0, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
ShowWindow(hewnd, SW_SHOWNORMAL);
UpdateWindow(hewnd);
DrawNetwork(hewnd);
}
////////////////////////////////////////////////////////////////////////////////
// MAIN WINDOW PROCEDURE //
////////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case ID_NETEDIT: {
HWND hewnd;
hewnd = initEditor(hwnd);
}
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// WIN MAIN PROCEDURE //
////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX MainClass, EditorClass;
HWND hwnd;
MSG Msg;
g_hInst = hInstance;
// Main Window Class registration
MainClass.cbSize = sizeof(WNDCLASSEX);
MainClass.style = NULL;
MainClass.lpfnWndProc = WndProc;
MainClass.cbClsExtra = 0;
MainClass.cbWndExtra = 0;
MainClass.hInstance = g_hInst;
MainClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
MainClass.hCursor = LoadCursor(NULL, IDC_ARROW);
MainClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
MainClass.lpszMenuName = MAKEINTRESOURCE(IDM_MENU1);
MainClass.lpszClassName = g_szMainClassName;
MainClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&MainClass))
{
MessageBox(0, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
// Editor window registration
EditorClass.cbSize = sizeof(WNDCLASSEX);
EditorClass.style = NULL;
EditorClass.lpfnWndProc = EditProc;
EditorClass.cbClsExtra = 0;
EditorClass.cbWndExtra = 0;
EditorClass.hInstance = g_hEdInst;
EditorClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
EditorClass.hCursor = LoadCursor(NULL, IDC_ARROW);
EditorClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
EditorClass.lpszMenuName = NULL;
EditorClass.lpszClassName = g_szEditClassName;
EditorClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&EditorClass))
{
MessageBox(0, "Window Registration Failed!", "Error!",
MB_OK);
return 0;
}
// Window creation
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szMainClassName,
"Main Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
NULL, NULL, g_hInst, NULL);
if(hwnd == NULL)
{
MessageBox(0, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Main loop
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
This is going to be an editor for my project, a neural network editor. But i have to learn how to draw them on screen before i start with the serious stuff... Should i post also the resource file?
i guess not!
-
Jul 9th, 2001, 05:35 AM
#4
BTW: How do i make it look nicer? i mean whith indentatoin and stuff like that... thanks alot!
-
Jul 9th, 2001, 05:56 AM
#5
Monday Morning Lunatic
Use [code][/code] tags. Or you could use php tags for colour coding, but then you get an extra newline at the top.
PS: you shouldn't need to #include <wingdi.h>
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
-
Jul 9th, 2001, 12:33 PM
#6
PowerPoster
what is rong with that source code
What is rong with that code. I did not find any mistake mistake but also I am not at home so I have not tested it. When I test, I will just see what is rong with that.
PS: Why do you need the resource files except the resource menu file.
-
Jul 17th, 2001, 07:29 AM
#7
Yeah, i know there's nothing wrong whith that code, but it simply doesn't work. I'm getting a null pointer at
hDispl = BeginPaint(hwnd, lpPaint);
so the problem should be located here. Then i never get the circle painted on the window.
Any guesses?
Also, i #include wingdi.h for the graphics functions.
thanks for your help
-
Jul 17th, 2001, 07:51 AM
#8
Monday Morning Lunatic
You shouldn't need wingdi.h as well, just use windows.h
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
-
Jul 17th, 2001, 11:06 AM
#9
Frenzied Member
Well I got it to work, but for some reason the hDC in your paint code is not returning to correct hDC. But if I directly ask Windows to return it I get it to work.
Example:
PHP Code:
draw_cell(GetDC(hwnd), 1, 1);
Also you have done a bad thing according to MSDN. You have Parksie to thank for this
PHP Code:
while(GetMessage(&Msg, NULL, 0, 0)) //THIS IS A NO NO
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
//CHANGE TO
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Jul 17th, 2001, 04:22 PM
#10
-
Jul 21st, 2001, 02:20 AM
#11
PowerPoster
I recommend "online msdn" if you dont have msdn on a cd. It is the best refrence for Windows API.
Here is the main page:
http://msdn.microsoft.com/library/
-
Jul 21st, 2001, 02:33 AM
#12
Code:
if(PeekMessage(&msg, my_hWnd, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
This way, your application doesnt wait for windows to get a message before moving on.
Z.
-
Jul 22nd, 2001, 10:40 AM
#13
Thank you guys. I'll take that into account

bye
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
|