⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mywindows.c

📁 这是一个介绍 linux 编程知识的文章。
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

#include "common.h"
#include "minigui.h"
#include "gdi.h"
#include "window.h"
#include "control.h"

#include "mywindows.h"

void errorWindow(HWND hwnd, char* str, char* title)
{
    char* a;

    if (!strstr (str, "%s")) {
        a = alloca (strlen (str) + 5);
        strcpy(a, str);
        strcat(a, ": %s");
        str = a;
    }

    myMessageBox (hwnd, MB_OK | MB_ICONSTOP, 
                title?title:"Error", str, strerror (errno));
}

int myMessageBox (HWND hwnd, DWORD dwStyle, char* title, char* text, ...)
{
    char * buf = NULL;
    int size = 0;
    int i = 0;
    va_list args;
    int rc;

    va_start(args, text);

    do {
        size += 1000;
        if (buf) free(buf);
        buf = malloc(size);
        i = vsnprintf(buf, size, text, args);
    } while (i == size);

    va_end(args);

    rc = MessageBox (hwnd, buf, title, dwStyle);

    if (buf)
        free (buf);

    return rc;
}

DLGTEMPLATE DlgButtons =
{
    WS_BORDER | WS_CAPTION, WS_EX_NONE,
    (640 - 418)/2, (480 - 200)/2, 418, 200,
    NULL, 0, 0, 5, NULL, 0
};

#define IDC_BASE        100
#define IDC_ONE         101
#define IDC_TWO         102
#define IDC_THREE       103

CTRLDATA CtrlButtons [] =
{ 
    { "static", WS_VISIBLE | SS_ICON,
        10, 10, 40, 40, IDC_STATIC, "LOGO", 0 },
    { "static", WS_VISIBLE | SS_LEFT,
        50, 10, 340, 120, IDC_STATIC, NULL, 0 },
    { "button", WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP | WS_GROUP,
        100, 140, 80, 24, IDC_ONE, NULL, 0 },
    { "button", WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
        200, 140, 80, 24, IDC_TWO, NULL, 0 },
    { "button", WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
        300, 140, 80, 24, IDC_THREE, NULL, 0 }
};

static int 
ButtonsBoxProc (HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
    case MSG_INITDIALOG:
        return 1;
    break;
        
    case MSG_COMMAND:
        if (wParam == IDC_ONE || wParam == IDC_TWO
                || wParam == IDC_THREE)
            EndDialog (hDlg, wParam);
        break;
    }
    
    return DefaultDialogProc (hDlg, message, wParam, lParam);
}

int myWinMessage (HWND hwnd, char* title, char* button1, 
                       char* text, ...)
{
    char * buf = NULL;
    int size = 0;
    int i = 0;
    va_list args;
    int rc;

    va_start(args, text);
    do {
        size += 1000;
        if (buf) free(buf);
        buf = malloc(size);
        i = vsnprintf(buf, size, text, args);
    } while (i == size);
    va_end(args);

    DlgButtons.caption   = title;
    DlgButtons.controls  = CtrlButtons;
    DlgButtons.controlnr = 3;

    CtrlButtons [1].caption = buf;
    CtrlButtons [2].caption = button1;
    CtrlButtons [2].x       = 300;
    
    CtrlButtons [0].dwAddData = GetLargeSystemIcon (IDI_APPLICATION);
    rc = DialogBoxIndirectParam (&DlgButtons, hwnd,
            ButtonsBoxProc, 0L);

    if (buf)
        free (buf);

    return rc - IDC_BASE;
}

int myWinChoice (HWND hwnd, char* title, char* button1, char* button2,
                       char* text, ...)
{
    char * buf = NULL;
    int size = 0;
    int i = 0;
    va_list args;
    int rc;

    va_start(args, text);
    do {
        size += 1000;
        if (buf) free(buf);
        buf = malloc(size);
        i = vsnprintf(buf, size, text, args);
    } while (i == size);
    va_end(args);

    DlgButtons.caption   = title;
    DlgButtons.controls  = CtrlButtons;
    DlgButtons.controlnr = 4;

    CtrlButtons [1].caption = buf;
    CtrlButtons [2].caption = button1;
    CtrlButtons [3].caption = button2;
    CtrlButtons [2].x       = 200;
    CtrlButtons [3].x       = 300;
    
    CtrlButtons [0].dwAddData = GetLargeSystemIcon (IDI_APPLICATION);
    rc = DialogBoxIndirectParam (&DlgButtons, hwnd,
            ButtonsBoxProc, 0L);

    if (buf)
        free (buf);

    return rc - IDC_BASE;
}

int myWinTernary (HWND hwnd, char* title, char* button1, char* button2,
                       char* button3, char* text, ...)
{
    char * buf = NULL;
    int size = 0;
    int i = 0;
    va_list args;
    int rc;

    va_start(args, text);
    do {
        size += 1000;
        if (buf) free(buf);
        buf = malloc(size);
        i = vsnprintf(buf, size, text, args);
    } while (i == size);
    va_end(args);

    DlgButtons.caption   = title;
    DlgButtons.controls  = CtrlButtons;
    DlgButtons.controlnr = 5;

    CtrlButtons [1].caption = buf;
    CtrlButtons [2].caption = button1;
    CtrlButtons [3].caption = button2;
    CtrlButtons [4].caption = button3;
    CtrlButtons [2].x       = 100;
    CtrlButtons [3].x       = 200;
    CtrlButtons [4].x       = 300;
    
    CtrlButtons [0].dwAddData = GetLargeSystemIcon (IDI_APPLICATION);
    rc = DialogBoxIndirectParam (&DlgButtons, hwnd,
            ButtonsBoxProc, 0L);

    if (buf)
        free (buf);

    return rc - IDC_BASE;
}

static int StatusWinProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
        case MSG_PAINT:
        {
            HDC hdc;
            char* text;
            RECT rc;
            
            hdc = BeginPaint (hWnd);
 
            text = (char*) GetWindowAdditionalData (hWnd);
            if (text) {
                GetClientRect (hWnd, &rc);
                SetTextColor (hdc, COLOR_black);
                SetBkColor (hdc, COLOR_lightgray);
                TextOut (hdc, 5, (rc.bottom - GetCharHeight())>>1, text);
            }
 
            EndPaint (hWnd, hdc);
        }
        return 0;
    }
 
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}

HWND createStatusWin (HWND hParentWnd, int width, int height, 
            char* title, char* text, ...)
{
    HWND hwnd;
    char* buf = NULL;
    int size = 0;
    int i = 0;
    va_list args;
    MAINWINCREATE CreateInfo;

    va_start (args, text);
    do {
        size += 1000;
        if (buf)
            free(buf);
        buf = malloc(size);
        
        i = vsnprintf(buf, size, text, args);
    } while (i == size);
    va_end(args);
    
    width = ClientWidthToWindowWidth (WS_CAPTION | WS_BORDER, width);
    height= ClientHeightToWindowHeight (WS_CAPTION | WS_BORDER, height, FALSE);
    
    CreateInfo.dwStyle = WS_CAPTION | WS_BORDER | WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = title;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor (IDC_WAIT);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = StatusWinProc;
    CreateInfo.lx = (GetGDCapability (HDC_SCREEN, GDCAP_MAXX) - width) >> 1;
    CreateInfo.ty = (GetGDCapability (HDC_SCREEN, GDCAP_MAXY) - height) >> 1;
    CreateInfo.rx = CreateInfo.lx + width;
    CreateInfo.by = CreateInfo.ty + height;
    CreateInfo.iBkColor = COLOR_lightgray;
    CreateInfo.dwAddData = (DWORD) buf;
    CreateInfo.hHosting = hParentWnd;

    hwnd = CreateMainWindow (&CreateInfo);
    if (hwnd == HWND_INVALID)
        return hwnd;

    UpdateWindow (hwnd, TRUE);

    return hwnd;
}

void destroyStatusWin (HWND hwnd)
{
    char* buff;

    buff = (char*)GetWindowAdditionalData (hwnd);

    DestroyMainWindow (hwnd);
    MainWindowThreadCleanup (hwnd);
    free (buff);
}

HWND createProgressWin (HWND hParentWnd, char * title, char * label,
        int id, int range)
{
    HWND hwnd;
    MAINWINCREATE CreateInfo;
    int ww, wh;
    HWND hStatic, hProgBar;

    ww = ClientWidthToWindowWidth (WS_CAPTION | WS_BORDER, 400);
    wh = ClientHeightToWindowHeight (WS_CAPTION | WS_BORDER, 
            (range > 0) ? 70 : 35, FALSE);
    
    CreateInfo.dwStyle = WS_CAPTION | WS_BORDER | WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = title;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(IDC_WAIT);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = DefaultMainWinProc;
    CreateInfo.lx = (GetGDCapability (HDC_SCREEN, GDCAP_MAXX) - ww) >> 1;
    CreateInfo.ty = (GetGDCapability (HDC_SCREEN, GDCAP_MAXY) - wh) >> 1;
    CreateInfo.rx = CreateInfo.lx + ww;
    CreateInfo.by = CreateInfo.ty + wh;
    CreateInfo.iBkColor = COLOR_lightgray;
    CreateInfo.dwAddData = 0L;
    CreateInfo.hHosting = hParentWnd;

    hwnd = CreateMainWindow (&CreateInfo);
    if (hwnd == HWND_INVALID)
        return hwnd;

    hStatic = CreateWindow ("static", 
                  label, 
                  WS_VISIBLE | SS_SIMPLE, 
                  IDC_STATIC, 
                  10, 10, 380, 16, hwnd, 0);
    
    if (range > 0) {
        hProgBar = CreateWindow ("progressbar", 
                  NULL, 
                  WS_VISIBLE,
                  id,
                  10, 30, 380, 30, hwnd, 0);
        SendDlgItemMessage (hwnd, id, PBM_SETRANGE, 0, range);
    }
    else
        hProgBar = HWND_INVALID;

    UpdateWindow (hwnd, TRUE);
    SendMessage (hStatic, MSG_PAINT, 0, 0L);

    if (hProgBar != HWND_INVALID)
        SendMessage (hProgBar, MSG_PAINT, 0, 0L);

    return hwnd;
}

void destroyProgressWin (HWND hwnd)
{
    DestroyAllControls (hwnd);
    DestroyMainWindow (hwnd);
    ThrowAwayMessages (hwnd);
    MainWindowThreadCleanup (hwnd);
}


#define INTERW_CTRLS        5
#define INTERH_CTRLS        5

#define LABEL_HEIGHT        16
#define LABEL_WIDTH         160
#define BUTTON_WIDTH        80
#define BUTTON_HEIGHT       24
#define ENTRY_HEIGHT        22

#define MARGIN_TOP          10
#define MARGIN_BOTTOM       10
#define MARGIN_LEFT         50
#define MARGIN_RIGHT        10

static int
WinMenuBoxProc (HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
{
  switch (message) {
    case MSG_INITDIALOG:
    {
        myWINMENUITEMS* pWinMenuItems = (myWINMENUITEMS*)lParam;
        int i = 0;
        char** items = pWinMenuItems->items;

        while (items[i]) {
            SendDlgItemMessage (hDlg, pWinMenuItems->listboxid, 
                    LB_ADDSTRING, 0, (LPARAM)(items[i]));
            i++;
        }
        
        SendDlgItemMessage (hDlg, pWinMenuItems->listboxid,
                LB_SETCURSEL, (WPARAM)(*(pWinMenuItems->selected)), 0L);

        SetWindowAdditionalData2 (hDlg, (DWORD)lParam);
        return 1;
    }
    break;

    case MSG_COMMAND:
    {
        myWINMENUITEMS* pWinMenuItems 
            = (myWINMENUITEMS*)GetWindowAdditionalData2 (hDlg);

        if ( (wParam >= pWinMenuItems->minbuttonid) 
                && (wParam <= pWinMenuItems->maxbuttonid) ) {
            *(pWinMenuItems->selected)
                = SendDlgItemMessage (hDlg, pWinMenuItems->listboxid,
                        LB_GETCURSEL, 0, 0L);
            EndDialog (hDlg, wParam);
        }
    }
        break;
  }

    return DefaultDialogProc (hDlg, message, wParam, lParam);
}

int myWinMenu (HWND hParentWnd, const char* title, const char* label, 
                int width, int listboxheight, char** items, int* listItem, 
                myWINBUTTON* buttons)
{
    DLGTEMPLATE DlgBoxData;
    CTRLDATA* pCtrlData;
    int buttonCount = 0, ctrlCount;
    int i;
    int cw, ch, ww, wh;
    int maxid = 0, minbuttonid, maxbuttonid;
    int buttonx, buttony;
    myWINMENUITEMS WinMenuItems;
    RECT rcText;
    int listboxy;

    minbuttonid = buttons->id;
    maxbuttonid = buttons->id;
    while ((buttons + buttonCount)->text) {
        int id;

        id = (buttons + buttonCount)->id;
        maxid += id;
        if ( id > maxbuttonid)
            maxbuttonid = id;
        else if (id < minbuttonid)
            minbuttonid = id;

        buttonCount ++;
    }
    maxid ++;

    if (buttonCount == 0)
        return 0;

    ctrlCount = buttonCount + 3;
    if ( !(pCtrlData = alloca (sizeof (CTRLDATA) * ctrlCount)) )
        return 0;
    
    DlgBoxData.dwStyle      = WS_CAPTION | WS_BORDER;
    DlgBoxData.dwExStyle    = WS_EX_NONE;
    DlgBoxData.caption      = title;
    DlgBoxData.hIcon        = 0;
    DlgBoxData.hMenu        = 0;
    DlgBoxData.controlnr    = ctrlCount;
    DlgBoxData.controls     = pCtrlData;
    DlgBoxData.dwAddData    = 0L;

    cw = BUTTON_WIDTH * buttonCount + INTERW_CTRLS * (buttonCount - 1);
    cw = max (cw, width);
    cw += MARGIN_LEFT + MARGIN_RIGHT;

    rcText.left = 0;
    rcText.top  = 0;
    rcText.right = cw - MARGIN_LEFT - MARGIN_RIGHT;
    rcText.bottom = GetCharHeight ();
    DrawText (HDC_SCREEN, label, -1, &rcText, 
                DT_LEFT | DT_TOP | DT_WORDBREAK | DT_EXPANDTABS | DT_CALCRECT);

    ch = listboxheight + BUTTON_HEIGHT + INTERH_CTRLS 
            + MARGIN_TOP + MARGIN_BOTTOM + RECTH (rcText) + INTERH_CTRLS;

    ww = ClientWidthToWindowWidth (DlgBoxData.dwStyle, cw);
    wh = ClientHeightToWindowHeight (DlgBoxData.dwStyle, ch, FALSE);

    DlgBoxData.x = (GetGDCapability (HDC_SCREEN, GDCAP_HPIXEL) - ww)/2;
    DlgBoxData.y = (GetGDCapability (HDC_SCREEN, GDCAP_VPIXEL) - wh)/2;
    DlgBoxData.w = ww;
    DlgBoxData.h = wh;
   
    // LOGO:
    pCtrlData->class    = "static";
    pCtrlData->dwStyle  = WS_CHILD | WS_VISIBLE | SS_ICON;
    pCtrlData->x        = 10;
    pCtrlData->y        = MARGIN_TOP;
    pCtrlData->w        = 40;
    pCtrlData->h        = 40;;
    pCtrlData->id       = IDC_STATIC;
    pCtrlData->caption  = "LOGO";
    pCtrlData->dwAddData = GetLargeSystemIcon (IDI_APPLICATION);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -