2 hustis1403 HUSTIS1403 于 2016.02.26 00:20 提问

windows程序设计 为什么窗口一开始空白,需要最小化或者改变大小才显示

/*-----------------------------------------
SINEWAVE.C -- Sine Wave Using Polyline
(c) Charles Petzold, 1998
-----------------------------------------*/

#include
#include

#define NUM 1000
#define TWOPI (2 * 3.14159)

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("SineWave");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;

if (!RegisterClass(&wndclass))
{
    MessageBox(NULL, TEXT("Program requires Windows NT!"),
        szAppName, MB_ICONERROR);
    return 0;
}

hwnd = CreateWindow(szAppName, TEXT("Sine Wave Using Polyline"),
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT,
    CW_USEDEFAULT, CW_USEDEFAULT,
    NULL, NULL, hInstance, NULL);

ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxClient, cyClient;
HDC hdc;
int i;
PAINTSTRUCT ps;
POINT apt[NUM];

switch (message)
{
case WM_SIZE:
    cxClient = LOWORD(lParam);
    cyClient = HIWORD(lParam);
    return 0;

case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps);

    MoveToEx(hdc, 0, cyClient / 2, NULL);
    LineTo(hdc, cxClient, cyClient / 2);

    for (i = 0; i < NUM; i++)
    {
        apt[i].x = i * cxClient / NUM;
        apt[i].y = (int)(cyClient / 2 * (1 - sin(TWOPI * i / NUM)));
    }

    Polyline(hdc, apt, NUM);
    return 0;

case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);

}

而一般的程序都是直接显示,这个程序哪儿不同了?
下面是前面写过的一个。
#include
#include"SYSMETS.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static WCHAR szAppName[] = L"SYSMET1";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = nullptr;
wndclass.lpszClassName = szAppName;

if (!RegisterClass(&wndclass))
{
    MessageBox(NULL, L"需要win32操作系统。", L"error", MB_OKCANCEL);
    return  0;
}
hwnd = CreateWindow(szAppName, L"SYSMETS", WS_OVERLAPPEDWINDOW|WS_VSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);

ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cyChar, cxCaps,cyClient,iVscrollPos;
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
static wchar_t szBuffer[10];
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
cxCaps = int(1.5*double(cxChar));
ReleaseDC(hwnd, hdc);

    SetScrollRange(hwnd, SB_VERT, 0, NUMLINES - 1, FALSE);
    SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);
    return 0;

case WM_SIZE:
    cyClient = HIWORD(lParam);
    return 0;

case WM_VSCROLL:
    switch (LOWORD(wParam))
    {
    case SB_LINEUP:
        iVscrollPos -= 1;break;
        break;
    case SB_LINEDOWN:
        iVscrollPos += 1;break;
    case SB_PAGEUP:
        iVscrollPos -= cyClient / cyChar;break;
    case SB_PAGEDOWN:
        iVscrollPos += cyClient / cyChar;break;
    case SB_THUMBPOSITION:
        iVscrollPos = HIWORD(wParam);break;
    default:break;
    }

    if (iVscrollPos < 0)iVscrollPos = 0;
    else if (iVscrollPos>NUMLINES - 1)iVscrollPos = NUMLINES - 1;
    if (iVscrollPos != GetScrollPos(hwnd, SB_VERT))
    {
        SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);
        InvalidateRect(hwnd, NULL, TRUE); 
    }

case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps);
    for (int i = 0;i != NUMLINES;++i)
    {
        int y = cyChar*(i - iVscrollPos);
        TextOut(hdc, 0, y, sysmetrics[i].szLabel, lstrlen(sysmetrics[i].szLabel));
        TextOut(hdc, 22 * cxCaps, y, sysmetrics[i].szDesc, lstrlen(sysmetrics[i].szDesc));
        SetTextAlign(hdc, TA_RIGHT | TA_TOP);
        TextOut(hdc, 22 * cxCaps + 40 * cxChar, y, szBuffer, wsprintf(szBuffer, L"%5d", GetSystemMetrics(sysmetrics[i].iIndex)));
        SetTextAlign(hdc, TA_LEFT | TA_TOP);
    }
    EndPaint(hwnd, &ps);
    return 0;
case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);

}
具体什么原因 求大神解答,自己从WM_SIZE WM_CREATE UpdateWindow各个方面去思考都想不通。

1个回答

qq_34047461
qq_34047461   2016.02.26 09:28

本人感觉应该是没有刷新窗口的原因,在最小化然后最大化之后窗口重新刷新了,所以这个时候才显示了。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!