NO

Author Topic: File location  (Read 9575 times)

Tsiku

  • Guest
File location
« on: March 15, 2010, 12:33:45 PM »
We are learning programming in c, and using pelles c for it. I need to make project where i drag file in to pelle c console and it shows its full file path. Can anyone help me get me started with it?

Tsiku

  • Guest
Re: File location
« Reply #1 on: March 18, 2010, 08:25:46 PM »
anyone ?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: File location
« Reply #2 on: March 18, 2010, 09:56:08 PM »
Look these functions:

VOID DragAcceptFiles(
    HWND hWnd,
    BOOL fAccept
);
UINT DragQueryFile(
    HDROP hDrop,
    UINT iFile,
    LPTSTR lpszFile,
    UINT cch
);
VOID DragFinish(
    HDROP hDrop
);

This windows message: WM_DROPFILES

Must it to be console application ?
May the source be with you

Tsiku

  • Guest
Re: File location
« Reply #3 on: March 20, 2010, 11:19:24 AM »
Look these functions:

VOID DragAcceptFiles(
    HWND hWnd,
    BOOL fAccept
);
UINT DragQueryFile(
    HDROP hDrop,
    UINT iFile,
    LPTSTR lpszFile,
    UINT cch
);
VOID DragFinish(
    HDROP hDrop
);

This windows message: WM_DROPFILES

Must it to be console application ?


Yes a console application, and it must have 3  .lib files and 3  .dll files .

Tsiku

  • Guest
Re: File location
« Reply #4 on: March 22, 2010, 11:46:40 AM »
how to get started with those example codes ?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: File location
« Reply #5 on: March 22, 2010, 12:44:34 PM »
Those works only with GUI (windowed) program.

Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>

#pragma lib "shell32.lib"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void WM_DropFiles(HWND hWnd, HDROP hDrop);

char szAppName[] = "WinDragDrop";
char szFrameClass[] = "cFrame";
HWND hFrame;
HANDLE hInst;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                LPSTR lpCmdLine, int nCmdShow)
{
        WNDCLASS wc;
        MSG msg;

        wc.style        = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc  = (WNDPROC) 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)1;
        wc.lpszMenuName = NULL;
        wc.lpszClassName= szFrameClass;

        if (!RegisterClass(&wc)) return 0;
        hInst = hInstance;

        hFrame = CreateWindow(szFrameClass, szAppName,
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT, CW_USEDEFAULT,
                CW_USEDEFAULT, CW_USEDEFAULT,
                NULL, NULL, hInst, NULL);
        if(!hFrame) return 0;
        ShowWindow(hFrame, nCmdShow);
        UpdateWindow(hFrame);
        DragAcceptFiles(hFrame, TRUE);
        while(GetMessage(&msg, NULL, 0, 0))
        {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }
        DragAcceptFiles(hFrame, FALSE);
         return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
        switch(wMsg) {
        case WM_DROPFILES:
                WM_DropFiles(hWnd, (HDROP)wParam);
                return 0;
//      case WM_CREATE:
        case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        default:
                return DefWindowProc(hWnd, wMsg, wParam, lParam);
    }
}

void WM_DropFiles(HWND hWnd, HDROP hDrop)
{
int nCnt, nLen, nIdx;
char szFile[260];

nCnt = DragQueryFile(hDrop, -1, NULL, 0);
for (nIdx = 0; nIdx < nCnt; nIdx++) {
nLen = DragQueryFile(hDrop, nIdx, NULL, 0);
DragQueryFile(hDrop, nIdx, szFile, sizeof(szFile));
}
DragFinish(hDrop);
}
May the source be with you

Tsiku

  • Guest
Re: File location
« Reply #6 on: March 22, 2010, 03:19:30 PM »
But any code for console ?

Tsiku

  • Guest
Re: File location
« Reply #7 on: March 24, 2010, 07:42:16 PM »
Can that program done in pelles c console app ?

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: File location
« Reply #8 on: March 25, 2010, 03:03:55 AM »
Can that program done in pelles c console app ?
As you already got the answer: NO.

Drag&Drop is a GUI operation, there is no such thing in a console...

Ralf

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: File location
« Reply #9 on: March 25, 2010, 08:16:25 AM »
Can that program done in pelles c console app ?
As you already got the answer: NO.

Drag&Drop is a GUI operation, there is no such thing in a console...

Ralf
I am not sure that this is correct, because if you drop a file in a console window you get the file name in the command line.

I think you must try to find the handle of the console window and then use a code like the code from timovjl.
best regards
 Alex ;)

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: File location
« Reply #10 on: March 25, 2010, 08:53:12 AM »
I think it is similar to handling a simple paste action.

Have you already tried to drag and drop a file on your application window, while waiting for input?
---
Stefan

Proud member of the UltraDefrag Development Team

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: File location
« Reply #11 on: March 25, 2010, 09:42:58 AM »
perhaps cmd.exe's console window isn't window with normal message loop.

You can get window handle thisway:
HWND hWndCon = GetConsoleWindow();

But i can't get messages from it with PeekMessage();
May the source be with you

Offline DMac

  • Member
  • *
  • Posts: 272
Re: File location
« Reply #12 on: March 25, 2010, 06:24:19 PM »

We are learning programming in c, and using pelles c for it. I need to make project where i drag file in to pelle c console and it shows its full file path.

There is a very simple way to do just this if this is all you have to do.
I played around and came up with a one line application in about 5 minutes.

Notice that you can open a command prompt window and whatever file you drag into it causes the prompt to display the file's path.  Since this is the default behavior of the command prompt there is no need to try to duplicate it.

What you want to do is launch the command prompt from your application.  Here is a little snippet to get you started:

Code: [Select]
#include <stdlib.h>

int main(int argc, char *argv[])
{
     return [YOUR ONE LINE OF HOMEWORK CODE HERE];
}

Tip: when you launch cmd.exe use the /k switch followed by cd C:\\ to set the prompt to the root directory.
No one cares how much you know,
until they know how much you care.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: File location
« Reply #13 on: March 26, 2010, 04:19:55 AM »
Can that program done in pelles c console app ?
As you already got the answer: NO.

Drag&Drop is a GUI operation, there is no such thing in a console...

Ralf
I am not sure that this is correct, because if you drop a file in a console window you get the file name in the command line.

I think you must try to find the handle of the console window and then use a code like the code from timovjl.
Well, I am not sure that you get any window handle in a true console program, the only way to accomplish what the OP seems to try and achieve would be via commandline arguments, and I assume that this is what DMac tried to say (I guess)...

Ralf

Offline DMac

  • Member
  • *
  • Posts: 272
Re: File location
« Reply #14 on: March 26, 2010, 05:23:05 PM »
Quote
Well, I am not sure that you get any window handle in a true console program

The console does indeed run inside a window and it is possible to get the handle of that window.

I once launched a windows dialog from the console, then, by artful means, got the consol window's handle.  I then set the dialog I created to be the consol window's parent.

Quote
[T]he only way to accomplish what the OP seems to try and achieve would be via commandline arguments, and I assume that this is what DMac tried to say (I guess)...

I tried to give the OP enough clues to find the solution without giving him the actual solution.  However I guess I was not clear enough.

I found that I could simply launch cmd.exe from my console app just as I would do it manually by Start>Run and typing "cmd.exe /k cd c:\"

What is realy happening is that my console app runs, launches another command prompt, and exits.
The command prompt that I launched remains open and I can drag a file into it and see the path to the file echoed there.

DMac
No one cares how much you know,
until they know how much you care.