Author Archives: admin

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

C++语言程序设计之正则表达式的简单示例

现在来说,支持C++11标准的C++语言均支持正则表达式。下面是简单的正则表达式示例,it定位到第一个位置,end_it是空迭代器,起到尾后迭代器的作用。同时,for循环中的it递增运算通过regex_search来推进迭代器,最终完成遍历任务。

#include <regex>
#include <iostream>

int main(void)
{
	std::string data = "<Person><Item><ID>100001</ID><NAME>CHN</NMAE><AGE>5500</AGE></Item>"
		"<Item><ID>100002</ID><NAME>USA</NMAE><AGE>300</AGE></Item></Person>";
	std::regex re("<Item><ID>(.*?)</ID><NAME>(.*?)</NMAE><AGE>(.*?)</AGE></Item>");

	for (std::sregex_iterator it(data.begin(), data.end(), re), end_it; it != end_it; it++)
	{
		std::cout << it->str(1) << "\t"
			<< it->str(2) << "\t"
			<< it->str(3) << std::endl;
	}

	return 0;
}

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;
}

Qt程序设计之正则表达式的简单示例

正则表达式(Regular Expression)描述了一种字符串匹配的模式(Pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

#include <QRegularExpression>

int main(void)
{
    QString data = "<Person><Item><ID>100001</ID><NAME>CHN</NMAE><AGE>5500</AGE></Item>"
                   "<Item><ID>100002</ID><NAME>USA</NMAE><AGE>300</AGE></Item></Person>";
    QRegularExpression re("<Item><ID>(.*?)</ID><NAME>(.*?)</NMAE><AGE>(.*?)</AGE></Item>");

    QRegularExpressionMatchIterator it = re.globalMatch(data);
    while (it.hasNext())
    {
        QRegularExpressionMatch match = it.next();
        qDebug() << match.captured(1).toUtf8().data()
                 << match.captured(2).toUtf8().data()
                 << match.captured(3).toUtf8().data();
    }

    return 0;
}

Qt程序设计之Unicode编码字符串转中文问题

Qt程序设计之Unicode编码字符串转中文问题,就是要替换掉前面的“\\u”,再将4位16进制数转成QChar类型,问题基本解决。下面就用Qt自带的正则表达式解决,方法如下:

QString Unicode2CN(QString data)
{
    QRegularExpression re("\\\\u([0-9a-fA-F]{4})");
    QRegularExpressionMatchIterator it = re.globalMatch(data);

    while(it.hasNext())
    {
        QRegularExpressionMatch match = it.next();
        data.replace(match.captured(0), QChar(match.captured(1).toUShort(NULL, 16)));
    }

    return data;
}

Continue reading