标题:C语言在图形界面下怎么实现连接mysql数据库等操作
取消只看楼主
rain9797
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2020-3-1
结帖率:100%
 问题点数:0 回复次数:1 
C语言在图形界面下怎么实现连接mysql数据库等操作
在使用mysql和win32api的时候,我发现在VS2019下,项目-属性-链接器-子系统-控制台上我的mysql可以正常操作,但是当我添加了图形界面后,控制台得变为窗口,这个时候,我不能连接数据库了并对数据库进行一些操作了,这该怎么办?为了稍微简单点,我把源代码的宏定义什么的删了


程序代码:

MYSQL mysql;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)
{
    //mysql部分
    if (NULL == mysql_init(&mysql))                                             //分配和初始化MYSQL对象  
    {
       printf("mysql_init(): %s\n", mysql_error(&mysql));
    }

    if (NULL == mysql_real_connect(&mysql,//尝试与运行在主机上的MySQL数据库引擎建立连接
                                    "localhost",
                                    "root",
                                    "123321qwertasd.",
                                "c_design", 0, NULL, 0)){
        printf("mysql_real_connect(): %s\n", mysql_error(&mysql));
    }

    //printf("Connected MySQL successful! \n"); 
    
    if (mysql_query(&mysql, CREATE_DATABASE) == 0)                      //创建数据库
    {                
        printf("create database successful!\n");
    }

    if (mysql_query(&mysql, CREAT_TABLE) == 0)                          //创建表
    {                
        printf("create table successful!\n");
    }

    if (mysql_query(&mysql, LOAD_SORCE) == 0)                           //加载文本文件
    {
        printf("load successful!\n");
    }
    else{
        printf("load failed!\n");
    }

    if (mysql_query(&mysql, CREATE_DATABASE) == 0) {
        printf("create database successful!\n");
    }
    else {
        printf("database has exited!\n");
    }

    mysql_close(&mysql);
    
    //图形用户界面部分
    WNDCLASSEX wc;                          //窗口类
    HWND hwnd;                              //窗口句柄
    MSG Msg;                                //消息

    //设置顶级窗口基本属性
    wc.cbSize = sizeof(WNDCLASSEX);         
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = CreateSolidBrush(RGB(255,255,255));
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "window_class";

    //注册主窗口类
    if (!RegisterClassEx(&wc))              
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindow("window_class", "登录", WS_OVERLAPPED | WS_CAPTION | WS_EX_LAYERED, 500, 200, 360, 390, NULL, NULL, NULL, NULL);

    if (hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, 1);
    UpdateWindow(hwnd);

    //设置login窗口基本属性
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = LoginWndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "login";

    //注册login窗口类
    if (!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    //设置register窗口基本属性
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = RegisterWndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "register";

    //注册register窗口类基本属性
    if (!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    //设置improve窗口类基本属性
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = ImproveWndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "improve";

    //注册improve窗口类
    if (!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    //消息队列
    while (GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)//窗口过程处理函数
{
   
    HINSTANCE hInstance = GetModuleHandle(NULL);                           //获取一个模块句柄
    HWND llogin  ;                                                         //窗口句柄
    HWND rregister;

    switch (msg)
    {
        //在父窗口中定义多个子控件
    case WM_CREATE:
    {
        CreateWindow("Button", "登录", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
            90, 80, 200, 40, hwnd, (HMENU)IDB_LOGIN, hInstance, NULL);

        CreateWindow("Button", "注册", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
            90, 150, 190, 40, hwnd, (HMENU)IDB_REGISTER, hInstance, NULL);

        CreateWindow("Button", "游客登录", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
            90, 200, 190, 20, hwnd, (HMENU)IDB_VISILAND, hInstance, NULL);

        CreateWindow("Button", "同意", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX,
            50, 320, 100, 20, hwnd, (HMENU)IDB_FOUR, hInstance, NULL);
        return 0;
    }

    //点击子控件后的操作
    case WM_COMMAND:
    {
        switch LOWORD(wParam)
        {
        case IDB_LOGIN:                                               //进入账号登录的窗口
        {
            llogin = CreateWindow("login", "账号登录", WS_EX_LAYERED | WS_OVERLAPPED | WS_CAPTION,
                550, 200, 350, 360, NULL, NULL, hInstance, NULL);
            ShowWindow(llogin, 1);
            UpdateWindow(llogin);
            return 0;
        }
        case IDB_REGISTER:                                               //进入账号注册的窗口
        {
            rregister = CreateWindow("register", "账号注册", WS_EX_LAYERED | WS_OVERLAPPED | WS_CAPTION,
                520, 200, 350, 360, hwnd, NULL, hInstance, NULL);
            ShowWindow(rregister, 1);
            UpdateWindow(rregister);
            return 0;
        }
        case IDB_VISILAND:                                              //进入游客登陆的窗口,当选择游客登陆返回提示信息并退出
        {
            MessageBox(hwnd, "退出。", "提示", MB_OK | MB_ICONINFORMATION);
            PostQuitMessage(0);
            return 0;
        }
        case IDB_FOUR:
        {
            SendMessage((HWND)lParam, WM_SETTEXT, (WPARAM)NULL, (LPARAM)"同意");
            return 0;
        }
        }
        return 0;
    }

    case WM_CLOSE:
    {
        DestroyWindow(hwnd);
        return 0;
    }

    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }

    default:
    {
        return DefWindowProc(hwnd, msg, wParam, lParam);                              //把不关心的进程丢给Windows处理
    }
    }
    return 0;
}

LRESULT CALLBACK LoginWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    HINSTANCE hInstance = GetModuleHandle(NULL);                                  //获取一个模块句柄
    HWND edits[2] = {0};                                                                //编辑框句柄

    switch (msg)
    {
    case WM_PAINT:
    {
        return 0;
    }
    case WM_CREATE:
    {
        CreateWindow("static", "账号:", WS_VISIBLE | WS_CHILD | SS_CENTERIMAGE/*水平居中*/,
            50, 80, 40, 40, hwnd, (HMENU)IDE_LOGIN1, hInstance, NULL);

        CreateWindow("static", "密码:", WS_VISIBLE | WS_CHILD | SS_CENTERIMAGE/*水平居中*/,
            50, 150, 40, 40, hwnd, (HMENU)IDE_LOGIN2, hInstance, NULL);

        edits[0] = CreateWindow("edit", "", WS_VISIBLE | WS_CHILD | ES_LEFT/*左对齐*/
            | ES_AUTOHSCROLL/*水平自滚*/ | ES_NOHIDESEL/*重新聚焦反色选中*/ | WS_BORDER/*边框*/,
            90, 80, 200, 40, hwnd, (HMENU)IDE_LOGIN3, hInstance, NULL);
        Edit_LimitText(edits[0], 14);
        //GetWindowText(edits[0], get_account, 14);

        edits[1] = CreateWindowEx( NULL,"edit","", WS_CHILD|WS_VISIBLE|WS_BORDER|ES_PASSWORD,
            90, 150, 190, 40, hwnd, NULL, hInstance, NULL );
        Edit_LimitText(edits[1], 16);
        //GetWindowText(edits[1], get_password, 16);

        CreateWindow("Button", "登录", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
            90, 250, 190, 40, hwnd, (HMENU)IDB_LOGIN5, hInstance, NULL);
        return 0;
    }

    case WM_COMMAND:
    {
        switch LOWORD(wParam)
        {
        case IDE_REGISTER1:
        {
            return 0;
        }
        case IDE_LOGIN2:
        {
            return 0;
        }
        case IDE_LOGIN3:
        {
            return 0;
        }
        case IDE_LOGIN4:
        {   
            
            return 0;
        }
        case IDB_LOGIN5:
        {
            if (mysql_query(&mysql, QUERY(get_account, get_password)) != 0)
            {

            }
            MessageBox(hwnd, "登录成功", "提示", MB_OK | MB_ICONINFORMATION);
            return 0;
        }
        return 0;
        }
        return 0;
    }

    case WM_CLOSE:
    {
        DestroyWindow(hwnd);                                                  //摧毁当前窗口,而不是摧毁所有窗口
        return 0;
    }

    default:
    {
        return DefWindowProc(hwnd, msg, wParam, lParam);            //把不关心的进程丢给Windows处理
    }
    }
    return 0;
}

LRESULT CALLBACK RegisterWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{
    HINSTANCE hInstance = GetModuleHandle(NULL);                                  //获取一个模块句柄
    HWND iimprove;
    HWND edits[2];                                                                //编辑框句柄
    switch (msg)
    {
    case WM_CREATE:
    {
        CreateWindow("static", "账号:", WS_VISIBLE | WS_CHILD | SS_CENTERIMAGE/*水平居中*/,
            50, 80, 40, 40, hwnd, (HMENU)IDE_REGISTER1, hInstance, NULL);

        CreateWindow("static", "密码:", WS_VISIBLE | WS_CHILD | SS_CENTERIMAGE/*水平居中*/,
            50, 150, 40, 40, hwnd, (HMENU)IDE_REGISTER2, hInstance, NULL);

        edits[0] = CreateWindow("edit", "", WS_VISIBLE | WS_CHILD | ES_LEFT/*左对齐*/
            | ES_AUTOHSCROLL/*水平自滚*/ | ES_NOHIDESEL/*重新聚焦反色选中*/ | WS_BORDER/*边框*/,
            90, 80, 200, 40, hwnd, (HMENU)IDE_REGISTER3, hInstance, NULL);
        Edit_LimitText(edits[0], 14);

        edits[1] = CreateWindowEx(NULL, "edit", "", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_PASSWORD,
            90, 150, 190, 40, hwnd, NULL, hInstance, NULL);
        Edit_LimitText(edits[1], 16);

        CreateWindow("Button", "下一步", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
            90, 250, 190, 40, hwnd, (HMENU)IDB_REGISTER5, hInstance, NULL);
        return 0;
    }

    case WM_COMMAND:
    {
        switch LOWORD(wParam)
        {
        case IDE_REGISTER1:
        {
            return 0;
        }
        case IDE_REGISTER2:
        {
            return 0;
        }
        case IDE_REGISTER3:
        {
            GetWindowText(edits[0], get_account, 14);
            return 0;
        }
        case IDE_REGISTER4:
        {
            GetWindowText(edits[1], get_password, 16);
            return 0;
        }
        case IDB_REGISTER5:
        {
            iimprove = CreateWindow("improve", "信息完善", WS_EX_LAYERED | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
                300, 100, 700, 600, hwnd, NULL, hInstance, NULL);
            ShowWindow(iimprove, 1);
            UpdateWindow(iimprove);
            return 0;
        }
        return 0;
        }
        return 0;
    }

    case WM_CLOSE:
    {
        DestroyWindow(hwnd);                                                  //摧毁当前窗口,而不是摧毁所有窗口
        return 0;
    }

    default:
    {
        return DefWindowProc(hwnd, msg, wParam, lParam);                     //把不关心的进程丢给Windows处理
    }
    return 0;
    }
}

LRESULT CALLBACK ImproveWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
     HINSTANCE hInstance = GetModuleHandle(NULL);                                  //获取一个模块句柄
     HWND edits[5];                                                                //编辑框句柄

     switch (msg)
     {
     case WM_CREATE:
     {
         CreateWindow("static", "真实姓名:", WS_VISIBLE | WS_CHILD | SS_CENTERIMAGE/*水平居中*/,
             25, 80, 65, 40, hwnd, (HMENU)IDE_IMPROVE1, hInstance, NULL);

         CreateWindow("static", "所在单位:", WS_VISIBLE | WS_CHILD | SS_CENTERIMAGE/*水平居中*/,
             25, 140, 65, 40, hwnd, (HMENU)IDE_IMPROVE2, hInstance, NULL);

         CreateWindow("static", "性别:", WS_VISIBLE | WS_CHILD | SS_CENTERIMAGE/*水平居中*/,
             50, 200, 40, 40, hwnd, (HMENU)IDE_IMPROVE3, hInstance, NULL);

         CreateWindow("static", "年龄:", WS_VISIBLE | WS_CHILD | SS_CENTERIMAGE/*水平居中*/,
             50, 260, 40, 40, hwnd, (HMENU)IDE_IMPROVE4, hInstance, NULL);

         CreateWindow("static", "个性签名:", WS_VISIBLE | WS_CHILD | SS_CENTERIMAGE/*水平居中*/,
             25, 320, 60, 40, hwnd, (HMENU)IDE_IMPROVE5, hInstance, NULL);

         edits[0] = CreateWindow("edit", "", WS_VISIBLE | WS_CHILD | ES_LEFT/*左对齐*/
             | ES_AUTOHSCROLL/*水平自滚*/ | ES_NOHIDESEL/*重新聚焦反色选中*/ | WS_BORDER/*边框*/,
             90, 80, 250, 40, hwnd, (HMENU)IDE_IMPROVE6, hInstance, NULL);
         Edit_LimitText(edits[0], 10);


         edits[1] = CreateWindow("edit", "", WS_VISIBLE | WS_CHILD | WS_BORDER,
             90, 140, 250, 40, hwnd, (HMENU)IDE_IMPROVE7, hInstance, NULL);
         Edit_LimitText(edits[1], 30);

         edits[2] = CreateWindow("edit", "", WS_VISIBLE | WS_CHILD | ES_LEFT/*左对齐*/
             | ES_AUTOHSCROLL/*水平自滚*/ | ES_NOHIDESEL/*重新聚焦反色选中*/ | WS_BORDER/*边框*/,
             90, 200, 250, 40, hwnd, (HMENU)IDE_IMPROVE8, hInstance, NULL);
         Edit_LimitText(edits[2], 4);

         edits[3] = CreateWindow("edit", "", WS_VISIBLE | WS_CHILD | WS_BORDER,
             90, 260, 250, 40, hwnd, (HMENU)IDE_IMPROVE9, hInstance, NULL);
         Edit_LimitText(edits[3], 2);

         edits[4] = CreateWindow("edit", "", WS_VISIBLE | WS_CHILD | ES_LEFT/*左对齐*/
             | ES_AUTOHSCROLL/*水平自滚*/ | ES_NOHIDESEL/*重新聚焦反色选中*/ | WS_BORDER/*边框*/,
             90, 320, 300, 40, hwnd, (HMENU)IDE_IMPROVE10, hInstance, NULL);
         Edit_LimitText(edits[4], 2);

         CreateWindow("Button", "注册完成", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
             90, 400, 200, 40, hwnd, (HMENU)IDB_IMPROVE11, hInstance, NULL);
         return 0;
     }

     case WM_COMMAND:
     {
         switch LOWORD(wParam)
         {
         case IDE_IMPROVE6:
         {
             GetWindowText(edits[0], get_name, 10);
             return 0;
         }
         case IDE_IMPROVE7:
         {
             GetWindowText(edits[1], get_unit, 10);
             return 0;
         }
         case IDE_IMPROVE8:
         {
             GetWindowText(edits[2], get_unit, 10);
             return 0;
         }
         case IDE_IMPROVE9:
         {
             GetWindowText(edits[3], get_unit, 10);
             return 0;
         }
         case IDE_IMPROVE10:
         {
             GetWindowText(edits[4], get_unit, 10);
             return 0;
         }
         case IDB_IMPROVE11:
         {
             return 0;
         }
         return 0;
         }
         return 0;
     }

     case WM_CLOSE:
     {
         DestroyWindow(hwnd);                                                  //摧毁当前窗口,而不是摧毁所有窗口
         return 0;
     }

     default:
     {
         return DefWindowProc(hwnd, msg, wParam, lParam);            //把不关心的进程丢给Windows处理
     }
     }
        return 0;
}
搜索更多相关主题的帖子: NULL case mysql HWND return 
2020-11-09 23:19
rain9797
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2020-3-1
得分:0 
我发现问题所在,一个失误,结了
2020-11-10 15:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-503843-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.089700 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved