HUSTIS1403 2016-02-25 16:20 采纳率: 0%
浏览 1751

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 2016-02-26 01:28
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?