我试图让我的窗户可以拖动。
这是我的代码,
#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#include "include/GLFW/glfw3.h"
void cursor_position_callback(GLFWwindow* window, double x, double y);
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
int cp_x;
int cp_y;
int wrel_cpx;
int wrel_cpy;
int w_posx;
int w_posy;
int buttonEvent;
int main(){
glfwInit();
glfwWindowHint(GLFW_DECORATED, 0);
GLFWwindow *window = glfwCreateWindow(640, 480, "Undecorated Resizable", 0, 0);
int w_width;
int w_height;
int ccp_x;
int ccp_y;
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwMakeContextCurrent(window);
while(!glfwWindowShouldClose(window)){
if(buttonEvent == 1){
glfwSetWindowPos(window, wrel_cpx - cp_x, wrel_cpy - cp_y);
}
glfwSwapBuffers(window);
glfwWaitEvents();
}
return 0;
}
void cursor_position_callback(GLFWwindow* window, double x, double y){
glfwGetWindowPos(window, &w_posx, &w_posy);
wrel_cpx = cp_x + w_posx;
wrel_cpy = cp_y + w_posy;
cp_x = floor(x);
cp_y = floor(y);
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
buttonEvent = 1;
}
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
buttonEvent = 0;
}
}
当我运行我的程序并试图拖动窗口时,我的窗口的位置将保持其位置并且绝对没有任何作用。
如果我将wrel_cpx - cp_x
更改为wrel_cpx + cp_x
之类的内容,我的窗口会变得疯狂。
有人可以帮帮我吗?
答案 0 :(得分:3)
也许答案对某人有用。 用于拖动未修饰窗口的正确代码:
#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
void cursor_position_callback(GLFWwindow* window, double x, double y);
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
int cp_x;
int cp_y;
int offset_cpx;
int offset_cpy;
int w_posx;
int w_posy;
int buttonEvent;
int main(){
glfwInit();
glfwWindowHint(GLFW_DECORATED, 0);
GLFWwindow *window = glfwCreateWindow(640, 480, "Undecorated Resizable", 0, 0);
int w_width;
int w_height;
int ccp_x;
int ccp_y;
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwMakeContextCurrent(window);
while(!glfwWindowShouldClose(window)){
if(buttonEvent == 1){
glfwGetWindowPos(window, &w_posx, &w_posy);
glfwSetWindowPos(window, w_posx + offset_cpx, w_posy + offset_cpy);
offset_cpx = 0;
offset_cpy = 0;
cp_x += offset_cpx;
cp_y += offset_cpy;
}
glfwSwapBuffers(window);
glfwWaitEvents();
}
return 0;
}
void cursor_position_callback(GLFWwindow* window, double x, double y){
if (buttonEvent == 1) {
offset_cpx = x - cp_x;
offset_cpy = y - cp_y;
}
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
buttonEvent = 1;
double x, y;
glfwGetCursorPos(window, &x, &y);
cp_x = floor(x);
cp_y = floor(y);
}
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
buttonEvent = 0;
cp_x = 0;
cp_y = 0;
}
}