Click here to Skip to main content
15,916,426 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: m_ctrEditLocation.ReplaceSel Using String Resource Pin
Gary R. Wheeler15-Sep-02 2:29
Gary R. Wheeler15-Sep-02 2:29 
GeneralOutputting Text into An Edit Control Pin
MJ3215-Sep-02 1:09
MJ3215-Sep-02 1:09 
GeneralRe: Outputting Text into An Edit Control Pin
Pavel Klocek15-Sep-02 1:12
Pavel Klocek15-Sep-02 1:12 
GeneralRe: Outputting Text into An Edit Control Pin
MJ3215-Sep-02 1:39
MJ3215-Sep-02 1:39 
GeneralOpen Text File into Hexadecimal Pin
mzakarni14-Sep-02 23:08
mzakarni14-Sep-02 23:08 
GeneralRe: Open Text File into Hexadecimal Pin
l a u r e n15-Sep-02 0:58
l a u r e n15-Sep-02 0:58 
GeneralRe: Open Text File into Hexadecimal Pin
mzakarni15-Sep-02 2:39
mzakarni15-Sep-02 2:39 
GeneralLogical bug in program Pin
axa14-Sep-02 22:20
axa14-Sep-02 22:20 
I've been injured my eyes trying to find the logical bug (since it is
not syntax or any other error that could VC report), so I'm asking you
if you could help me to find the bug.
Program is retyped from book and unfortunately and don't have the
original source code to compare it with.
Program should create main window and in it the other four child
windows and do some display in each of them except the main window.
But when I try to run the program, it only display the main window and
the client area, without child windows in it!
Just to say one more time, VC does not report any error or warning, so
it must be the logical bug in code.
Here's the source (I apologize for text length in mail):

#include <windows.h><br />
#include <math.h><br />
<br />
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);<br />
<br />
int cyChar;<br />
<br />
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, <br />
                                                 PSTR szCmdLine, int iCmdShow)<br />
{<br />
        static TCHAR szAppName[] = TEXT("Multi1");<br />
        HWND hwnd;<br />
        MSG msg;<br />
        WNDCLASS wc;<br />
<br />
        wc.style                                = CS_HREDRAW | CS_VREDRAW;<br />
        wc.lpfnWndProc          = WndProc;<br />
        wc.cbClsExtra           = 0;<br />
        wc.cbWndExtra           = 0;<br />
        wc.hInstance            = hInstance;<br />
        wc.hIcon                                = LoadIcon(NULL, IDI_APPLICATION);<br />
        wc.hCursor                      = LoadCursor(NULL, IDC_ARROW);<br />
        wc.hbrBackground        = (HBRUSH) GetStockObject(WHITE_BRUSH);<br />
        wc.lpszMenuName = NULL;<br />
        wc.lpszClassName        = szAppName;<br />
<br />
        if (!RegisterClass(&wc))<br />
        {<br />
                MessageBox(NULL, TEXT("This Program Requires Windows NT!"),<br />
                                          szAppName, MB_ICONERROR);<br />
                return 0;<br />
        }<br />
<br />
        hwnd = CreateWindow(szAppName, TEXT("Multitasking Demo"),<br />
                                                          WS_OVERLAPPEDWINDOW,<br />
                                                          CW_USEDEFAULT, CW_USEDEFAULT,<br />
                                                          CW_USEDEFAULT, CW_USEDEFAULT,<br />
                                                          NULL, NULL, hInstance, NULL);<br />
<br />
        ShowWindow(hwnd, iCmdShow);<br />
        UpdateWindow(hwnd);<br />
<br />
        while (GetMessage(&msg, NULL, 0, 0))<br />
        {<br />
                TranslateMessage(&msg);<br />
                DispatchMessage(&msg);<br />
        }<br />
        return msg.wParam;<br />
}<br />
<br />
int CheckBottom(HWND hwnd, int cyClient, int iLine)<br />
{<br />
        if (iLine * cyChar + cyChar > cyClient)<br />
        {<br />
                InvalidateRect(hwnd, NULL, TRUE);<br />
                UpdateWindow(hwnd);<br />
                iLine = 0;<br />
        }<br />
        return iLine;<br />
}<br />
<br />
/*---------------------------------------------------<br />
    Window 1: Display increasing sequence of number<br />
  ---------------------------------------------------*/<br />
LRESULT APIENTRY WndProc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
        static int iNum, iLine, cyClient;<br />
        HDC hdc;<br />
        TCHAR szBuffer[16];<br />
<br />
        switch (message)<br />
        {<br />
                case WM_SIZE:<br />
                        cyClient = HIWORD(lParam);<br />
                        return 0;<br />
<br />
                case WM_TIMER:<br />
                        if (iNum < 0)<br />
                                iNum = 0;<br />
<br />
                        iLine = CheckBottom(hwnd, cyClient, iLine);<br />
                        hdc = GetDC(hwnd);<br />
<br />
                        TextOut(hdc, 0, iLine * cyChar, szBuffer,<br />
                                          wsprintf(szBuffer, TEXT("%d"), iNum++));<br />
<br />
                        ReleaseDC(hwnd, hdc);<br />
                        iLine++;<br />
                        return 0;<br />
        }<br />
        return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
/*----------------------------------------------------------<br />
         Window 2: Display increasing sequence of prime numbers<br />
  ----------------------------------------------------------*/<br />
LRESULT APIENTRY WndProc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
        static int iNum = 1, iLine, cyClient;<br />
        HDC hdc;<br />
        int i, iSqrt;<br />
        TCHAR szBuffer[16];<br />
<br />
        switch (message)<br />
        {<br />
                case WM_SIZE:<br />
                        cyClient = HIWORD(lParam);<br />
                        return 0;<br />
<br />
                case WM_TIMER:<br />
                        do<br />
                        {<br />
                                if (++iNum < 0)<br />
                                        iNum = 0;<br />
<br />
                                iSqrt = (int) sqrt(iNum);<br />
                                for (i = 2; i <= iSqrt; i++)<br />
                                        if (iNum % i == 0)<br />
                                                break;<br />
                        }<br />
                        while (i <= iSqrt);<br />
<br />
                        iLine = CheckBottom(hwnd, cyClient, iLine);<br />
                        hdc = GetDC(hwnd);<br />
<br />
                        TextOut(hdc, 0, iLine * cyChar, szBuffer,<br />
                                          wsprintf(szBuffer, TEXT("%d"), iNum));<br />
<br />
                        ReleaseDC(hwnd, hdc);<br />
                        iLine++;<br />
                        return 0;<br />
        }<br />
        return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
/*--------------------------------------------------------------<br />
    Window 3: Display increasing sequence of Fibonacci numbers<br />
  --------------------------------------------------------------*/<br />
LRESULT APIENTRY WndProc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
        static int iNum = 0, iNext = 1, iLine, cyClient;<br />
        HDC hdc;<br />
        int iTemp;<br />
        TCHAR szBuffer[16];<br />
<br />
        switch (message)<br />
        {<br />
                case WM_SIZE:<br />
                        cyClient = HIWORD(lParam);<br />
                        return 0;<br />
<br />
                case WM_TIMER:<br />
                        if (iNum < 0)<br />
                        {<br />
                                iNum = 0;<br />
                                iNext = 1;<br />
                        }<br />
<br />
                        iLine = CheckBottom(hwnd, cyClient, iLine);<br />
                        hdc = GetDC(hwnd);<br />
<br />
                        TextOut(hdc, 0, iLine * cyChar, szBuffer,<br />
                                          wsprintf(szBuffer, TEXT("%d"), iNum));<br />
<br />
                        ReleaseDC(hwnd, hdc);<br />
                        iTemp = iNum;<br />
                        iNum = iNext;<br />
                        iNext += iTemp;<br />
                        iLine++;<br />
                        return 0;<br />
        }<br />
        return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
/*---------------------------------------------<br />
    Window 4: Display circles of random radii<br />
  ---------------------------------------------*/<br />
LRESULT APIENTRY WndProc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
        static int cxClient, cyClient;<br />
        HDC hdc;<br />
        int iDiameter;<br />
<br />
        switch (message)<br />
        {<br />
                case WM_SIZE:<br />
                        cxClient = LOWORD(lParam);<br />
                        cyClient = HIWORD(lParam);<br />
                        return 0;<br />
                <br />
                case WM_TIMER:<br />
                        InvalidateRect(hwnd, NULL, TRUE);<br />
                        UpdateWindow(hwnd);<br />
<br />
                        iDiameter = rand() % (max(1, min(cxClient, cyClient)));<br />
                        hdc = GetDC(hwnd);<br />
<br />
                        Ellipse(hdc, (cxClient - iDiameter) / 2,<br />
                                                         (cyClient - iDiameter) / 2,<br />
                                                         (cxClient + iDiameter) / 2,<br />
                                                         (cyClient + iDiameter) / 2);<br />
<br />
                        ReleaseDC(hwnd, hdc);<br />
                        return 0;<br />
        }<br />
        return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
/*---------------------------------------<br />
    Main Window to create child windows<br />
  ---------------------------------------*/<br />
LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
        static HWND hwndChild[4];<br />
        static TCHAR* szChildClass[] = { TEXT("Child1"), TEXT("Child2"),<br />
                                                                                          <br />
TEXT("Child3"),<br />
TEXT("Child4") };<br />
        static WNDPROC ChildProc[] = { WndProc1, WndProc2, WndProc3, WndProc4 };<br />
        HINSTANCE hInstance;<br />
        int i, cxClient, cyClient;<br />
        WNDCLASS wc;<br />
<br />
        switch (message)<br />
        {<br />
                case WM_CREATE:<br />
                        hInstance = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE);<br />
<br />
                        wc.style                                = CS_HREDRAW | CS_VREDRAW;<br />
                        wc.cbClsExtra           = 0;<br />
                        wc.cbWndExtra           = 0;<br />
                        wc.hInstance            = hInstance;<br />
                        wc.hIcon                                = NULL;<br />
                        wc.hCursor                      = LoadCursor(NULL, IDC_ARROW);<br />
                        wc.hbrBackground        = (HBRUSH) GetStockObject(WHITE_BRUSH);<br />
                        wc.lpszMenuName = NULL;<br />
<br />
                        for (i = 0; i < 4; i++)<br />
                        {<br />
                                wc.lpfnWndProc          = ChildProc[i];<br />
                                wc.lpszClassName        = szChildClass[i];<br />
<br />
                                RegisterClass(&wc);<br />
<br />
                                hwndChild[i] = CreateWindow(szChildClass[i], NULL,<br />
                                                                                          <br />
WS_CHILDWINDOW |<br />
WS_BORDER | WS_VISIBLE,<br />
                                                                                          <br />
0, 0, 0, 0,<br />
                                                                                          <br />
hwnd, (HMENU) i,<br />
hInstance, NULL);<br />
                        }<br />
<br />
                        cyChar = HIWORD(GetDialogBaseUnits());<br />
                        SetTimer(hwnd, 1, 10, NULL);<br />
                        return 0;<br />
<br />
                case WM_SIZE:<br />
                        cxClient = LOWORD(lParam);<br />
                        cyClient = HIWORD(lParam);<br />
<br />
                        for (i = 0; i < 4; i++);<br />
                                MoveWindow(hwndChild[i], (i % 2) * cxClient / 2,<br />
                                                                                          <br />
(i > 1) * cyClient / 2,<br />
                                                                                          <br />
cxClient / 2, cyClient / 2,<br />
TRUE);<br />
                        return 0;<br />
<br />
                case WM_TIMER:<br />
                        for (i = 0; i < 4; i++)<br />
                                SendMessage(hwndChild[i], WM_TIMER, wParam, lParam);<br />
                        return 0;<br />
<br />
                case WM_CHAR:<br />
                        if (wParam == '\x1B')<br />
                                DestroyWindow(hwnd);<br />
                        return 0;<br />
<br />
                case WM_DESTROY:<br />
                        KillTimer(hwnd, 1);<br />
                        PostQuitMessage(0);<br />
                        return 0;<br />
        }<br />
        return DefWindowProc(hwnd, message, wParam, lParam);<br />
}


Help in any form is welcome Smile | :)
Thanks
GeneralRe: Logical bug in program Pin
Hans Ruck15-Sep-02 5:54
Hans Ruck15-Sep-02 5:54 
GeneralRe: Logical bug in program Pin
axa15-Sep-02 10:44
axa15-Sep-02 10:44 
QuestionCan't a message be peeked in a thread? Pin
Chen Jiadong14-Sep-02 22:04
Chen Jiadong14-Sep-02 22:04 
AnswerRe: Can't a message be peeked in a thread? Pin
Neville Franks14-Sep-02 22:52
Neville Franks14-Sep-02 22:52 
GeneralRe: Can't a message be peeked in a thread? Pin
Chen Jiadong15-Sep-02 1:55
Chen Jiadong15-Sep-02 1:55 
GeneralRe: Can't a message be peeked in a thread? Pin
Neville Franks15-Sep-02 2:16
Neville Franks15-Sep-02 2:16 
GeneralRe: Can't a message be peeked in a thread? Pin
Chen Jiadong15-Sep-02 4:24
Chen Jiadong15-Sep-02 4:24 
QuestionHow to print > 1 copies faster? Pin
Hiusing14-Sep-02 17:22
Hiusing14-Sep-02 17:22 
AnswerRe: How to print > 1 copies faster? Pin
Mike Nordell16-Sep-02 0:37
Mike Nordell16-Sep-02 0:37 
GeneralSeparate COLORREF Pin
Steve L.14-Sep-02 13:07
Steve L.14-Sep-02 13:07 
GeneralRe: Separate COLORREF Pin
Piotr Mizera14-Sep-02 13:35
Piotr Mizera14-Sep-02 13:35 
GeneralRe: Separate COLORREF Pin
Steve L.14-Sep-02 13:42
Steve L.14-Sep-02 13:42 
QuestionVS.NET: Compartible with MFC6? Pin
Philip Patrick14-Sep-02 12:47
professionalPhilip Patrick14-Sep-02 12:47 
AnswerRe: VS.NET: Compartible with MFC6? Pin
Anders Molin14-Sep-02 13:10
professionalAnders Molin14-Sep-02 13:10 
AnswerRe: VS.NET: Compartible with MFC6? Pin
Michael Dunn14-Sep-02 15:23
sitebuilderMichael Dunn14-Sep-02 15:23 
GeneralRe: VS.NET: Compartible with MFC6? Pin
Mustafa Demirhan15-Sep-02 21:01
Mustafa Demirhan15-Sep-02 21:01 
GeneralRe: VS.NET: Compartible with MFC6? Pin
Michael Dunn16-Sep-02 3:48
sitebuilderMichael Dunn16-Sep-02 3:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.