标题:初学win 32 窗体创建hwnd 遇到创建hwnd为空 请问什么原因
只看楼主
zxlovezxl
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-3-24
结帖率:0
已结贴  问题点数:20 回复次数:5 
初学win 32 窗体创建hwnd 遇到创建hwnd为空 请问什么原因
以下是 全部代码,建Win32 空项目,c.bmp文件一个
#include <windows.h>
#include <iostream>
#include <time.h>
using namespace std;
const string APPTITLE = "Game Loop";
HWND window;
HDC device;
bool gameover = false;
void DrawBitmap(char *filename, int x, int y)
{
    HBITMAP image = (HBITMAP)LoadImage(0, "c.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    BITMAP bm;
    GetObject(image, sizeof(BITMAP), &bm);
    HDC hdcImage = CreateCompatibleDC(device);
    SelectObject(hdcImage, image);
    BitBlt(device, x, y, bm.bmWidth, bm.bmHeight, hdcImage, 0, 0, SRCCOPY);
    DeleteDC(hdcImage);
    DeleteObject((HBITMAP)image);
}
bool Game_Init()
{
    srand(time(NULL));
    return 1;
}
void Game_Run()
{
    if (gameover == true) return;
    RECT rect;
    GetClientRect(window, &rect);
    int x = rand() % (rect.right - rect.left);
    int y = rand() % (rect.bottom - rect.top);
    DrawBitmap("c.bmp", x, y);
}
void Game_End()
{
    ReleaseDC(window, device);
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        gameover = true;
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.lpfnWndProc = (WNDPROC)WinProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = APPTITLE.c_str();
    wc.hIconSm = NULL;
    return RegisterClassEx(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    window = CreateWindow(APPTITLE.c_str(), APPTITLE.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
    if (window == 0) return 0;
    ShowWindow(window, nCmdShow);
    UpdateWindow(window);
    device = GetDC(window);
    return 1;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;
    MyRegisterClass(hInstance);
    if (!InitInstance(hInstance, nCmdShow)) return 0;
    if (!Game_Init()) return 0;
    while (!gameover)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        Game_Run();
    }
    Game_End();

    return msg.wParam;
}
请问为什么 不出结果,一闪而过,window为什么为空
搜索更多相关主题的帖子: include window false 项目 
2017-03-24 23:16
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:10 
Win32程序不建立InitInstance, 是在MainWin内CreateWindow,回调函数中处理WM_CREATE
2017-03-25 12:16
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:0 
说错了,是WinMain
2017-03-25 12:17
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:10 
你应该告诉别人 MyRegisterClass(hInstance); 执行失败

原因是 wc.style 未赋值。
2017-03-27 08:38
zxlovezxl
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-3-24
得分:0 
高手啊一语道破 谢谢,已解决
2017-03-27 11:55
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:0 
确实没仔细看
2017-03-27 13:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-475469-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014232 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved