Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 19
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
All today's birthdays', congrats!
ramses (16)
Arcstarter (31)
Zak (15)


Next birthdays
05/12 Colin 99 (53)
05/14 hvguy (41)
05/14 thehappyelectron (14)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

windows application programming

1 2 
Move Thread LAN_403
Linas
Fri Mar 08 2013, 07:51PM Print
Linas Registered Member #1143 Joined: Sun Nov 25 2007, 04:55PM
Location: Vilnius, Lithuania
Posts: 721
Hello, does any one here know more about windows application programming ?
I am making multi-threaded fractal painting problem, all hard job is done, all is left is fractal painting in window as fast as i can.

I have function which handles my window, but i don't know how to pass message so it will be update each time every time.
( i want to make something like animation, zooming into Julia set or other fractal)

code for window handler is here:

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

switch(msg)
{
case WM_LBUTTONDOWN:
{
zoom+=5;
sideJob();
g_hbmBall = CreateBitmap( 400, 400, 1,32,wBits);
BITMAP bm;
PAINTSTRUCT ps;

HDC hdc = BeginPaint(hwnd, &ps);

HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmBall);

GetObject(g_hbmBall, sizeof(bm), &bm);

BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;

}
more details here:
Link2
Back to top
Daedronus
Sat Mar 09 2013, 02:21AM
Daedronus Registered Member #2329 Joined: Tue Sept 01 2009, 08:25AM
Location:
Posts: 370
The short answer is to use a timer and process WM_TIMER messages.
And, don't bother repainting on anything else then WM_PAIN and use invalidaterect when you decide you want something repainted.

Long answer you need to use a custom message loop using PeekMessage instead of GetMessage. This is non blocking and it will allow you to draw the animation while not processing messages. But the draw function better be fast.
Back to top
Linas
Sat Mar 09 2013, 07:25AM
Linas Registered Member #1143 Joined: Sun Nov 25 2007, 04:55PM
Location: Vilnius, Lithuania
Posts: 721
Daedronus wrote ...

The short answer is to use a timer and process WM_TIMER messages.
And, don't bother repainting on anything else then WM_PAIN and use invalidaterect when you decide you want something repainted.

Long answer you need to use a custom message loop using PeekMessage instead of GetMessage. This is non blocking and it will allow you to draw the animation while not processing messages. But the draw function better be fast.
WT_timer don't do anything, or i don't wait long enough. do you know syntax how to send message to winpros function, since i can update window when i finish calculation.
Glad to see that some one understands this stuff, since i am quite new in here
Back to top
Daedronus
Sat Mar 09 2013, 11:00PM
Daedronus Registered Member #2329 Joined: Tue Sept 01 2009, 08:25AM
Location:
Posts: 370
You need to set-up a time before you receive WM_TIMER events
Link2

Use PostThreadMessage to send a windows message to the message loop from another thread.
Link2

If you want a custom message ID, use any value like this: WM_USER+x, for example:
#define WM_MY_CUSTOM_WND_MESSAGE (WM_USER +1)
#define WM_MY_SECOND_CUSTOM_WND_MESSAGE (WM_USER +2)
Back to top
Linas
Sun Mar 10 2013, 02:45PM
Linas Registered Member #1143 Joined: Sun Nov 25 2007, 04:55PM
Location: Vilnius, Lithuania
Posts: 721
Daedronus wrote ...

You need to set-up a time before you receive WM_TIMER events
Link2

Use PostThreadMessage to send a windows message to the message loop from another thread.
Link2

If you want a custom message ID, use any value like this: WM_USER+x, for example:
#define WM_MY_CUSTOM_WND_MESSAGE (WM_USER +1)
#define WM_MY_SECOND_CUSTOM_WND_MESSAGE (WM_USER +2)

I set up timer, but it looks i have wrong timer identifier, i set to 0,5s intervals, but it do nothing. (used random number for it, 101)
about custom message, i know how to create one, i just don't know how to send it, so it will be processed by switch (ii don't know syntax for it)
i can calculate fractal in 66fps, and BitBlt() works quite fast for painting, so it should looks like animation, if only i know how to send custom message :( )

if i set messages.message= WM_MY_SECOND_CUSTOM_WND_MESSAGE; in main loop for message dispatching, it paint it only one time, and i can't turn it off, since i jam message sending in my opinion, i need to use if() to check if message is quit
Back to top
Daedronus
Sun Mar 10 2013, 08:37PM
Daedronus Registered Member #2329 Joined: Tue Sept 01 2009, 08:25AM
Location:
Posts: 370
If you can do 66fps you should use what is explained here:
Link2
Even if it's not DirectX related, the message handling is similar.
Back to top
Linas
Mon Mar 11 2013, 08:50AM
Linas Registered Member #1143 Joined: Sun Nov 25 2007, 04:55PM
Location: Vilnius, Lithuania
Posts: 721
Daedronus wrote ...

If you can do 66fps you should use what is explained here:
Link2
Even if it's not DirectX related, the message handling is similar.
Thanks for the link, find some usefull info aout some parts of my program. But rapid painting is the flaw.
I did experiment, simply generating fractal in main, and painting as bmp without any window, and it paints single picture, than hangs and do nothing, and at same second get message from windows that my app is stopped working.
So my approach it total flaw
for painting code looks like this:
zoom*=2;
    sideJob2(0,0,400,400); //refill matrix with new zoom
    HBITMAP cross = (HBITMAP)CreateBitmap( 401, 400, 1,32,wBits); // make bmp from matrix
    SelectObject(hdc, cross);
    HDC hdc_x = GetDC(HWND_DESKTOP);
    BitBlt(hdc_x,100,100,400,400,hdc,0,0,SRCCOPY); //paint bmp
    ReleaseDC(HWND_DESKTOP,hdc_x);
can some know better idea how to do this ?

Here is what i did so far, if you take the bottom edge with mouse and try to resize, (holding bottom edge of window, and collapsing upwards, and then expand again) you will see that fractal is repainted , but not all, just that part which was covered.
]program.zip[/file]
if some one is interested i can give source file
Back to top
Daedronus
Mon Mar 11 2013, 09:26AM
Daedronus Registered Member #2329 Joined: Tue Sept 01 2009, 08:25AM
Location:
Posts: 370
one thing you should know about SelectObject
It returns the previously selected object, you have to save this and select it back when you are done, or you will have a memory/object leak.
GDI objects are global resources and if you consume them windows in general (not just your application) will start to misbehave.

If you don't mind post the code here, someone might be able to provide more help.
Back to top
Linas
Mon Mar 11 2013, 10:37AM
Linas Registered Member #1143 Joined: Sun Nov 25 2007, 04:55PM
Location: Vilnius, Lithuania
Posts: 721
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <winuser.h>
#include <D3d9types.h>//3D colour
#include <windowsx.h>
using namespace std;
#define WM_MY_SECOND_CUSTOM_WND_MESSAGE (WM_USER +2)
#define IDI_MYICON 1000

HBITMAP g_hbmBall = NULL;
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);



/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";
int32_t wBits[160000] = {0};
double long zoom=0.1;

       BITMAP bm;
       PAINTSTRUCT ps;
       HWND hwnd;
       HDC hdc = BeginPaint(hwnd, &ps);
            
void sideJob ()
{
     long h=0;
    float tmp1, tmp2;
    float num_real, num_img;
    float radius;
    int i =0;
    int x=0,y=0;
    for (y=0; y<400; y++)
    {
        for (x=0; x<400; x++)
        {
            num_real = y - 200;
            num_real = num_real / zoom;
            num_img = x - 200;
            num_img = num_img / zoom;
            i=0;
            radius = 0;
            while ((i<256-1) && (radius < 8))
            {
                tmp1 = num_real * num_real;
                tmp2 = num_img * num_img;
                num_img = 2*num_real*num_img - 0.01;
                num_real = tmp1 - tmp2 + 0.285;
                radius = tmp1 + tmp2;
                i++;
            }
            wBits[h]=D3DCOLOR_ARGB(128,12*i,18*i,10*i);
            h++;
        }
    }

}

void sideJob2(void)
{
 long h=0;
  double long x,xx,y,cx,cy;
  int i=0;
  double long  offset_xx= 1.5827773825793396;//+0.000181L;
  double long  offset_yy= 0.0000000181;// 0.000000000000181L;
  for(int hy=0;hy<=400;hy++)
  {
    for(int hx=0;hx<=400;hx++)
    {
      cx = (((double long)hx)/((double long)400)-0.5)/zoom;//*3.0-0.7;
      cy = (((double long)hy)/((double long)400)-0.5)/zoom;//*3.0;
      cx -=offset_xx;
      cy -=offset_yy;
      x = 0.0;
      y = 0.0;
      for(i=1;i<256;i++)
        {
           xx = x*x-y*y+cx;
           y = 2.0*x*y+cy;
          x = xx;
          if (x*x+y*y>100.0)
           break;
        }
        wBits[h]=D3DCOLOR_ARGB(128,12*i,18*i,10*i);
        h++;
    }
    }
}

UINT msg;
int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{

sideJob2();
                 /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */
    UINT uResult;
    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;
      uResult=SetTimer(hwnd,101,1000, (TIMERPROC) NULL);  

        hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Julia Set Fraktalų skaičiavimas",       
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           400,                 /* The programs width */
           400,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
           ShowWindow (hwnd, nFunsterStil);
  
    while(TRUE)
    {
        // Check to see if any messages are waiting in the queue
        while(PeekMessage(&messages, NULL, 0, 0, PM_REMOVE))
        {
            // translate keystroke messages into the right format
            TranslateMessage(&messages);
            // send the message to the WindowProc function
            DispatchMessage(&messages);
        }
        if(messages.message == WM_QUIT)
            break;
    }
    return messages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_PAINT:
        {
            zoom*=1.1;
            sideJob2();
            g_hbmBall = CreateBitmap( 401, 400, 1,32,wBits);
            HDC hdc = BeginPaint(hwnd, &ps);
            
            HDC hdcMem = CreateCompatibleDC(hdc);
            HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmBall);
            
            GetObject(g_hbmBall, sizeof(bm), &bm);
            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
            BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
   
            SelectObject(hdcMem, hbmOld);
            DeleteDC(hdcMem);
            EndPaint(hwnd, &ps);
        }
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
             PostQuitMessage(0);
        break;
        default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
This is not multi-threaded version, but use only single thread by default. when i get it working, i will use POSIX library for multi thread.
Back to top
Daedronus
Mon Mar 11 2013, 11:23AM
Daedronus Registered Member #2329 Joined: Tue Sept 01 2009, 08:25AM
Location:
Posts: 370
// here, enjoy


#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <winuser.h>
#include <D3d9types.h>//3D colour
#include <windowsx.h>
using namespace std;
#define WM_MY_SECOND_CUSTOM_WND_MESSAGE (WM_USER +2)
#define IDI_MYICON 1000

HBITMAP g_hbmBall = NULL;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);



/* Make the class name into a global variable */
WCHAR szClassName[ ] = L"WindowsApp";
unsigned int wBits[160000] = {0};
double long zoom=1.1;

BITMAP bm;
PAINTSTRUCT ps;
HWND hwnd;
HDC hdc = BeginPaint(hwnd, &ps);

void sideJob ()
{
long h=0;
float tmp1, tmp2;
float num_real, num_img;
float radius;
int i =0;
int x=0,y=0;
for (y=0; y<400; y++)
{
for (x=0; x<400; x++)
{
num_real = y - 200;
num_real = num_real / zoom;
num_img = x - 200;
num_img = num_img / zoom;
i=0;
radius = 0;
while ((i<256-1) && (radius < 8))
{
tmp1 = num_real * num_real;
tmp2 = num_img * num_img;
num_img = 2*num_real*num_img - 0.01;
num_real = tmp1 - tmp2 + 0.285;
radius = tmp1 + tmp2;
i++;
}
wBits[h]=D3DCOLOR_ARGB(128,12*i,18*i,10*i);
h++;
}
}

}

void sideJob2(void)
{
long h=0;
double long x,xx,y,cx,cy;
int i=0;
double long offset_xx= 1.5827773825793396;//+0.000181L;
double long offset_yy= 0.0000000181;// 0.000000000000181L;
for(int hy=0;hy<=400;hy++)
{
for(int hx=0;hx<=400;hx++)
{
cx = (((double long)hx)/((double long)400)-0.5)/zoom;//*3.0-0.7;
cy = (((double long)hy)/((double long)400)-0.5)/zoom;//*3.0;
cx -=offset_xx;
cy -=offset_yy;
x = 0.0;
y = 0.0;
for(i=1;i<256;i++)
{
xx = x*x-y*y+cx;
y = 2.0*x*y+cy;
x = xx;
if (x*x+y*y>100.0)
break;
}
wBits[h]=D3DCOLOR_ARGB(128,12*i,18*i,10*i);
h++;
}
}
}

UINT msg;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{

sideJob2();
/* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
UINT uResult;
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
uResult=SetTimer(hwnd,101,1000, (TIMERPROC) NULL);

hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
L"Julia Set Fraktalų skaičiavimas",
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
400, /* The programs width */
400, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
ShowWindow (hwnd, nFunsterStil);

while(TRUE)
{
// Check to see if any messages are waiting in the queue
while(PeekMessage(&messages, NULL, 0, 0, PM_REMOVE))
{
// translate keystroke messages into the right format
TranslateMessage(&messages);
// send the message to the WindowProc function
DispatchMessage(&messages);
}
if(messages.message == WM_QUIT)
break;

//-----------------
zoom*=1.1;
sideJob2();

g_hbmBall = CreateBitmap( 401, 400, 1,32,wBits);
HDC hdc = GetWindowDC(hwnd);
HDC hdcMem = CreateCompatibleDC(hdc);

HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmBall);
GetObject(g_hbmBall, sizeof(bm), &bm);
// FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);


DeleteDC(hdcMem);
ReleaseDC(hwnd, hdc);
DeleteObject(g_hbmBall);
//-----------------
}
return messages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
/*
case WM_PAINT:
{
// we don't really do anything here
break;
}
*/
case WM_CLOSE:
{
DestroyWindow(hwnd);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
{
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}
return 1;
}
Back to top
1 2 

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.