windows程序設計 學習記錄1

剛開始的入門程序,在窗口中輸出Hello windows 98!,窗口名是hellomsg。

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    MessageBox(NULL,TEXT("Hello Windows 98!"), TEXT("HelloMsg") , 0 );
    return 0;
}

//

調用幾個API函數,輸出屏幕的像素。

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)
{
    TCHAR szBuffer [1024];
    va_list pArgList;

    va_start (pArgList, szFormat);

    _vsntprintf (szBuffer, sizeof(szBuffer)/sizeof(TCHAR), szFormat, pArgList );

    va_end (pArgList);

    return MessageBox(NULL, szBuffer, szCaption, 0);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   PSTR szCmdLine, int iCmdShow)
{
    int cxScreen, cyScreen;

    cxScreen = GetSystemMetrics(SM_CXSCREEN);
    cyScreen = GetSystemMetrics(SM_CYSCREEN);

    MessageBoxPrintf(TEXT("ScrnSize"),
                     TEXT("The screen is %i pixels wide by %i pixels high."),
                     cxScreen, cyScreen);
    return 0;
}


//

用createwindow創建一個多功能的窗口,用消息隊列處理從交互中獲得的信息。

#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
static TCHAR szAppName[]= TEXT ("HelloWin");

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG msg;            /* Here messages to the application are saved */
    WNDCLASS wndclass;        /* Data structure for the windowclass */

    /* The Window structure */
    wndclass.hInstance = hInstance;
    wndclass.lpszClassName = szAppName;
    wndclass.lpfnWndProc = WndProc;      /* This function is called by windows */
    wndclass.style = CS_HREDRAW | CS_VREDRAW;                 /* Catch double-clicks */
    //wndclass.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    //wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
    wndclass.lpszMenuName = NULL;                 /* No menu */
    wndclass.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wndclass.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClass (&wndclass)){
        MessageBox (NULL,TEXT("This program requires Windows NT!"),
                    szAppName,MB_ICONERROR);
        return 0;
    }

    /* The class is registered, let's create the program*/
    hwnd = CreateWindow (
                            /* Extended possibilites for variation */
           szAppName,         /* Classname */
           TEXT("The Hello Program"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           CW_USEDEFAULT,                 /* The programs width */
           CW_USEDEFAULT,                 /* and height in pixels */
           NULL,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, iCmdShow);
    UpdateWindow(hwnd);
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&msg, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&msg);
        /* Send message to WindowProcedure */
        DispatchMessage(&msg);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return msg.wParam;
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;

    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            PlaySound(TEXT("hellowin.wav"),NULL,SND_FILENAME | SND_ASYNC);
            return 0;

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

            GetClientRect(hwnd, &rect);

            DrawText(hdc, TEXT("Hello, Windows 98!"),-1,&rect,
                     DT_SINGLELINE | DT_CENTER | DT_VCENTER );

            EndPaint(hwnd, &ps);
            return 0;

        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            return 0;
        //default:                      /* for messages that we don't deal with */
          //  return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
}


//

各種函數的名稱,返回類型,初始化的變量還不懂,太多太亂。

發佈了45 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章