-6

i'm just start to learn C++ for Win and now i at the three part of tut . so i have this code :

#include <Windows.h>
#include <stdio.h>
#define WIN_WIDTH 300
#define WIN_HEIGHT 200
#define class_name L"HDC"

LRESULT CALLBACK WinProc(HWND hwnd , UINT Message ,WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hinstance ,HINSTANCE hPrev,PSTR cmdline ,int ishow)
{
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wndclassex ={0};
    wndclassex.style = CS_HREDRAW | CS_VREDRAW;
    wndclassex.cbSize = sizeof(wndclassex);
    wndclassex.lpfnWndProc;
    wndclassex.hInstance = hinstance;
    wndclassex.lpszClassName = class_name;
    wndclassex.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    RegisterClassEx(&wndclassex);

    hwnd = CreateWindow(class_name,L"My Second WinDow Application",
                        WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
                        WIN_WIDTH,WIN_HEIGHT,NULL,NULL,hinstance,NULL);
    if(!hwnd)
        return EXIT_FAILURE;
    HDC hdc = GetDC(hwnd);
    if(!hdc)
        return EXIT_FAILURE;
    ShowWindow(hwnd,ishow);
    UpdateWindow(hwnd);
    RECT shen;
    GetClientRect(hwnd,&shen);
    FillRect(hdc,&shen,(HBRUSH)GetStockObject(WHITE_BRUSH));

    while(1)
    {
        if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE))
        {
            if(msg.message==WM_QUIT)
                break;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    ReleaseDC(hwnd,hdc);
    UnregisterClass(class_name,hinstance);
    return msg.wParam;
}

LRESULT CALLBACK DefWinProc(HWND hwnd,UINT Message,WPARAM wParam ,LPARAM lParam)
{
    switch(Message)
    {
        case WM_DESTROY:
        case WM_CLOSE:
            PostQuitMessage(0);
            return 0;

    }
    return DefWinProc(hwnd,Message,wParam,lParam);
}

but i'm not lucky the system notice that :

'device context.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'device context.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Symbols loaded (source information stripped).
First-chance exception at 0x00000000 in device context.exe: 0xC0000005: Access violation.
First-chance exception at 0x00000000 in device context.exe: 0xC0000005: Access violation.
First-chance exception at 0x00000000 in device context.exe: 0xC0000005: Access violation.
The program '[15148] device context.exe: Native' has exited with code 1 (0x1).

Please Somebody know that error just help me fix it , I'm just wanna lean window Programming . Thank you first.

4

3 回答 3

5
wndclassex.lpfnWndProc;

您确实意识到这完全没有任何作用,并且wndclassex.lpfnWndProc指针仍然为 NULL,对吗?

于 2013-08-21T20:05:06.263 回答
2

这只是一个建议,但请考虑一下:

wndclassex.lpfnWndProc;

有些东西告诉我没有为您的注册功能分配窗口过程。(或做任何其他远程建设性的事情)。

尝试:

wndclassex.lpfnWndProc = WndProc;
于 2013-08-21T20:06:35.113 回答
1

在这种情况下,您的问题很可能是您在此处为窗口过程指定了一个空指针:

wndclassex.lpfnWndProc;

这很糟糕,因为您实际上需要一个窗口过程才能工作,至少您必须响应 WM_CREATE 消息以确保实际创建了窗口(链接

然后,您尝试重新定义 DefWinProc,它是在 Windows 库之一中定义的函数,它只为您不想处理的消息实现默认行为。

您需要做的第一件事是为窗口函数找到另一个名称,我看到您定义了一个名为 WinProc 的函数原型,取而代之的是,然后将我之前向您展示的行更改为

wndclassex.lpfnWndProc = WinProc;

它应该工作。

于 2013-08-21T20:08:42.473 回答