许多不知从何而来的警告

问题描述 投票:0回答:1

我正在尝试创建电影观看程序,一切都很好,但我没有尝试将 sqlite 包含到该程序中。许多警告甚至与 sqlite 无关。没有任何帮助,所以我希望这里有人可以告诉我该怎么做,因为程序无法构建。

问题出在哪里?

完整代码:

main.c:

#include <windows.h>
#include <stdio.h>
#include <commctrl.h>
#include <pthread.h>
#include "sqlite3.h"
#include "header.h"

static HWND MainWindow, BT_Play, STATIC_ProgBarPos, STATIC_MoneyStatus, STATIC_DBStatus, BT_Info, movieProgress;

int devStatus = 1;
int BT_PlayStatus = 0;

int money = 1000;   //delete later
//int movieProgress = 100;  //delete later
    
void UpdateLabel(void);
void WatchingMovie(void);
void SwitchStatus(void);

pthread_t thread1, thread2;

sqlite3 *db;
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) 
{
    switch(Message) 
    {
        case WM_CREATE:
        {
            break;
        }
        case WM_COMMAND:
        {
            if(wParam == id_BT_Play)
            {
                SwitchStatus();
                pthread_create(&thread1, NULL, WatchingMovie, NULL);
                break;
            }
            if(wParam == id_BT_Info)
            {
            char str[20];
            sprintf(str, "%d", money);
            MessageBox(NULL, str,"Info",MB_ICONEXCLAMATION|MB_OK);
            break;
            }
        }
        case WM_HSCROLL:
            UpdateLabel();
            break;
        case WM_DESTROY: 
        {
            PostQuitMessage(0);
            break;
        }
        default:
            return DefWindowProc(hwnd, Message, wParam, lParam);
    }
    return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{
    
    WNDCLASSEX wc; /* A properties struct of our window */
    HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
    MSG msg; /* A temporary location for all messages */


    /* zero out the struct and set the stuff we want to modify */
    memset(&wc,0,sizeof(wc));
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.lpfnWndProc   = WndProc; /* This is where we will send messages to */
    wc.hInstance     = hInstance;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    
    /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszClassName = "WindowClass";
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */


    int result  = sqlite3_open("Catalog.db", &db);

    if(!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }
    
    

    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, /* x */
        CW_USEDEFAULT, /* y */
        800, /* width */
        600, /* height */
        NULL,NULL,hInstance,NULL);
    BT_Play = CreateWindowEx(NULL,"BUTTON","Play",WS_VISIBLE|WS_CHILD,
        330, /* x */
        440, /* y */
        100, /* width */
        50, /* height */
        hwnd,(HMENU)id_BT_Play,hInstance,NULL);
    BT_Info = CreateWindowEx(NULL,"BUTTON","Info",WS_VISIBLE|WS_CHILD,
        600, /* x */
        200, /* y */
        100, /* width */
        50, /* height */
        hwnd,(HMENU)id_BT_Info,hInstance,NULL);
    STATIC_ProgBarPos = CreateWindowEx(NULL,"Static", "0", WS_CHILD | WS_VISIBLE, 
        270,
        60,
        30,
        30,
        hwnd, (HMENU)id_STATIC_ProgBarPos, NULL, NULL);
    STATIC_MoneyStatus = CreateWindowEx(NULL,"Static", "0", WS_CHILD | WS_VISIBLE, 
        400,
        100,
        40,
        30,
        hwnd, (HMENU)id_STATIC_MoneyStatus, NULL, NULL);
    movieProgress = CreateWindowW(TRACKBAR_CLASSW, L"Trackbar Control",
        WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS,
        20,
        20,
        500,
        40,
        hwnd, (HMENU) 3, NULL, NULL);
    
    SendMessageW(movieProgress, TBM_SETRANGE,  TRUE, MAKELONG(0, 100)); 
    SendMessageW(movieProgress, TBM_SETPAGESIZE, 0,  10); 
    SendMessageW(movieProgress, TBM_SETTICFREQ, 10, 0); 
    SendMessageW(movieProgress, TBM_SETPOS, TRUE, 0); 
    
    if(devStatus == 1)
    {
        STATIC_DBStatus = CreateWindowEx(NULL,"Static", "0", WS_CHILD | WS_VISIBLE, 
        1,
        1,
        20,
        20,
        hwnd, (HMENU)id_STATIC_DBStatus, NULL, NULL);
    }

    if(hwnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }
    while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
        TranslateMessage(&msg); /* Translate key codes to chars if present */
        DispatchMessage(&msg); /* Send it to WndProc */
    }
    return msg.wParam;
}
void UpdateLabel(void) 
{

    LRESULT pos = SendMessageW(movieProgress, TBM_GETPOS, 0, 0);
    wchar_t buf[4];
    sprintf(buf, "%d", pos);

    SetWindowText(STATIC_ProgBarPos, buf);
}

void WatchingMovie(void)
{
    while(BT_PlayStatus == 1)
    {
        money-=20;
        //movieProgress+=1;
        int pos = SendMessageW(movieProgress, TBM_GETPOS, 0, 0);
        SendMessageW(movieProgress, TBM_SETPOS, TRUE, pos+1);
        pthread_create(&thread2, NULL, UpdateLabel, NULL);
        char tempStr[20];
        sprintf(tempStr, "%d", money);
        SetWindowText(STATIC_MoneyStatus, tempStr);
        sleep(1);
        //SwitchStatus();
    }
}
void SwitchStatus(void)
{
    if(BT_PlayStatus == 1)
    {
        BT_PlayStatus = 0;
        SetWindowText(BT_Play,"Play");
    }
    else
    {
        BT_PlayStatus = 1;
        SetWindowText(BT_Play,"Pause");
    }
}

标题.h:

#ifndef HEADER_H

#define HEADER_H

#include <windows.h>

#define id_BT_Play 1001

#define id_SB_Progress 1002

#define id_STATIC_ProgBarPos 1003

#define id_BT_Info 1004

#define id_STATIC_MoneyStatus 1005

#define id_STATIC_DBStatus 1006

#endif

我试图删除有关 sqlite3、有关线程的所有内容。无论如何,“CreateWindowEx”甚至“sprintf”都存在一些问题(为什么?)。

c winapi
1个回答
0
投票

我查看了你的代码,我想我已经查明了所有这些警告的原因。有一些小错误和不一致之处。

首先,您将

sprintf
wchar_t
缓冲区一起使用,这是不兼容的。
sprintf
函数需要一个
char*
缓冲区。由于您使用的是宽字符 (
wchar_t
),因此您应该使用
swprintf
来代替。以下是修复方法:

wchar_t buf[4];
swprintf(buf, 4, L"%d", pos);

同样,当您使用

SetWindowText
时,请谨慎对待 Unicode。如果您的项目设置为 Unicode,则应使用
SetWindowTextW
并使用
wchar_t
字符串。

其次,线程的函数签名不正确。传递给

pthread_create
的函数应采用
void* function_name(void*)
形式。您的
UpdateLabel
WatchingMovie
函数当前返回
void
并且不带任何参数。你应该像这样修改它们:

void* UpdateLabel(void* arg)
{
    // Function content
    return NULL;
}

void* WatchingMovie(void* arg)
{
    // Function content
    return NULL;
}

此外,由于您使用的是 Windows,因此最好使用 Windows 特定的线程函数,例如

CreateThread
而不是
pthread

还有一点是

sleep(1);
的使用。在 Windows 上,您应该使用
Sleep(1000);
(注意大写“S”和以毫秒为单位的时间)。

最后,在使用

CreateWindowEx
和其他函数时,请注意 ANSI 与 Unicode 版本。根据您的项目设置中是否定义了
UNICODE
,您可能需要使用
CreateWindowExW
等用于 Unicode 的函数。

© www.soinside.com 2019 - 2024. All rights reserved.