游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2733|回复: 2

窗口生成后就消失怎么办

[复制链接]

1

主题

1

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2014-11-25 19:26:12 | 显示全部楼层 |阅读模式
窗口生成后,立刻就消失,怎么解决啊
#include <Windows.h>

#include <iostream>

using namespace std;

const string ProgramTitle = "Hello Windows ";

//The window event callback function
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    string text = "Hello Windows!";
       
        switch ( message )
        {
        case WM_PAINT:
                {
                        //get the dimension of the window
                        RECT rt;
                        GetClientRect( hWnd, &rt );
                       
                        //start drawing on device context
                        PAINTSTRUCT ps;
                        HDC hdc = BeginPaint( hWnd, &ps );
                       
                        //draw some text
                        DrawText( hdc, text.c_str(), text.length(), &rt, DT_CENTER );
                       
                        //draw 1000 random pixels
                        for( int n = 0; n < 3000; n++ )
                        {
                                int x = rand() % ( rt.right - rt.left );
                                int y = rand() % ( rt.bottom - rt.top );
                                COLORREF c = RGB( rand() % 256, rand() % 256, rand() % 256 );
                                SetPixel( hdc, x, y, c);
                        }
                       
                       
                        EndPaint( hWnd, &ps );
                }
                        break;
                       
        case WM_DESTROY:
                PostQuitMessage( 0 );
                break;
        }
        return DefWindowProc( hWnd, message, wParam, lParam );
}

//Helper function to set up the window properties
ATOM MyRegisterClass( HINSTANCE hInstance)
{
        //set the window's properties
        WNDCLASSEX wc;
        wc.cbSize = sizeof( WNDCLASSEX );
        wc.style = CS_HREDRAW|CS_VREDRAW;
        wc.lpfnWndProc = (WNDPROC) WinProc;
        wc.cbWndExtra = 0;
        wc.cbClsExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon =  NULL;
        wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = ( HBRUSH )GetStockObject( BLACK_BRUSH );
        wc.lpszClassName = ProgramTitle.c_str();
        wc.lpszMenuName = NULL;
        wc.hIconSm = NULL;
       
        return RegisterClassEx( &wc );
}

//Helper function to reate the window and refresh it
bool InitInstance( HINSTANCE hInstance, int nCmdShow )
{
        HWND hWnd = CreateWindow(
                ProgramTitle.c_str(), //window class
                ProgramTitle.c_str(), //titler bar                       
                WS_OVERLAPPEDWINDOW,  //window style
                CW_USEDEFAULT, CW_USEDEFAULT, //position of the window
                640, 480,             //dimensions of the window
                NULL,                 //parent window ( not used )
                NULL,                 //menu ( not used )
                hInstance,            //application instance
                NULL );               //window parameter ( not used )
       
        //was there an error creating the window ?
        if( hWnd == 0 ) return 0;
       
        ShowWindow( hWnd, nCmdShow );
        UpdateWindow( hWnd );
       
        return 1;
}

//Entry point for the Window program
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
        //create the window
    MyRegisterClass( hInstance );
        if( InitInstance( hInstance, nCmdShow ) ) return 0;
       
        //main message loop
        MSG msg;
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
        }
        return msg.wParam;
       
}

0

主题

18

帖子

308

积分

中级会员

Rank: 3Rank: 3

积分
308
发表于 2014-11-26 09:40:24 | 显示全部楼层
if( !InitInstance( hInstance, nCmdShow ) ) return 0;

34

主题

844

帖子

1755

积分

金牌会员

Rank: 6Rank: 6

积分
1755
发表于 2014-11-26 19:10:17 | 显示全部楼层
楼上好眼力!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-25 23:04

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表