Category Archives: 计算机系统编程

System Programming

Windows程序设计之控制台程序的定时器使用方法

一、函数原型

VOID CALLBACK TimerProc(
  HWND hwnd,         // handle to window
  UINT uMsg,         // WM_TIMER message
  UINT_PTR idEvent,  // timer identifier
  DWORD dwTime       // current system time
);

UINT_PTR SetTimer(
  HWND hWnd,              // handle to window
  UINT_PTR nIDEvent,      // timer identifier
  UINT uElapse,           // time-out value
  TIMERPROC lpTimerFunc   // timer procedure
);

Continue reading

Windows程序设计之创建、打开、读写文件方法介绍

一、创建或打开文件

HANDLE CreateFile(
  LPCTSTR lpFileName,                         // file name
  DWORD dwDesiredAccess,                      // access mode
  DWORD dwShareMode,                          // share mode
  LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
  DWORD dwCreationDisposition,                // how to create
  DWORD dwFlagsAndAttributes,                 // file attributes
  HANDLE hTemplateFile                        // handle to template file
);

Continue reading

Windows程序设计之第一个基本窗口程序示例

#include <Windows.h>
#include <tchar.h>


// 窗口处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0); // 发送WM_QUIT消息,结束程序
        break;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam); // 系统默认窗口处理函数
}

INT WINAPI _tWinMain(
    _In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPTSTR lpCmdLine,
    _In_ INT nShowCmd)
{
    WNDCLASS wc;
    HWND hWnd;
    MSG msg;

    // 注册窗口类
    ZeroMemory(&wc, sizeof(wc));
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = TEXT("BasicWindow");

    RegisterClass(&wc);
    
    // 创建窗口
    hWnd = CreateWindow(TEXT("BasicWindow"), TEXT("BasicWindow"),
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
 
    // 显示窗口
    ShowWindow(hWnd, nShowCmd);
    UpdateWindow(hWnd);

    // 消息循环
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

Windows程序设计之基本常用函数和类介绍

一、窗口处理函数

LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

二、窗口类

typedef struct _WNDCLASS { 
    UINT       style; 
    WNDPROC    lpfnWndProc; 
    int        cbClsExtra; 
    int        cbWndExtra; 
    HINSTANCE  hInstance; 
    HICON      hIcon; 
    HCURSOR    hCursor; 
    HBRUSH     hbrBackground; 
    LPCTSTR    lpszMenuName; 
    LPCTSTR    lpszClassName; 
} WNDCLASS, *PWNDCLASS; 

ATOM RegisterClass(
  CONST WNDCLASS *lpWndClass  // class data
);

Continue reading

Windows程序设计之使用FindFirstFile和FindNextFile遍历文件夹

一、FindFirstFile和FindNextFile函数

HANDLE FindFirstFile(
  LPCTSTR lpFileName,               // file name
  LPWIN32_FIND_DATA lpFindFileData  // data buffer
);

BOOL FindNextFile(
  HANDLE hFindFile,                // search handle 
  LPWIN32_FIND_DATA lpFindFileData // data buffer
);

typedef struct _WIN32_FIND_DATA {
  DWORD    dwFileAttributes; 
  FILETIME ftCreationTime; 
  FILETIME ftLastAccessTime; 
  FILETIME ftLastWriteTime; 
  DWORD    nFileSizeHigh; 
  DWORD    nFileSizeLow; 
  DWORD    dwReserved0; 
  DWORD    dwReserved1; 
  TCHAR    cFileName[ MAX_PATH ]; 
  TCHAR    cAlternateFileName[ 14 ]; 
} WIN32_FIND_DATA, *PWIN32_FIND_DATA; 

Continue reading

Windows程序设计之Hello World示例

#include <Windows.h>
#include <tchar.h>

INT WINAPI _tWinMain(
    _In_ HINSTANCE hInstance,           // handle to current instance
    _In_opt_ HINSTANCE hPrevInstance,   // handle to previous instance
    _In_ LPTSTR lpCmdLine,              // command line
    _In_ INT nShowCmd)                  // show state
{
    MessageBox(NULL, TEXT("Windows Aplication"), TEXT("HELLO WORLD"), MB_OK);
    return 0;
}