Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 9th, 2006, 4:51 PM   #1
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
win32 api

I'm just getting started with the Win32 API, and I can't seem to display some simple text when the user clicks the middle button of the mouse. It doesn't show any error message--it just closes, as if I had closed it. Could anyone help? I'm sure it's something simple...

#include <windows.h>

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_LBUTTONDOWN:
        {
            char szFileName[MAX_PATH];
            HINSTANCE hInstance = GetModuleHandle(NULL);

            GetModuleFileName(hInstance, szFileName, MAX_PATH);
            MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);
        }
        break;
        case WM_MBUTTONDOWN:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);

            TextOut(hdc,10,10,"Hello World",11);

            EndPaint(hwnd, &ps);
        }
        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;

    //Step 1: Registering the Window Class
    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))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        0,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
Writlaus is offline   Reply With Quote
Old Mar 9th, 2006, 5:23 PM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
The case statement for 'WM_MBUTTONDOWN' doesn't 'break', so it's falling through into the 'WM_CLOSE' case which closes the window.
Cache is offline   Reply With Quote
Old Mar 9th, 2006, 6:45 PM   #3
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
Also, MSDN specifies that:

Quote:
An application should not call BeginPaint except in response to a WM_PAINT message.
Cache is offline   Reply With Quote
Old Mar 10th, 2006, 1:23 AM   #4
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
Why thank you, that obviously helped a lot. I knew it was something simple
Writlaus is offline   Reply With Quote
Old Mar 10th, 2006, 2:12 AM   #5
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
Quote:
An application should not call BeginPaint except in response to a WM_PAINT message.
Does that mean it won't work if I call it when not responding to a WM_PAINT message? Because now my code won't close, but it also won't display the text.
Writlaus is offline   Reply With Quote
Old Mar 10th, 2006, 2:56 AM   #6
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
If you want to do it outside WM_PAINT and not use BeginPaint(), you could use:

hdc=GetDC(hwnd);

When the device context is no longer needed, make sure you call:

ReleaseDC(hwnd, hdc);
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Mar 10th, 2006, 3:01 AM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by Writlaus
Does that mean it won't work if I call it when not responding to a WM_PAINT message? Because now my code won't close, but it also won't display the text.
As I understand it, when a program receives a WM_PAINT message, there will be some defined region that has to be (re)painted. When a program receives other messages, such a region is not necessarily defined (it will normally be the whole screen or window, rather than a region to be repainted, but can also be related to some other settings that are unrelated to what the programmer is trying to do) so the effect of BeginPaint() or the code after it becomes unpredictable.

In particular, because BeginPaint() sets a clipping region (and invalidates background in some circumstances), it is possible that the code after it will not have intended effects (eg if it does things outside the clipping region).

As a general rule, when documentation for a library says "only do it this way", the reason is that the designers of that library found it impossible to ensure the function worked correctly if you ignore their statement. Library writers don't tend to like to admit defeat by adding such clauses and, when they do, there is usually a good reason.
grumpy is offline   Reply With Quote
Old Mar 10th, 2006, 4:11 PM   #8
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
Ah, all is clear (for now, heh). The text displayed alright. Thank you, very much.
Writlaus is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:19 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC