Thread: Make a child window from DLL

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    1

    Make a child window from DLL

    Hi guys I'm new here,
    I am trying to make a DLL so that I call a certain function in that DLL, it will create a child window for the current program from where I call that function(this is going to be called on a different language).


    I got it to work, but the problem is after the child window pop up, after a server seconds it disappears, but when I hover the area where it used to be parts of the child window appear, meaning it is there but has become invisible somehow.

    Let me know if there's a way to fix this.
    Much appreciated.

    I will also add my code(btw I got the code from this forum)
    Code:
    #include "stdafx.h"
    #include <mmsystem.h>
    
    
    HWND window = NULL;
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK HelloWndProc(HWND, UINT, WPARAM, LPARAM);
    
    
    
    
    
    
    _DLLAPI int __stdcall CreateWin(HWND hwnd)
    {
        HINSTANCE hInstance = (HINSTANCE)hwnd;
        static TCHAR szAppName2[] = TEXT("SineApplication");
        HWND hwnd2;
        MSG msg;
        WNDCLASS mywndclass;
    
    
    
    
    
    
        mywndclass.style = CS_HREDRAW | CS_VREDRAW;
        mywndclass.lpfnWndProc = WndProc;
        mywndclass.cbClsExtra = 0;
        mywndclass.cbWndExtra = sizeof(long);
        mywndclass.hInstance = hInstance;
        mywndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        mywndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        mywndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        mywndclass.lpszMenuName = NULL;
        mywndclass.lpszClassName = szAppName2;
    
    
        if (!RegisterClass(&mywndclass))
        {
            MessageBox(NULL, TEXT("Not Working"),
                szAppName2, MB_ICONERROR);
            return 0;
        }
    
    
    
    
        hwnd2 = CreateWindow(szAppName2,
            TEXT("Hello World"),
            WS_CHILD | WS_VISIBLE | WS_CAPTION
            | WS_SYSMENU | WS_THICKFRAME
            | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CLIPSIBLINGS,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            300,
            300,
            hwnd,
            NULL,
            hInstance,
            NULL);
    
    
        SetParent(hwnd2, hwnd);
        ShowWindow(hwnd2, SW_SHOW);
        UpdateWindow(hwnd2);
    
    
        while (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
    
        return msg.wParam;
    }
    
    
    
    
    
    
    
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC     hdc;
        PAINTSTRUCT ps;
        RECT    rect;
    
    
        switch (message) {
        case WM_CREATE:
            return 0;
    
    
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
    
    
            GetClientRect(hwnd, &rect);
    
    
            DrawText(hdc, TEXT("Welcome to child window"), -1, &rect,
                DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    
    
            EndPaint(hwnd, &ps);
            return 0;
    
    
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
    
    
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    Last edited by ThePromisedOne; 11-21-2018 at 03:46 PM.

  2. #2
    Registered User
    Join Date
    Dec 2010
    Location
    Shamokin PA (middle of nowhere)
    Posts
    145
    Code:
    HINSTANCE hInstance = (HINSTANCE)hwnd;
    A HINSTANCE isn't a HWND. That looks like a very suspect construct to me.

    It looks like you are trying to create a 'Custom Control'. A Custom Control is a child window of a parent. There are very specific ways of going about this.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    Shamokin PA (middle of nowhere)
    Posts
    145
    Here's a link to a tutorial of mine on this topic (Custom Controls)....

    Fred's Tutorial #10: Building A Custom Control - A Simple Example

    The coding is in PowerBASIC but that doesn't matter (much). I have this in C++ somewhere, but I'd have to hunt it up.

    A much better architecture for you to consider, is a registration function in the dll called from the host during the host's WM_CREATE processing. Then the CreateWindow() call for the child window control is in the host - not the server/client.

    Another point to consider is that the client should be able to create multiple instances of the child window control (custom control - that's what RegisterClassEx() gives you - a custom window/contol). For this reason statics or globals are strictly verboten.
    Last edited by freddie; 11-22-2018 at 09:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HDC of a child window
    By Gordon in forum Windows Programming
    Replies: 3
    Last Post: 05-29-2008, 01:58 AM
  2. Access Function in Child Window From Parent Window
    By Welder in forum Windows Programming
    Replies: 3
    Last Post: 05-20-2008, 03:06 AM
  3. parent window minimizing on child window destruction
    By Yarin in forum Windows Programming
    Replies: 1
    Last Post: 03-02-2008, 09:34 AM
  4. Creating a child window in a parent window
    By vopo in forum Windows Programming
    Replies: 8
    Last Post: 10-06-2007, 04:15 PM
  5. Child window inside child window
    By Magos in forum Windows Programming
    Replies: 13
    Last Post: 04-20-2004, 06:51 AM

Tags for this Thread