如何在 Windows 上显示打开文件对话框?

发布于 2024-07-29 02:54:07 字数 3939 浏览 6 评论 0原文

我试图根据 msdn 的说法在 Windows CE 6.0 上显示一个 openfile 对话框,它与 win32 中的过程相同,但它不起作用。 我提交代码中有趣的部分以供审查:

#include <windows.h>
#include <commctrl.h>
#include <winuser.h>
#include <stdio.h>
#include <Commdlg.h>

/* Prototypes */
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
HWND create_window(int width, int height) ;
void resize_window(int width, int height) ;

HWND    g_hWindow ;

/* Function to modify the host window size to match the size of the movie. */
void resize_window(int width, int height)
{
   RECT r;
   r.left = r.top = 0;
   r.right = width ;
   r.bottom  = height ;
   AdjustWindowRectEx(&r,WS_BORDER,false,WS_EX_CLIENTEDGE);
   SetWindowPos(g_hWindow, NULL, 0,0,r.right, r.bottom,SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
}


HWND create_window(int width, int height)
{
   WNDCLASS wce; // A Window class in Windows CE

   wce.style         = CS_VREDRAW | CS_HREDRAW; 
   wce.lpfnWndProc   = (WNDPROC) WndProc; 
   wce.cbClsExtra    = 0; 
   wce.cbWndExtra    = 0; 
   wce.hInstance     = GetModuleHandle(NULL); 
   wce.hIcon         = LoadIcon((HINSTANCE) NULL, (LPCWSTR)MB_ICONQUESTION);
   wce.hCursor       = LoadCursor((HINSTANCE) NULL, IDC_ARROW); 
   wce.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
   wce.lpszMenuName  = 0;
   wce.lpszClassName = _TEXT("TEST");

   if (!RegisterClass(&wce)) return 0; 

   RECT r;
   r.left = r.top = 0;
   r.right = width ;
   r.bottom  = height ;
   AdjustWindowRectEx(&r,WS_BORDER,false,WS_EX_CLIENTEDGE);

  // Create the window
   g_hWindow = CreateWindowEx(
      0,          // Ex Styles
      WS_BORDER,      // creates a window that has a thin-line border with no title bar
      CW_USEDEFAULT,  // x
      CW_USEDEFAULT,  // y
      r.right-r.left,  // Height
      r.bottom-r.top,  // Width
      NULL,           // Parent Window
      NULL,           // Menu, or windows id if child
      GetModuleHandle(NULL),      // 
      NULL            // Pointer to window specific data
      );

   ShowWindow( g_hWindow, SW_SHOW  ); // make the window visible on the display

   return g_hWindow ;
}

/* 
 * Messaging function call back from Windows CE that is passed as 
 * an argument when the window is created
 */
LRESULT CALLBACK WndProc(HWND   hWnd,
                         UINT   msg,
                         WPARAM wParam,
                         LPARAM lParam ) 
{

   switch( msg ) 
   {
    case WM_ACTIVATEAPP:


            // Invalidate to get new text painted.
        this->Invalidate();
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;   

    case WM_CREATE:
        LPOPENFILENAME opendialog;
        wchar_t szFile[260];


        opendialog = (LPOPENFILENAME) malloc(sizeof (LPOPENFILENAME));
        opendialog->lStructSize = sizeof (LPOPENFILENAME);
        opendialog->hwndOwner = g_hWindow;
        opendialog->hInstance = GetModuleHandle(NULL);
        *szFile = (char_t)_TEXT("\0");
        opendialog->lpstrFile = szFile;
        opendialog->nFilterIndex = 0;
        opendialog->nMaxFile = 256;
        opendialog->lpstrInitialDir = NULL;
        opendialog->Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

        GetOpenFileName(opendialog);


    default:
        return DefWindowProc( hWnd, msg, wParam, lParam);
   } 
   return 0;
}

/* Function to hide (or make visible) the taskbar so that the player has the full display */
void set_display(bool set_full)
{
   HWND hwndTaskbar = ::FindWindow(L"HHTaskBar", NULL);
   if (set_full)
   {
       ::ShowWindow(hwndTaskbar, SW_HIDE); 
   }
   else
   {
       ::ShowWindow(hwndTaskbar, SW_SHOW); 
   }
   g_full_scrn = set_full;
}


int _cdecl WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR argv, int argc )
{

   memset(&g_gfx_context,0,sizeof(g_gfx_context));
   create_window(1, 1); // This is a windows CE specific call.
}

I'm trying to get an openfile dialog to show up on windows CE 6.0 according to msdn it's the same process as in win32, but it doesn't work. I submit for review the interresting part of the code :

#include <windows.h>
#include <commctrl.h>
#include <winuser.h>
#include <stdio.h>
#include <Commdlg.h>

/* Prototypes */
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
HWND create_window(int width, int height) ;
void resize_window(int width, int height) ;

HWND    g_hWindow ;

/* Function to modify the host window size to match the size of the movie. */
void resize_window(int width, int height)
{
   RECT r;
   r.left = r.top = 0;
   r.right = width ;
   r.bottom  = height ;
   AdjustWindowRectEx(&r,WS_BORDER,false,WS_EX_CLIENTEDGE);
   SetWindowPos(g_hWindow, NULL, 0,0,r.right, r.bottom,SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
}


HWND create_window(int width, int height)
{
   WNDCLASS wce; // A Window class in Windows CE

   wce.style         = CS_VREDRAW | CS_HREDRAW; 
   wce.lpfnWndProc   = (WNDPROC) WndProc; 
   wce.cbClsExtra    = 0; 
   wce.cbWndExtra    = 0; 
   wce.hInstance     = GetModuleHandle(NULL); 
   wce.hIcon         = LoadIcon((HINSTANCE) NULL, (LPCWSTR)MB_ICONQUESTION);
   wce.hCursor       = LoadCursor((HINSTANCE) NULL, IDC_ARROW); 
   wce.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
   wce.lpszMenuName  = 0;
   wce.lpszClassName = _TEXT("TEST");

   if (!RegisterClass(&wce)) return 0; 

   RECT r;
   r.left = r.top = 0;
   r.right = width ;
   r.bottom  = height ;
   AdjustWindowRectEx(&r,WS_BORDER,false,WS_EX_CLIENTEDGE);

  // Create the window
   g_hWindow = CreateWindowEx(
      0,          // Ex Styles
      WS_BORDER,      // creates a window that has a thin-line border with no title bar
      CW_USEDEFAULT,  // x
      CW_USEDEFAULT,  // y
      r.right-r.left,  // Height
      r.bottom-r.top,  // Width
      NULL,           // Parent Window
      NULL,           // Menu, or windows id if child
      GetModuleHandle(NULL),      // 
      NULL            // Pointer to window specific data
      );

   ShowWindow( g_hWindow, SW_SHOW  ); // make the window visible on the display

   return g_hWindow ;
}

/* 
 * Messaging function call back from Windows CE that is passed as 
 * an argument when the window is created
 */
LRESULT CALLBACK WndProc(HWND   hWnd,
                         UINT   msg,
                         WPARAM wParam,
                         LPARAM lParam ) 
{

   switch( msg ) 
   {
    case WM_ACTIVATEAPP:


            // Invalidate to get new text painted.
        this->Invalidate();
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;   

    case WM_CREATE:
        LPOPENFILENAME opendialog;
        wchar_t szFile[260];


        opendialog = (LPOPENFILENAME) malloc(sizeof (LPOPENFILENAME));
        opendialog->lStructSize = sizeof (LPOPENFILENAME);
        opendialog->hwndOwner = g_hWindow;
        opendialog->hInstance = GetModuleHandle(NULL);
        *szFile = (char_t)_TEXT("\0");
        opendialog->lpstrFile = szFile;
        opendialog->nFilterIndex = 0;
        opendialog->nMaxFile = 256;
        opendialog->lpstrInitialDir = NULL;
        opendialog->Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

        GetOpenFileName(opendialog);


    default:
        return DefWindowProc( hWnd, msg, wParam, lParam);
   } 
   return 0;
}

/* Function to hide (or make visible) the taskbar so that the player has the full display */
void set_display(bool set_full)
{
   HWND hwndTaskbar = ::FindWindow(L"HHTaskBar", NULL);
   if (set_full)
   {
       ::ShowWindow(hwndTaskbar, SW_HIDE); 
   }
   else
   {
       ::ShowWindow(hwndTaskbar, SW_SHOW); 
   }
   g_full_scrn = set_full;
}


int _cdecl WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR argv, int argc )
{

   memset(&g_gfx_context,0,sizeof(g_gfx_context));
   create_window(1, 1); // This is a windows CE specific call.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2024-08-05 02:54:07
LPOPENFILENAME opendialog;
mb_char_t szFile[260];

opendialog = (LPOPENFILENAME) mb_malloc(sizeof (LPOPENFILENAME));
opendialog->lStructSize = sizeof (LPOPENFILENAME);

您在这里声明一个指向 OPENFILENAME 结构的指针,并为其分配内存。

您应该能够直接在堆栈上分配 OPENFILENAME ,如下所示:

OPENFILENAME opendialog = {0};
mb_char_t szFile[260] = {0};

opendialog.lStructSize = sizeof (opendialog);
opendialog.hwndOwner = g_hWindow;
opendialog.hInstance = GetModuleHandle(NULL);
opendialog.lpstrFile = szFile;
opendialog.nFilterIndex = 0;
opendialog.nMaxFile = 256;
opendialog.lpstrInitialDir = NULL;
opendialog.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

GetOpenFileName(&opendialog);
LPOPENFILENAME opendialog;
mb_char_t szFile[260];

opendialog = (LPOPENFILENAME) mb_malloc(sizeof (LPOPENFILENAME));
opendialog->lStructSize = sizeof (LPOPENFILENAME);

You are declaring a pointer to an OPENFILENAME structure here, and allocating memory for it.

You should be able to allocate OPENFILENAME on stack directly, like so:

OPENFILENAME opendialog = {0};
mb_char_t szFile[260] = {0};

opendialog.lStructSize = sizeof (opendialog);
opendialog.hwndOwner = g_hWindow;
opendialog.hInstance = GetModuleHandle(NULL);
opendialog.lpstrFile = szFile;
opendialog.nFilterIndex = 0;
opendialog.nMaxFile = 256;
opendialog.lpstrInitialDir = NULL;
opendialog.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

GetOpenFileName(&opendialog);
霊感 2024-08-05 02:54:07

这里有一个问题:

opendialog->lStructSize = sizeof (LPOPENFILENAME);

这会将结构大小设置为指针的大小,在您的例子中为 4。 你应该有:

opendialog->lStructSize = sizeof (OPENFILENAME);

Here's a problem:

opendialog->lStructSize = sizeof (LPOPENFILENAME);

That will set the struct size to the size of a pointer, which is 4 in your case. You should have:

opendialog->lStructSize = sizeof (OPENFILENAME);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文