2 wuvalue wuvalue 于 2016.05.04 20:25 提问

GetModuleFileName替换GetCurrentDirectory打不开文件了?求问,谢谢

#include
#include //_T()

#define ID_LIST 1
#define ID_TEXT 2
#define MAXREAD 8192
#define DIRATTR (DDL_READWRITE | DDL_READONLY | DDL_HIDDEN | DDL_SYSTEM |\
DDL_DIRECTORY | DDL_ARCHIVE | DDL_DRIVES)

#define DTFLAGS (DT_WORDBREAK | DT_EXPANDTABS | DT_NOCLIP | DT_NOPREFIX)
WNDPROC OldList;

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

RECT rect;
TCHAR szBuffer[MAX_PATH + 1];

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevINstance, LPSTR lPCmdLine, int nCmdShow)
{
static TCHAR szAppName[] = _T("head");
WNDCLASSEX WindowClass;
HWND hWnd;
MSG msg;

WindowClass.cbSize = sizeof(WNDCLASSEX);
WindowClass.style = CS_HREDRAW | CS_VREDRAW;
WindowClass.lpfnWndProc = WndProc;
WindowClass.cbClsExtra = 0;
WindowClass.cbWndExtra = 0;
WindowClass.hInstance = hInstance;
WindowClass.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
WindowClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
WindowClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); //防止程序运行时,系统颜色改变,出现空值
WindowClass.lpszMenuName = nullptr;
WindowClass.lpszClassName = szAppName;
WindowClass.hIconSm = nullptr;

if (!RegisterClassEx(&WindowClass))
{
    MessageBoxEx(NULL, _T("This program requires windowsNT!"), szAppName,
        MB_ICONERROR, LANG_CHINESE_SIMPLIFIED);
    return 0;
}

hWnd = CreateWindowEx(
    WS_EX_STATICEDGE,//WS_EX_OVERLAPPEDWINDOW,
    szAppName,
    _T("head"),
    WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,   //
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    nullptr,
    nullptr,
    hInstance,
    nullptr
);

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

while (GetMessage(&msg, nullptr, 0, 0) == TRUE)
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

int             i, cxChar, cyChar;
HANDLE          hFile;
TCHAR           szFile[MAX_PATH + 1];
BYTE            buffer[MAXREAD];
static BOOL     bValidFile;
static HWND     hwndList, hwndText;
HDC             hdc;

PAINTSTRUCT     ps;


switch (message)
{

case WM_CREATE:

    cxChar = LOWORD(GetDialogBaseUnits());
    cyChar = HIWORD(GetDialogBaseUnits());
    rect.left = 20 * cxChar;
    rect.top = 3 * cyChar;

    hwndList = CreateWindowEx(
        WS_EX_STATICEDGE,//WS_EX_OVERLAPPEDWINDOW,
        _T("listbox"),
        nullptr,
        WS_CHILDWINDOW | WS_VISIBLE | LBS_STANDARD,         //LBS_STANDARD由LBS_NOTIFY、LBS_SORT、WS_VSCROLL、WS_BORDER组合
        cxChar,                                             //LBS_NOTIFY使父窗口收到消息
        cyChar * 3,
        cxChar * 13 + GetSystemMetrics(SM_CXVSCROLL),
        cyChar * 10,
        hwnd,
        (HMENU)ID_LIST,
        (HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE),  //((LPCREATESTRUCT)lParam)->hInstance,
        nullptr
    );

    GetModuleFileNameW(NULL, (LPWSTR)szBuffer,MAX_PATH + 1);               //Load current directry to buffer

// GetCurrentDirectory(MAX_PATH + 1, szBuffer);

    hwndText = CreateWindowEx(
        WS_EX_STATICEDGE,//WS_EX_OVERLAPPEDWINDOW,
        _T("static"),
        szBuffer,
        WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT,
        cxChar,
        cyChar,
        cxChar * MAX_PATH,
        cyChar,
        hwnd,
        (HMENU) ID_TEXT,
        (HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE),  //((LPCREATESTRUCT)lParam)->hInstance,
        nullptr
    );

    OldList = (WNDPROC)SetWindowLongPtr(hwndList,GWL_WNDPROC,(LPARAM) ListProc);

    SendMessage(hwndList, LB_DIR, DIRATTR, (LPARAM) _T("*.*"));
    return 0;

case WM_SIZE:

    rect.right = LOWORD(lParam);
    rect.bottom = HIWORD(lParam);
    return 0;

case WM_SETFOCUS:
    SetFocus(hwndList);
    return 0;

case WM_COMMAND:
    if (LOWORD(wParam) == ID_LIST && HIWORD(wParam) == LBN_DBLCLK)  //Scan ID和通知码
    {
        if (LB_ERR == (i = SendMessage(hwndList, LB_GETCURSEL, 0, 0)))
            break;

        SendMessage(hwndList, LB_GETTEXT, i, (LPARAM) szBuffer);

        if (INVALID_HANDLE_VALUE != (hFile = CreateFile(szBuffer,GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0,nullptr)))
        {
            CloseHandle(hFile);
            bValidFile = TRUE;
            lstrcpy(szFile, szBuffer);

// GetCurrentDirectory(MAX_PATH + 1, szBuffer);

            GetModuleFileNameW(NULL, (LPWSTR)szBuffer, MAX_PATH + 1);

            if (szBuffer[lstrlen(szBuffer) - 1] != '\\')            //添加\在主目录和子目录之间
                lstrcat(szBuffer, _T("\\"));
            SetWindowText(hwndText,lstrcat(szBuffer, szFile));
        }
        else
        {
            bValidFile = FALSE;
            szBuffer[lstrlen(szBuffer) - 1] = '\0';

            if (!SetCurrentDirectory(szBuffer + 1))   ?/
            {
                szBuffer[3] = ':';
                szBuffer[4] = '\0';
                SetCurrentDirectory(szBuffer + 2);
            }

            GetModuleFileNameW(NULL,(LPWSTR)szBuffer, MAX_PATH + 1);

// GetCurrentDirectory(MAX_PATH + 1, szBuffer);

            SetWindowText(hwndText, szBuffer);
            SendMessage(hwndList, LB_RESETCONTENT, 0 ,0);
            SendMessage(hwndList, LB_DIR, DIRATTR, (LPARAM)_T("*.*"));
        }
        InvalidateRect(hwnd, nullptr, TRUE);
    }
    return 0;

case WM_PAINT:

hdc = BeginPaint(hwnd, &ps);
if (!bValidFile)
{
DrawText(hdc, _T("---测验-123--"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
break;
}

    if (INVALID_HANDLE_VALUE == (hFile = CreateFile(szBuffer,GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)))
    {
        DrawTextW(hdc, (LPCWSTR)buffer, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        bValidFile = FALSE;
        break;
    }
    ReadFile(hFile, buffer, MAXREAD, (LPDWORD)(&i), NULL);
    CloseHandle(hFile);


    SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
    SetTextColor(hdc, GetSysColor(COLOR_BTNTEXT));
    SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));

    DrawTextA(hdc< (LPCSTR)buffer, i, &rect, DTFLAGS);  

// DT_WORDBREAK------- Lines are automatically broken between words if a word extends past the edge of the rectangle
// DT_EXPANDTABS------Expands tab characters. The default number of characters per tab is eight
// DT_NOCLIP----------Draws without clipping. DrawTextEx is somewhat faster when DT_NOCLIP is used.
// DT_NOPREFIX--------single line

// DrawTextEx(hdc, (LPWSTR)buffer, i, &rect, DTFLAGS, NULL);
EndPaint(hwnd, &ps);
return 0;

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

}

LRESULT CALLBACK ListProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_KEYDOWN && wParam == VK_RETURN)
SendMessage(GetParent(hwnd), WM_COMMAND, MAKELONG(1, LBN_DBLCLK), (LPARAM) hwnd);

return CallWindowProc(OldList, hwnd, message, wParam, lParam);

}

2个回答

oyljerry
oyljerry   Ds   Rxr 2016.05.04 21:06

直接对比两者返回的路径是否相同

wuvalue
wuvalue 谢谢你
一年多之前 回复
CSDNXIAOD
CSDNXIAOD   2016.05.04 20:32

android 项目文件打不开的问题
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

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